Skip to content

Commit 1163279

Browse files
authoredJan 27, 2023
Rollup merge of #106806 - m-ou-se:format-args-flags, r=oli-obk
Replace format flags u32 by enums and bools. This gets rid of the `flags: u32` field where each bit has a special meaning, and replaces it by simple enums and booleans. Part of #99012
2 parents 6874f4e + 21cf9db commit 1163279

File tree

7 files changed

+173
-71
lines changed

7 files changed

+173
-71
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

+23-6
Original file line numberDiff line numberDiff line change
@@ -137,26 +137,43 @@ fn make_format_spec<'hir>(
137137
}
138138
Err(_) => ctx.expr(sp, hir::ExprKind::Err),
139139
};
140-
let fill = ctx.expr_char(sp, placeholder.format_options.fill.unwrap_or(' '));
140+
let &FormatOptions {
141+
ref width,
142+
ref precision,
143+
alignment,
144+
fill,
145+
sign,
146+
alternate,
147+
zero_pad,
148+
debug_hex,
149+
} = &placeholder.format_options;
150+
let fill = ctx.expr_char(sp, fill.unwrap_or(' '));
141151
let align = ctx.expr_lang_item_type_relative(
142152
sp,
143153
hir::LangItem::FormatAlignment,
144-
match placeholder.format_options.alignment {
154+
match alignment {
145155
Some(FormatAlignment::Left) => sym::Left,
146156
Some(FormatAlignment::Right) => sym::Right,
147157
Some(FormatAlignment::Center) => sym::Center,
148158
None => sym::Unknown,
149159
},
150160
);
151-
let flags = ctx.expr_u32(sp, placeholder.format_options.flags);
152-
let prec = make_count(ctx, sp, &placeholder.format_options.precision, argmap);
153-
let width = make_count(ctx, sp, &placeholder.format_options.width, argmap);
161+
// This needs to match `FlagV1` in library/core/src/fmt/mod.rs.
162+
let flags: u32 = ((sign == Some(FormatSign::Plus)) as u32)
163+
| ((sign == Some(FormatSign::Minus)) as u32) << 1
164+
| (alternate as u32) << 2
165+
| (zero_pad as u32) << 3
166+
| ((debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4
167+
| ((debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5;
168+
let flags = ctx.expr_u32(sp, flags);
169+
let precision = make_count(ctx, sp, &precision, argmap);
170+
let width = make_count(ctx, sp, &width, argmap);
154171
let format_placeholder_new = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
155172
sp,
156173
hir::LangItem::FormatPlaceholder,
157174
sym::new,
158175
));
159-
let args = ctx.arena.alloc_from_iter([position, fill, align, flags, prec, width]);
176+
let args = ctx.arena.alloc_from_iter([position, fill, align, flags, precision, width]);
160177
ctx.expr_call_mut(sp, format_placeholder_new, args)
161178
}
162179

‎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,

‎compiler/rustc_parse_format/src/tests.rs

+60-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ fn fmtdflt() -> FormatSpec<'static> {
1010
return FormatSpec {
1111
fill: None,
1212
align: AlignUnknown,
13-
flags: 0,
13+
sign: None,
14+
alternate: false,
15+
zero_pad: false,
16+
debug_hex: None,
1417
precision: CountImplied,
1518
width: CountImplied,
1619
precision_span: None,
@@ -126,7 +129,10 @@ fn format_type() {
126129
format: FormatSpec {
127130
fill: None,
128131
align: AlignUnknown,
129-
flags: 0,
132+
sign: None,
133+
alternate: false,
134+
zero_pad: false,
135+
debug_hex: None,
130136
precision: CountImplied,
131137
width: CountImplied,
132138
precision_span: None,
@@ -147,7 +153,10 @@ fn format_align_fill() {
147153
format: FormatSpec {
148154
fill: None,
149155
align: AlignRight,
150-
flags: 0,
156+
sign: None,
157+
alternate: false,
158+
zero_pad: false,
159+
debug_hex: None,
151160
precision: CountImplied,
152161
width: CountImplied,
153162
precision_span: None,
@@ -165,7 +174,10 @@ fn format_align_fill() {
165174
format: FormatSpec {
166175
fill: Some('0'),
167176
align: AlignLeft,
168-
flags: 0,
177+
sign: None,
178+
alternate: false,
179+
zero_pad: false,
180+
debug_hex: None,
169181
precision: CountImplied,
170182
width: CountImplied,
171183
precision_span: None,
@@ -183,7 +195,10 @@ fn format_align_fill() {
183195
format: FormatSpec {
184196
fill: Some('*'),
185197
align: AlignLeft,
186-
flags: 0,
198+
sign: None,
199+
alternate: false,
200+
zero_pad: false,
201+
debug_hex: None,
187202
precision: CountImplied,
188203
width: CountImplied,
189204
precision_span: None,
@@ -204,7 +219,10 @@ fn format_counts() {
204219
format: FormatSpec {
205220
fill: None,
206221
align: AlignUnknown,
207-
flags: 0,
222+
sign: None,
223+
alternate: false,
224+
zero_pad: false,
225+
debug_hex: None,
208226
precision: CountImplied,
209227
precision_span: None,
210228
width: CountIs(10),
@@ -222,7 +240,10 @@ fn format_counts() {
222240
format: FormatSpec {
223241
fill: None,
224242
align: AlignUnknown,
225-
flags: 0,
243+
sign: None,
244+
alternate: false,
245+
zero_pad: false,
246+
debug_hex: None,
226247
precision: CountIs(10),
227248
precision_span: Some(InnerSpan { start: 6, end: 9 }),
228249
width: CountIsParam(10),
@@ -240,7 +261,10 @@ fn format_counts() {
240261
format: FormatSpec {
241262
fill: None,
242263
align: AlignUnknown,
243-
flags: 0,
264+
sign: None,
265+
alternate: false,
266+
zero_pad: false,
267+
debug_hex: None,
244268
precision: CountIs(10),
245269
precision_span: Some(InnerSpan { start: 6, end: 9 }),
246270
width: CountIsParam(0),
@@ -258,7 +282,10 @@ fn format_counts() {
258282
format: FormatSpec {
259283
fill: None,
260284
align: AlignUnknown,
261-
flags: 0,
285+
sign: None,
286+
alternate: false,
287+
zero_pad: false,
288+
debug_hex: None,
262289
precision: CountIsStar(0),
263290
precision_span: Some(InnerSpan { start: 3, end: 5 }),
264291
width: CountImplied,
@@ -276,7 +303,10 @@ fn format_counts() {
276303
format: FormatSpec {
277304
fill: None,
278305
align: AlignUnknown,
279-
flags: 0,
306+
sign: None,
307+
alternate: false,
308+
zero_pad: false,
309+
debug_hex: None,
280310
precision: CountIsParam(10),
281311
width: CountImplied,
282312
precision_span: Some(InnerSpan::new(3, 7)),
@@ -294,7 +324,10 @@ fn format_counts() {
294324
format: FormatSpec {
295325
fill: None,
296326
align: AlignUnknown,
297-
flags: 0,
327+
sign: None,
328+
alternate: false,
329+
zero_pad: false,
330+
debug_hex: None,
298331
precision: CountIsName("b", InnerSpan { start: 6, end: 7 }),
299332
precision_span: Some(InnerSpan { start: 5, end: 8 }),
300333
width: CountIsName("a", InnerSpan { start: 3, end: 4 }),
@@ -312,7 +345,10 @@ fn format_counts() {
312345
format: FormatSpec {
313346
fill: None,
314347
align: AlignUnknown,
315-
flags: 0,
348+
sign: None,
349+
alternate: false,
350+
zero_pad: false,
351+
debug_hex: None,
316352
precision: CountIs(4),
317353
precision_span: Some(InnerSpan { start: 3, end: 5 }),
318354
width: CountImplied,
@@ -333,7 +369,10 @@ fn format_flags() {
333369
format: FormatSpec {
334370
fill: None,
335371
align: AlignUnknown,
336-
flags: (1 << FlagSignMinus as u32),
372+
sign: Some(Sign::Minus),
373+
alternate: false,
374+
zero_pad: false,
375+
debug_hex: None,
337376
precision: CountImplied,
338377
width: CountImplied,
339378
precision_span: None,
@@ -351,7 +390,10 @@ fn format_flags() {
351390
format: FormatSpec {
352391
fill: None,
353392
align: AlignUnknown,
354-
flags: (1 << FlagSignPlus as u32) | (1 << FlagAlternate as u32),
393+
sign: Some(Sign::Plus),
394+
alternate: true,
395+
zero_pad: false,
396+
debug_hex: None,
355397
precision: CountImplied,
356398
width: CountImplied,
357399
precision_span: None,
@@ -374,7 +416,10 @@ fn format_mixture() {
374416
format: FormatSpec {
375417
fill: None,
376418
align: AlignUnknown,
377-
flags: 0,
419+
sign: None,
420+
alternate: false,
421+
zero_pad: false,
422+
debug_hex: None,
378423
precision: CountImplied,
379424
width: CountImplied,
380425
precision_span: None,

‎src/tools/clippy/clippy_utils/src/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,8 @@ pub struct FormatSpec<'tcx> {
711711
pub fill: Option<char>,
712712
/// Optionally specified alignment.
713713
pub align: Alignment,
714-
/// Packed version of various flags provided, see [`rustc_parse_format::Flag`].
715-
pub flags: u32,
714+
/// Whether all flag options are set to default (no flags specified).
715+
pub no_flags: bool,
716716
/// Represents either the maximum width or the integer precision.
717717
pub precision: Count<'tcx>,
718718
/// The minimum width, will be padded according to `width`/`align`
@@ -728,7 +728,7 @@ impl<'tcx> FormatSpec<'tcx> {
728728
Some(Self {
729729
fill: spec.fill,
730730
align: spec.align,
731-
flags: spec.flags,
731+
no_flags: spec.sign.is_none() && !spec.alternate && !spec.zero_pad && spec.debug_hex.is_none(),
732732
precision: Count::new(
733733
FormatParamUsage::Precision,
734734
spec.precision,
@@ -773,7 +773,7 @@ impl<'tcx> FormatSpec<'tcx> {
773773
self.width.is_implied()
774774
&& self.precision.is_implied()
775775
&& self.align == Alignment::AlignUnknown
776-
&& self.flags == 0
776+
&& self.no_flags
777777
}
778778
}
779779

0 commit comments

Comments
 (0)
Please sign in to comment.