Skip to content

Commit dd37150

Browse files
committed
Reduce FormattingOptions to 64 bit.
1 parent ccb9429 commit dd37150

11 files changed

+354
-334
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

+21-19
Original file line numberDiff line numberDiff line change
@@ -361,24 +361,26 @@ 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.
376-
let flags: u32 = ((sign == Some(FormatSign::Plus)) as u32)
377-
| ((sign == Some(FormatSign::Minus)) as u32) << 1
378-
| (alternate as u32) << 2
379-
| (zero_pad as u32) << 3
380-
| ((debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4
381-
| ((debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5;
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.
373+
let flags: u32 = fill as u32
374+
| ((sign == Some(FormatSign::Plus)) as u32) << 21
375+
| ((sign == Some(FormatSign::Minus)) as u32) << 22
376+
| (alternate as u32) << 23
377+
| (zero_pad as u32) << 24
378+
| ((debug_hex == Some(FormatDebugHex::Lower)) as u32) << 25
379+
| ((debug_hex == Some(FormatDebugHex::Upper)) as u32) << 26
380+
| (width.is_some() as u32) << 27
381+
| (precision.is_some() as u32) << 28
382+
| align << 29
383+
| 1 << 31; // Highest bit always set.
382384
let flags = ctx.expr_u32(sp, flags);
383385
let precision = make_count(ctx, sp, precision, argmap);
384386
let width = make_count(ctx, sp, width, argmap);
@@ -387,7 +389,7 @@ fn make_format_spec<'hir>(
387389
hir::LangItem::FormatPlaceholder,
388390
sym::new,
389391
));
390-
let args = ctx.arena.alloc_from_iter([position, fill, align, flags, precision, width]);
392+
let args = ctx.arena.alloc_from_iter([position, flags, precision, width]);
391393
ctx.expr_call_mut(sp, format_placeholder_new, args)
392394
}
393395

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,

library/core/src/fmt/float.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ where
8686
true => flt2dec::Sign::MinusPlus,
8787
};
8888

89-
if let Some(precision) = fmt.options.precision {
89+
if let Some(precision) = fmt.options.get_precision() {
9090
float_to_decimal_common_exact(fmt, num, sign, precision)
9191
} else {
9292
let min_precision = 0;
@@ -162,7 +162,7 @@ where
162162
true => flt2dec::Sign::MinusPlus,
163163
};
164164

165-
if let Some(precision) = fmt.options.precision {
165+
if let Some(precision) = fmt.options.get_precision() {
166166
// 1 integral digit + `precision` fractional digits = `precision + 1` total digits
167167
float_to_exponential_common_exact(fmt, num, sign, precision + 1, upper)
168168
} else {
@@ -180,7 +180,7 @@ where
180180
true => flt2dec::Sign::MinusPlus,
181181
};
182182

183-
if let Some(precision) = fmt.options.precision {
183+
if let Some(precision) = fmt.options.get_precision() {
184184
// this behavior of {:.PREC?} predates exponential formatting for {:?}
185185
float_to_decimal_common_exact(fmt, num, sign, precision)
186186
} else {

0 commit comments

Comments
 (0)