Replace format flags u32 by enums and bools.

This commit is contained in:
Mara Bos 2023-01-13 13:32:49 +01:00
parent db137ba7d4
commit 0abf8a0617
6 changed files with 154 additions and 62 deletions

View file

@ -227,8 +227,30 @@ pub struct FormatOptions {
pub alignment: Option<FormatAlignment>,
/// The fill character. E.g. the `.` in `{:.>10}`.
pub fill: Option<char>,
/// The `+`, `-`, `0`, `#`, `x?` and `X?` flags.
pub flags: u32,
/// The `+` or `-` flag.
pub sign: Option<FormatSign>,
/// The `#` flag.
pub alternate: bool,
/// The `0` flag. E.g. the `0` in `{:02x}`.
pub zero_pad: bool,
/// The `x` or `X` flag (for `Debug` only). E.g. the `x` in `{:x?}`.
pub debug_hex: Option<FormatDebugHex>,
}
#[derive(Copy, Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
pub enum FormatSign {
/// The `+` flag.
Plus,
/// The `-` flag.
Minus,
}
#[derive(Copy, Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
pub enum FormatDebugHex {
/// The `x` flag in `{:x?}`.
Lower,
/// The `X` flag in `{:X?}`.
Upper,
}
#[derive(Copy, Clone, Encodable, Decodable, Debug, PartialEq, Eq)]

View file

