Skip to content

Commit 44e7fa8

Browse files
committed
Put const data of format_args in const block.
1 parent 4f55e11 commit 44e7fa8

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,6 +2270,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
22702270
)
22712271
}
22722272

2273+
pub(super) fn expr_const(&mut self, span: Span, expr: hir::Expr<'hir>) -> hir::Expr<'hir> {
2274+
let node_id = self.next_node_id();
2275+
let def_id =
2276+
self.create_def(node_id, None, DefKind::InlineConst, DefPathData::LateAnonConst, span);
2277+
let hir_id = self.lower_node_id(node_id);
2278+
let body = self.record_body(&[], expr);
2279+
self.expr(span, hir::ExprKind::ConstBlock(hir::ConstBlock { hir_id, def_id, body }))
2280+
}
2281+
22732282
fn expr_block_empty(&mut self, span: Span) -> &'hir hir::Expr<'hir> {
22742283
let blk = self.block_all(span, &[], None);
22752284
let expr = self.expr_block(blk);

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,12 @@ fn expand_format_args<'hir>(
525525
pieces.push(make_piece(ctx, sym::num, zero, macsp));
526526

527527
// ```
528-
// unsafe { <core::fmt::rt::Template>::new(&[pieces…]) }
528+
// unsafe { <core::fmt::rt::Template>::new(const { &[pieces…] }) }
529529
// ```
530530
let template_new =
531531
ctx.expr_lang_item_type_relative(macsp, hir::LangItem::FormatTemplate, sym::new);
532532
let pieces = ctx.expr_array_ref(macsp, ctx.arena.alloc_from_iter(pieces));
533+
let pieces = ctx.expr_const(macsp, pieces);
533534
let template = ctx.expr(
534535
macsp,
535536
hir::ExprKind::Call(ctx.arena.alloc(template_new), ctx.arena.alloc_from_iter([pieces])),

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11741174
}
11751175
}
11761176

1177-
fn record_body(
1177+
pub(super) fn record_body(
11781178
&mut self,
11791179
params: &'hir [hir::Param<'hir>],
11801180
value: hir::Expr<'hir>,

library/core/src/fmt/rt.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,11 @@ pub union Piece {
6767
unsafe impl Send for Piece {}
6868
unsafe impl Sync for Piece {}
6969

70-
// These are marked as #[stable] because of #[rustc_promotable] and #[rustc_const_stable].
71-
// With #[rustc_const_unstable], many format_args!() invocations would result in errors.
72-
//
73-
// There is still no way to use these on stable, because Piece itself is #[unstable] and not
74-
// reachable through any public path. (format_args!()'s expansion uses it as a lang item.)
7570
impl Piece {
76-
#[rustc_promotable]
77-
#[stable(feature = "rust1", since = "1.0.0")]
78-
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
7971
pub const fn str(s: &'static str) -> Self {
8072
Self { p: s as *const str as *const u8 }
8173
}
8274

83-
#[rustc_promotable]
84-
#[stable(feature = "rust1", since = "1.0.0")]
85-
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
8675
pub const fn num(i: usize) -> Self {
8776
Self { i }
8877
}

0 commit comments

Comments
 (0)