diff options
| author | bors <bors@rust-lang.org> | 2025-03-30 21:59:02 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-03-30 21:59:02 +0000 |
| commit | 2ea33b591050c4ca1a3752830b29112638faecf6 (patch) | |
| tree | 06a7036e609ed309df1d7f2dd252065470ffdcc3 | |
| parent | fedf10752b973d83f3f7c0f819b2659c21a634de (diff) | |
| parent | cc5ee70b1a51bf30e85d261cc6e485eb3431bcac (diff) | |
Auto merge of #139131 - m-ou-se:format-args-struct-expr, r=Mark-Simulacrum
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 https://github.com/rust-lang/rust/pull/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 tackle https://github.com/rust-lang/rust/issues/92698 (It doesn't change anything yet though, because the `args` argument to `fmt::Arguments::new_v1_formatted()` is not const-promotable.)
| -rw-r--r-- | compiler/rustc_ast_lowering/src/format.rs | 28 | ||||
| -rw-r--r-- | compiler/rustc_span/src/symbol.rs | 4 | ||||
| -rw-r--r-- | library/core/src/fmt/rt.rs | 8 |
3 files changed, 18 insertions, 22 deletions
diff --git a/compiler/rustc_ast_lowering/src/format.rs b/compiler/rustc_ast_lowering/src/format.rs index 343895984ca..0de0319c667 100644 --- a/compiler/rustc_ast_lowering/src/format.rs +++ b/compiler/rustc_ast_lowering/src/format.rs @@ -323,14 +323,12 @@ fn make_count<'hir>( /// Generates /// /// ```text -/// <core::fmt::rt::Placeholder::new( -/// …usize, // position -/// '…', // fill -/// <core::fmt::rt::Alignment>::…, // alignment -/// …u32, // flags -/// <core::fmt::rt::Count::…>, // width -/// <core::fmt::rt::Count::…>, // precision -/// ) +/// <core::fmt::rt::Placeholder { +/// position: …usize, +/// flags: …u32, +/// precision: <core::fmt::rt::Count::…>, +/// width: <core::fmt::rt::Count::…>, +/// } /// ``` fn make_format_spec<'hir>( ctx: &mut LoweringContext<'_, 'hir>, @@ -384,13 +382,13 @@ fn make_format_spec<'hir>( let flags = ctx.expr_u32(sp, flags); let precision = make_count(ctx, sp, precision, argmap); let width = make_count(ctx, sp, width, argmap); - let format_placeholder_new = ctx.arena.alloc(ctx.expr_lang_item_type_relative( - sp, - hir::LangItem::FormatPlaceholder, - sym::new, - )); - let args = ctx.arena.alloc_from_iter([position, flags, precision, width]); - ctx.expr_call_mut(sp, format_placeholder_new, args) + let position = ctx.expr_field(Ident::new(sym::position, sp), ctx.arena.alloc(position), sp); + let flags = ctx.expr_field(Ident::new(sym::flags, sp), ctx.arena.alloc(flags), sp); + let precision = ctx.expr_field(Ident::new(sym::precision, sp), ctx.arena.alloc(precision), sp); + let width = ctx.expr_field(Ident::new(sym::width, sp), ctx.arena.alloc(width), sp); + let placeholder = ctx.arena.alloc(hir::QPath::LangItem(hir::LangItem::FormatPlaceholder, sp)); + let fields = ctx.arena.alloc_from_iter([position, flags, precision, width]); + ctx.expr(sp, hir::ExprKind::Struct(placeholder, fields, hir::StructTailExpr::None)) } fn expand_format_args<'hir>( diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 6807f96e712..cc4c9860bc2 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -986,6 +986,7 @@ symbols! { field_init_shorthand, file, file_options, + flags, float, float_to_int_unchecked, floorf128, @@ -1570,6 +1571,7 @@ symbols! { pointer_like, poll, poll_next, + position, post_dash_lto: "post-lto", postfix_match, powerpc_target_feature, @@ -1585,6 +1587,7 @@ symbols! { precise_capturing, precise_capturing_in_traits, precise_pointer_size_matching, + precision, pref_align_of, prefetch_read_data, prefetch_read_instruction, @@ -2274,6 +2277,7 @@ symbols! { wasm_target_feature, where_clause_attrs, while_let, + width, windows, windows_subsystem, with_negative_coherence, diff --git a/library/core/src/fmt/rt.rs b/library/core/src/fmt/rt.rs index d27f7e6e0d8..0b04ebccae2 100644 --- a/library/core/src/fmt/rt.rs +++ b/library/core/src/fmt/rt.rs @@ -20,8 +20,8 @@ pub struct Placeholder { pub width: Count, } +#[cfg(bootstrap)] impl Placeholder { - #[cfg(bootstrap)] #[inline] pub const fn new( position: usize, @@ -33,12 +33,6 @@ impl Placeholder { ) -> Self { Self { position, fill, align, flags, precision, width } } - - #[cfg(not(bootstrap))] - #[inline] - pub const fn new(position: usize, flags: u32, precision: Count, width: Count) -> Self { - Self { position, flags, precision, width } - } } #[cfg(bootstrap)] |
