Skip to content

Commit 6cd5429

Browse files
committed
Use simplified formatting flags in fmt::Arguments.
1 parent c51957b commit 6cd5429

File tree

6 files changed

+128
-114
lines changed

6 files changed

+128
-114
lines changed

compiler/rustc_ast_lowering/src/expr.rs

-5
Original file line numberDiff line numberDiff line change
@@ -2162,11 +2162,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
21622162
self.expr(sp, hir::ExprKind::Lit(lit))
21632163
}
21642164

2165-
pub(super) fn expr_char(&mut self, sp: Span, value: char) -> hir::Expr<'hir> {
2166-
let lit = self.arena.alloc(hir::Lit { span: sp, node: ast::LitKind::Char(value) });
2167-
self.expr(sp, hir::ExprKind::Lit(lit))
2168-
}
2169-
21702165
pub(super) fn expr_str(&mut self, sp: Span, value: Symbol) -> hir::Expr<'hir> {
21712166
let lit = self
21722167
.arena

compiler/rustc_ast_lowering/src/format.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -361,24 +361,25 @@ fn make_format_spec<'hir>(
361361
zero_pad,
362362
debug_hex,
363363
} = &placeholder.format_options;
364-
let fill = ctx.expr_char(sp, fill.unwrap_or(' '));
365-
let align = ctx.expr_lang_item_type_relative(
366-
sp,
367-
hir::LangItem::FormatAlignment,
368-
match alignment {
369-
Some(FormatAlignment::Left) => sym::Left,
370-
Some(FormatAlignment::Right) => sym::Right,
371-
Some(FormatAlignment::Center) => sym::Center,
372-
None => sym::Unknown,
373-
},
374-
);
375-
// This needs to match `Flag` in library/core/src/fmt/rt.rs.
364+
let fill = fill.unwrap_or(' ');
365+
// These need to match the constants in library/core/src/fmt/rt.rs.
366+
let align = match alignment {
367+
Some(FormatAlignment::Left) => 0,
368+
Some(FormatAlignment::Right) => 1,
369+
Some(FormatAlignment::Center) => 2,
370+
None => 3,
371+
};
372+
// This needs to match the constants in library/core/src/fmt/rt.rs.
376373
let flags: u32 = ((sign == Some(FormatSign::Plus)) as u32)
377374
| ((sign == Some(FormatSign::Minus)) as u32) << 1
378375
| (alternate as u32) << 2
379376
| (zero_pad as u32) << 3
380377
| ((debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4
381-
| ((debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5;
378+
| ((debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5
379+
| align << 6
380+
| (width.is_some() as u32) << 8
381+
| (precision.is_some() as u32) << 9
382+
| (fill as u32) << 11;
382383
let flags = ctx.expr_u32(sp, flags);
383384
let precision = make_count(ctx, sp, precision, argmap);
384385
let width = make_count(ctx, sp, width, argmap);
@@ -387,7 +388,7 @@ fn make_format_spec<'hir>(
387388
hir::LangItem::FormatPlaceholder,
388389
sym::new,
389390
));
390-
let args = ctx.arena.alloc_from_iter([position, fill, align, flags, precision, width]);
391+
let args = ctx.arena.alloc_from_iter([position, flags, precision, width]);
391392
ctx.expr_call_mut(sp, format_placeholder_new, args)
392393
}
393394

compiler/rustc_hir/src/lang_items.rs

-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ language_item_table! {
321321
BeginPanic, sym::begin_panic, begin_panic_fn, Target::Fn, GenericRequirement::None;
322322

323323
// Lang items needed for `format_args!()`.
324-
FormatAlignment, sym::format_alignment, format_alignment, Target::Enum, GenericRequirement::None;
325324
FormatArgument, sym::format_argument, format_argument, Target::Struct, GenericRequirement::None;
326325
FormatArguments, sym::format_arguments, format_arguments, Target::Struct, GenericRequirement::None;
327326
FormatCount, sym::format_count, format_count, Target::Enum, GenericRequirement::None;

compiler/rustc_span/src/symbol.rs

-1
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,6 @@ symbols! {
975975
forbid,
976976
forget,
977977
format,
978-
format_alignment,
979978
format_args,
980979
format_args_capture,
981980
format_args_macro,

0 commit comments

Comments
 (0)