Skip to content

Commit 0abf8a0

Browse files
committed
Replace format flags u32 by enums and bools.
1 parent db137ba commit 0abf8a0

File tree

6 files changed

+154
-62
lines changed

6 files changed

+154
-62
lines changed

compiler/rustc_ast/src/format.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,30 @@ pub struct FormatOptions {
227227
pub alignment: Option<FormatAlignment>,
228228
/// The fill character. E.g. the `.` in `{:.>10}`.
229229
pub fill: Option<char>,
230-
/// The `+`, `-`, `0`, `#`, `x?` and `X?` flags.
231-
pub flags: u32,
230+
/// The `+` or `-` flag.
231+
pub sign: Option<FormatSign>,
232+
/// The `#` flag.
233+
pub alternate: bool,
234+
/// The `0` flag. E.g. the `0` in `{:02x}`.
235+
pub zero_pad: bool,
236+
/// The `x` or `X` flag (for `Debug` only). E.g. the `x` in `{:x?}`.
237+
pub debug_hex: Option<FormatDebugHex>,
238+
}
239+
240+
#[derive(Copy, Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
241+
pub enum FormatSign {
242+
/// The `+` flag.
243+
Plus,
244+
/// The `-` flag.
245+
Minus,
246+
}
247+
248+
#[derive(Copy, Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
249+
pub enum FormatDebugHex {
250+
/// The `x` flag in `{:x?}`.
251+
Lower,
252+
/// The `X` flag in `{:X?}`.
253+
Upper,
232254
}
233255

234256
#[derive(Copy, Clone, Encodable, Decodable, Debug, PartialEq, Eq)]

