Skip to content

Commit 3fa54aa

Browse files
committed
Auto merge of rust-lang#139131 - m-ou-se:format-args-struct-expr, r=<try>
Simplify expansion for format_args!(). Instead of calling `Placeholder::new()`, we can just use a struct expression directly. Before: ```rust Placeholder::new(…, …, …, …) ``` After: ```rust Placeholder { position: …, flags: …, width: …, precision: …, } ``` (I originally avoided the struct expression, because `Placeholder` had a lot of fields. But now that rust-lang#136974 is merged, it only has four fields left.) This will make the `fmt` argument to `fmt::Arguments::new_v1_formatted()` a candidate for const promotion, which is important if we ever hope to fix rust-lang#92698 (It doesn't change anything yet though, because the `args` argument to `fmt::Arguments::new_v1_formatted()` is not const-promotable.)
2 parents 85f518e + cc5ee70 commit 3fa54aa

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

compiler/rustc_ast_lowering/src/format.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,12 @@ fn make_count<'hir>(
323323
/// Generates
324324
///
325325
/// ```text
326-
/// <core::fmt::rt::Placeholder::new(
327-
/// …usize, // position
328-
/// '…', // fill
329-
/// <core::fmt::rt::Alignment>::…, // alignment
330-
/// …u32, // flags
331-
/// <core::fmt::rt::Count::…>, // width
332-
/// <core::fmt::rt::Count::…>, // precision
333-
/// )
326+
/// <core::fmt::rt::Placeholder {
327+
/// position: …usize,
328+
/// flags: …u32,
329+
/// precision: <core::fmt::rt::Count::…>,
330+
/// width: <core::fmt::rt::Count::…>,
331+
/// }
334332
/// ```
335333
fn make_format_spec<'hir>(
336334
ctx: &mut LoweringContext<'_, 'hir>,
@@ -384,13 +382,13 @@ fn make_format_spec<'hir>(
384382
let flags = ctx.expr_u32(sp, flags);
385383
let precision = make_count(ctx, sp, precision, argmap);
386384
let width = make_count(ctx, sp, width, argmap);
387-
let format_placeholder_new = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
388-
sp,
389-
hir::LangItem::FormatPlaceholder,
390-
sym::new,
391-
));
392-
let args = ctx.arena.alloc_from_iter([position, flags, precision, width]);
393-
ctx.expr_call_mut(sp, format_placeholder_new, args)
385+
let position = ctx.expr_field(Ident::new(sym::position, sp), ctx.arena.alloc(position), sp);
386+
let flags = ctx.expr_field(Ident::new(sym::flags, sp), ctx.arena.alloc(flags), sp);
387+
let precision = ctx.expr_field(Ident::new(sym::precision, sp), ctx.arena.alloc(precision), sp);
388+
let width = ctx.expr_field(Ident::new(sym::width, sp), ctx.arena.alloc(width), sp);
389+
let placeholder = ctx.arena.alloc(hir::QPath::LangItem(hir::LangItem::FormatPlaceholder, sp));
390+
let fields = ctx.arena.alloc_from_iter([position, flags, precision, width]);
391+
ctx.expr(sp, hir::ExprKind::Struct(placeholder, fields, hir::StructTailExpr::None))
394392
}
395393

396394
fn expand_format_args<'hir>(

compiler/rustc_span/src/symbol.rs

+4
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ symbols! {
986986
field_init_shorthand,
987987
file,
988988
file_options,
989+
flags,
989990
float,
990991
float_to_int_unchecked,
991992
floorf128,
@@ -1570,6 +1571,7 @@ symbols! {
15701571
pointer_like,
15711572
poll,
15721573
poll_next,
1574+
position,
15731575
post_dash_lto: "post-lto",
15741576
postfix_match,
15751577
powerpc_target_feature,
@@ -1585,6 +1587,7 @@ symbols! {
15851587
precise_capturing,
15861588
precise_capturing_in_traits,
15871589
precise_pointer_size_matching,
1590+
precision,
15881591
pref_align_of,
15891592
prefetch_read_data,
15901593
prefetch_read_instruction,
@@ -2274,6 +2277,7 @@ symbols! {
22742277
wasm_target_feature,
22752278
where_clause_attrs,
22762279
while_let,
2280+
width,
22772281
windows,
22782282
windows_subsystem,
22792283
with_negative_coherence,

library/core/src/fmt/rt.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub struct Placeholder {
2020
pub width: Count,
2121
}
2222

23+
#[cfg(bootstrap)]
2324
impl Placeholder {
24-
#[cfg(bootstrap)]
2525
#[inline]
2626
pub const fn new(
2727
position: usize,
@@ -33,12 +33,6 @@ impl Placeholder {
3333
) -> Self {
3434
Self { position, fill, align, flags, precision, width }
3535
}
36-
37-
#[cfg(not(bootstrap))]
38-
#[inline]
39-
pub const fn new(position: usize, flags: u32, precision: Count, width: Count) -> Self {
40-
Self { position, flags, precision, width }
41-
}
4236
}
4337

4438
#[cfg(bootstrap)]

0 commit comments

Comments
 (0)