@ -148,7 +148,14 @@ fn make_format_spec<'hir>(
None => sym::Unknown,
},
);
let flags = ctx.expr_u32(sp, placeholder.format_options.flags);
// This needs to match `FlagV1` in library/core/src/fmt/mod.rs.
let flags: u32 = ((placeholder.format_options.sign == Some(FormatSign::Plus)) as u32)
| ((placeholder.format_options.sign == Some(FormatSign::Minus)) as u32) << 1
| (placeholder.format_options.alternate as u32) << 2
| (placeholder.format_options.zero_pad as u32) << 3
| ((placeholder.format_options.debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4
| ((placeholder.format_options.debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5;
let flags = ctx.expr_u32(sp, flags);
let prec = make_count(ctx, sp, &placeholder.format_options.precision, argmap);
let width = make_count(ctx, sp, &placeholder.format_options.width, argmap);
let format_placeholder_new = ctx.arena.alloc(ctx.expr_lang_item_type_relative(

View file

@ -6,7 +6,10 @@ use rustc_ast::token;
use rustc_ast::util::literal::escape_byte_str_symbol;
use rustc_ast::util::parser::{self, AssocOp, Fixity};
use rustc_ast::{self as ast, BlockCheckMode};
use rustc_ast::{FormatAlignment, FormatArgPosition, FormatArgsPiece, FormatCount, FormatTrait};
use rustc_ast::{
FormatAlignment, FormatArgPosition, FormatArgsPiece, FormatCount, FormatDebugHex, FormatSign,
FormatTrait,
};
use std::fmt::Write;
impl<'a> State<'a> {
@ -675,17 +678,15 @@ pub fn reconstruct_format_args_template_string(pieces: &[FormatArgsPiece]) -> St
Some(FormatAlignment::Center) => template.push_str("^"),
None => {}
}
let flags = p.format_options.flags;
if flags >> (rustc_parse_format::FlagSignPlus as usize) & 1 != 0 {
template.push('+');
match p.format_options.sign {
Some(FormatSign::Plus) => template.push('+'),
Some(FormatSign::Minus) => template.push('-'),
None => {}
}
if flags >> (rustc_parse_format::FlagSignMinus as usize) & 1 != 0 {
template.push('-');
}
if flags >> (rustc_parse_format::FlagAlternate as usize) & 1 != 0 {
if p.format_options.alternate {
template.push('#');
}
if flags >> (rustc_parse_format::FlagSignAwareZeroPad as usize) & 1 != 0 {
if p.format_options.zero_pad {
template.push('0');
}
if let Some(width) = &p.format_options.width {
@ -709,11 +710,10 @@ pub fn reconstruct_format_args_template_string(pieces: &[FormatArgsPiece]) -> St
}
}
}
if flags >> (rustc_parse_format::FlagDebugLowerHex as usize) & 1 != 0 {
template.push('x');
}
if flags >> (rustc_parse_format::FlagDebugUpperHex as usize) & 1 != 0 {
template.push('X');
match p.format_options.debug_hex {
Some(FormatDebugHex::Lower) => template.push('x'),
Some(FormatDebugHex::Upper) => template.push('X'),
None => {}
}
template.push_str(match p.format_trait {
FormatTrait::Display => "",

View file

@ -4,7 +4,7 @@ use rustc_ast::tokenstream::TokenStream;
use rustc_ast::{
Expr, ExprKind, FormatAlignment, FormatArgPosition, FormatArgPositionKind, FormatArgs,
FormatArgsPiece, FormatArgument, FormatArgumentKind, FormatArguments, FormatCount,
FormatOptions, FormatPlaceholder, FormatTrait,
FormatDebugHex, FormatOptions, FormatPlaceholder, FormatSign, FormatTrait,
};
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{pluralize, Applicability, MultiSpan, PResult};
@ -435,7 +435,16 @@ pub fn make_format_args(
format_options: FormatOptions {
fill: format.fill,
alignment,
flags: format.flags,
sign: format.sign.map(|s| match s {
parse::Sign::Plus => FormatSign::Plus,
parse::Sign::Minus => FormatSign::Minus,
}),
alternate: format.alternate,
zero_pad: format.zero_pad,
debug_hex: format.debug_hex.map(|s| match s {
parse::DebugHex::Lower => FormatDebugHex::Lower,
parse::DebugHex::Upper => FormatDebugHex::Upper,
}),
precision,
width,
},

View file

@ -16,7 +16,6 @@
pub use Alignment::*;
pub use Count::*;
pub use Flag::*;
pub use Piece::*;
pub use Position::*;
@ -111,8 +110,14 @@ pub struct FormatSpec<'a> {
pub fill: Option<char>,
/// Optionally specified alignment.
pub align: Alignment,
/// Packed version of various flags provided.
pub flags: u32,
/// The `+` or `-` flag.
pub sign: Option<Sign>,
/// The `#` flag.
pub alternate: bool,
/// The `0` flag.
pub zero_pad: bool,
/// The `x` or `X` flag. (Only for `Debug`.)
pub debug_hex: Option<DebugHex>,
/// The integer precision to use.
pub precision: Count<'a>,
/// The span of the precision formatting flag (for diagnostics).
@ -162,24 +167,22 @@ pub enum Alignment {
AlignUnknown,
}
/// Various flags which can be applied to format strings. The meaning of these
/// flags is defined by the formatters themselves.
/// Enum for the sign flags.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Flag {
/// A `+` will be used to denote positive numbers.
FlagSignPlus,
/// A `-` will be used to denote negative numbers. This is the default.
FlagSignMinus,
/// An alternate form will be used for the value. In the case of numbers,
/// this means that the number will be prefixed with the supplied string.
FlagAlternate,
/// For numbers, this means that the number will be padded with zeroes,
/// and the sign (`+` or `-`) will precede them.
FlagSignAwareZeroPad,
/// For Debug / `?`, format integers in lower-case hexadecimal.
FlagDebugLowerHex,
/// For Debug / `?`, format integers in upper-case hexadecimal.
FlagDebugUpperHex,
pub enum Sign {
/// The `+` flag.
Plus,
/// The `-` flag.
Minus,
}
/// Enum for the debug hex flags.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum DebugHex {
/// The `x` flag in `{:x?}`.
Lower,
/// The `X` flag in `{:X?}`.
Upper,
}
/// A count is used for the precision and width parameters of an integer, and
@ -597,7 +600,10 @@ impl<'a> Parser<'a> {
let mut spec = FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
precision_span: None,
width: CountImplied,
@ -626,13 +632,13 @@ impl<'a> Parser<'a> {
}
// Sign flags
if self.consume('+') {
spec.flags |= 1 << (FlagSignPlus as u32);
spec.sign = Some(Sign::Plus);
} else if self.consume('-') {
spec.flags |= 1 << (FlagSignMinus as u32);
spec.sign = Some(Sign::Minus);
}
// Alternate marker
if self.consume('#') {
spec.flags |= 1 << (FlagAlternate as u32);
spec.alternate = true;
}
// Width and precision
let mut havewidth = false;
@ -647,7 +653,7 @@ impl<'a> Parser<'a> {
spec.width_span = Some(self.span(end - 1, end + 1));
havewidth = true;
} else {
spec.flags |= 1 << (FlagSignAwareZeroPad as u32);
spec.zero_pad = true;
}
}
@ -678,14 +684,14 @@ impl<'a> Parser<'a> {
// Optional radix followed by the actual format specifier
if self.consume('x') {
if self.consume('?') {
spec.flags |= 1 << (FlagDebugLowerHex as u32);
spec.debug_hex = Some(DebugHex::Lower);
spec.ty = "?";
} else {
spec.ty = "x";
}
} else if self.consume('X') {
if self.consume('?') {
spec.flags |= 1 << (FlagDebugUpperHex as u32);
spec.debug_hex = Some(DebugHex::Upper);
spec.ty = "?";
} else {
spec.ty = "X";
@ -708,7 +714,10 @@ impl<'a> Parser<'a> {
let mut spec = FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
precision_span: None,
width: CountImplied,

View file

@ -10,7 +10,10 @@ fn fmtdflt() -> FormatSpec<'static> {
return FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
width: CountImplied,
precision_span: None,
@ -126,7 +129,10 @@ fn format_type() {
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
width: CountImplied,
precision_span: None,
@ -147,7 +153,10 @@ fn format_align_fill() {
format: FormatSpec {
fill: None,
align: AlignRight,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
width: CountImplied,
precision_span: None,
@ -165,7 +174,10 @@ fn format_align_fill() {
format: FormatSpec {
fill: Some('0'),
align: AlignLeft,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
width: CountImplied,
precision_span: None,
@ -183,7 +195,10 @@ fn format_align_fill() {
format: FormatSpec {
fill: Some('*'),
align: AlignLeft,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
width: CountImplied,
precision_span: None,
@ -204,7 +219,10 @@ fn format_counts() {
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
precision_span: None,
width: CountIs(10),
@ -222,7 +240,10 @@ fn format_counts() {
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountIs(10),
precision_span: Some(InnerSpan { start: 6, end: 9 }),
width: CountIsParam(10),
@ -240,7 +261,10 @@ fn format_counts() {
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountIs(10),
precision_span: Some(InnerSpan { start: 6, end: 9 }),
width: CountIsParam(0),
@ -258,7 +282,10 @@ fn format_counts() {
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountIsStar(0),
precision_span: Some(InnerSpan { start: 3, end: 5 }),
width: CountImplied,
@ -276,7 +303,10 @@ fn format_counts() {
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountIsParam(10),
width: CountImplied,
precision_span: Some(InnerSpan::new(3, 7)),
@ -294,7 +324,10 @@ fn format_counts() {
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountIsName("b", InnerSpan { start: 6, end: 7 }),
precision_span: Some(InnerSpan { start: 5, end: 8 }),
width: CountIsName("a", InnerSpan { start: 3, end: 4 }),
@ -312,7 +345,10 @@ fn format_counts() {
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountIs(4),
precision_span: Some(InnerSpan { start: 3, end: 5 }),
width: CountImplied,
@ -333,7 +369,10 @@ fn format_flags() {
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: (1 << FlagSignMinus as u32),
sign: Some(Sign::Minus),
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
width: CountImplied,
precision_span: None,
@ -351,7 +390,10 @@ fn format_flags() {
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: (1 << FlagSignPlus as u32) | (1 << FlagAlternate as u32),
sign: Some(Sign::Plus),
alternate: true,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
width: CountImplied,
precision_span: None,
@ -374,7 +416,10 @@ fn format_mixture() {
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
width: CountImplied,
precision_span: None,