compiler/rustc_ast_lowering/src/format.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,14 @@ fn make_format_spec<'hir>(
148148
None => sym::Unknown,
149149
},
150150
);
151-
let flags = ctx.expr_u32(sp, placeholder.format_options.flags);
151+
// This needs to match `FlagV1` in library/core/src/fmt/mod.rs.
152+
let flags: u32 = ((placeholder.format_options.sign == Some(FormatSign::Plus)) as u32)
153+
| ((placeholder.format_options.sign == Some(FormatSign::Minus)) as u32) << 1
154+
| (placeholder.format_options.alternate as u32) << 2
155+
| (placeholder.format_options.zero_pad as u32) << 3
156+
| ((placeholder.format_options.debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4
157+
| ((placeholder.format_options.debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5;
158+
let flags = ctx.expr_u32(sp, flags);
152159
let prec = make_count(ctx, sp, &placeholder.format_options.precision, argmap);
153160
let width = make_count(ctx, sp, &placeholder.format_options.width, argmap);
154161
let format_placeholder_new = ctx.arena.alloc(ctx.expr_lang_item_type_relative(

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use rustc_ast::token;
66
use rustc_ast::util::literal::escape_byte_str_symbol;
77
use rustc_ast::util::parser::{self, AssocOp, Fixity};
88
use rustc_ast::{self as ast, BlockCheckMode};
9-
use rustc_ast::{FormatAlignment, FormatArgPosition, FormatArgsPiece, FormatCount, FormatTrait};
9+
use rustc_ast::{
10+
FormatAlignment, FormatArgPosition, FormatArgsPiece, FormatCount, FormatDebugHex, FormatSign,
11+
FormatTrait,
12+
};
1013
use std::fmt::Write;
1114

1215
impl<'a> State<'a> {
@@ -675,17 +678,15 @@ pub fn reconstruct_format_args_template_string(pieces: &[FormatArgsPiece]) -> St
675678
Some(FormatAlignment::Center) => template.push_str("^"),
676679
None => {}
677680
}
678-
let flags = p.format_options.flags;
679-
if flags >> (rustc_parse_format::FlagSignPlus as usize) & 1 != 0 {
680-
template.push('+');
681-
}
682-
if flags >> (rustc_parse_format::FlagSignMinus as usize) & 1 != 0 {
683-
template.push('-');
681+
match p.format_options.sign {
682+
Some(FormatSign::Plus) => template.push('+'),
683+
Some(FormatSign::Minus) => template.push('-'),
684+
None => {}
684685
}
685-
if flags >> (rustc_parse_format::FlagAlternate as usize) & 1 != 0 {
686+
if p.format_options.alternate {
686687
template.push('#');
687688
}
688-
if flags >> (rustc_parse_format::FlagSignAwareZeroPad as usize) & 1 != 0 {
689+
if p.format_options.zero_pad {
689690
template.push('0');
690691
}
691692
if let Some(width) = &p.format_options.width {
@@ -709,11 +710,10 @@ pub fn reconstruct_format_args_template_string(pieces: &[FormatArgsPiece]) -> St
709710
}
710711
}
711712
}
712-
if flags >> (rustc_parse_format::FlagDebugLowerHex as usize) & 1 != 0 {
713-
template.push('x');
714-
}
715-
if flags >> (rustc_parse_format::FlagDebugUpperHex as usize) & 1 != 0 {
716-
template.push('X');
713+
match p.format_options.debug_hex {
714+
Some(FormatDebugHex::Lower) => template.push('x'),
715+
Some(FormatDebugHex::Upper) => template.push('X'),
716+
None => {}
717717
}
718718
template.push_str(match p.format_trait {
719719
FormatTrait::Display => "",

compiler/rustc_builtin_macros/src/format.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_ast::tokenstream::TokenStream;
44
use rustc_ast::{
55
Expr, ExprKind, FormatAlignment, FormatArgPosition, FormatArgPositionKind, FormatArgs,
66
FormatArgsPiece, FormatArgument, FormatArgumentKind, FormatArguments, FormatCount,
7-
FormatOptions, FormatPlaceholder, FormatTrait,
7+
FormatDebugHex, FormatOptions, FormatPlaceholder, FormatSign, FormatTrait,
88
};
99
use rustc_data_structures::fx::FxHashSet;
1010
use rustc_errors::{pluralize, Applicability, MultiSpan, PResult};
@@ -435,7 +435,16 @@ pub fn make_format_args(
435435
format_options: FormatOptions {
436436
fill: format.fill,
437437
alignment,
438-
flags: format.flags,
438+
sign: format.sign.map(|s| match s {
439+
parse::Sign::Plus => FormatSign::Plus,
440+
parse::Sign::Minus => FormatSign::Minus,
441+
}),
442+
alternate: format.alternate,
443+
zero_pad: format.zero_pad,
444+
debug_hex: format.debug_hex.map(|s| match s {
445+
parse::DebugHex::Lower => FormatDebugHex::Lower,
446+
parse::DebugHex::Upper => FormatDebugHex::Upper,
447+
}),
439448
precision,
440449
width,
441450
},

compiler/rustc_parse_format/src/lib.rs

+37-28
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
pub use Alignment::*;
1818
pub use Count::*;
19-
pub use Flag::*;
2019
pub use Piece::*;
2120
pub use Position::*;
2221

@@ -111,8 +110,14 @@ pub struct FormatSpec<'a> {
111110
pub fill: Option<char>,
112111
/// Optionally specified alignment.
113112
pub align: Alignment,
114-
/// Packed version of various flags provided.
115-
pub flags: u32,
113+
/// The `+` or `-` flag.
114+
pub sign: Option<Sign>,
115+
/// The `#` flag.
116+
pub alternate: bool,
117+
/// The `0` flag.
118+
pub zero_pad: bool,
119+
/// The `x` or `X` flag. (Only for `Debug`.)
120+
pub debug_hex: Option<DebugHex>,
116121
/// The integer precision to use.
117122
pub precision: Count<'a>,
118123
/// The span of the precision formatting flag (for diagnostics).
@@ -162,24 +167,22 @@ pub enum Alignment {
162167
AlignUnknown,
163168
}
164169

165-
/// Various flags which can be applied to format strings. The meaning of these
166-
/// flags is defined by the formatters themselves.
170+
/// Enum for the sign flags.
167171
#[derive(Copy, Clone, Debug, PartialEq)]
168-
pub enum Flag {
169-
/// A `+` will be used to denote positive numbers.
170-
FlagSignPlus,
171-
/// A `-` will be used to denote negative numbers. This is the default.
172-
FlagSignMinus,
173-
/// An alternate form will be used for the value. In the case of numbers,
174-
/// this means that the number will be prefixed with the supplied string.
175-
FlagAlternate,
176-
/// For numbers, this means that the number will be padded with zeroes,
177-
/// and the sign (`+` or `-`) will precede them.
178-
FlagSignAwareZeroPad,
179-
/// For Debug / `?`, format integers in lower-case hexadecimal.
180-
FlagDebugLowerHex,
181-
/// For Debug / `?`, format integers in upper-case hexadecimal.
182-
FlagDebugUpperHex,
172+
pub enum Sign {
173+
/// The `+` flag.
174+
Plus,
175+
/// The `-` flag.
176+
Minus,
177+
}
178+
179+
/// Enum for the debug hex flags.
180+
#[derive(Copy, Clone, Debug, PartialEq)]
181+
pub enum DebugHex {
182+
/// The `x` flag in `{:x?}`.
183+
Lower,
184+
/// The `X` flag in `{:X?}`.
185+
Upper,
183186
}
184187

185188
/// A count is used for the precision and width parameters of an integer, and
@@ -597,7 +600,10 @@ impl<'a> Parser<'a> {
597600
let mut spec = FormatSpec {
598601
fill: None,
599602
align: AlignUnknown,
600-
flags: 0,
603+
sign: None,
604+
alternate: false,
605+
zero_pad: false,
606+
debug_hex: None,
601607
precision: CountImplied,
602608
precision_span: None,
603609
width: CountImplied,
@@ -626,13 +632,13 @@ impl<'a> Parser<'a> {
626632
}
627633
// Sign flags
628634
if self.consume('+') {
629-
spec.flags |= 1 << (FlagSignPlus as u32);
635+
spec.sign = Some(Sign::Plus);
630636
} else if self.consume('-') {
631-
spec.flags |= 1 << (FlagSignMinus as u32);
637+
spec.sign = Some(Sign::Minus);
632638
}
633639
// Alternate marker
634640
if self.consume('#') {
635-
spec.flags |= 1 << (FlagAlternate as u32);
641+
spec.alternate = true;
636642
}
637643
// Width and precision
638644
let mut havewidth = false;
@@ -647,7 +653,7 @@ impl<'a> Parser<'a> {
647653
spec.width_span = Some(self.span(end - 1, end + 1));
648654
havewidth = true;
649655
} else {
650-
spec.flags |= 1 << (FlagSignAwareZeroPad as u32);
656+
spec.zero_pad = true;
651657
}
652658
}
653659

@@ -678,14 +684,14 @@ impl<'a> Parser<'a> {
678684
// Optional radix followed by the actual format specifier
679685
if self.consume('x') {
680686
if self.consume('?') {
681-
spec.flags |= 1 << (FlagDebugLowerHex as u32);
687+
spec.debug_hex = Some(DebugHex::Lower);
682688
spec.ty = "?";
683689
} else {
684690
spec.ty = "x";
685691
}
686692
} else if self.consume('X') {
687693
if self.consume('?') {
688-
spec.flags |= 1 << (FlagDebugUpperHex as u32);
694+
spec.debug_hex = Some(DebugHex::Upper);
689695
spec.ty = "?";
690696
} else {
691697
spec.ty = "X";
@@ -708,7 +714,10 @@ impl<'a> Parser<'a> {
708714
let mut spec = FormatSpec {
709715
fill: None,
710716
align: AlignUnknown,
711-
flags: 0,
717+
sign: None,
718+
alternate: false,
719+
zero_pad: false,
720+
debug_hex: None,
712721
precision: CountImplied,
713722
precision_span: None,
714723
width: CountImplied,

0 commit comments

Comments
 (0)