diff --git a/Cargo.lock b/Cargo.lock index 970b1719aa120..50fc0f1a44061 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4148,6 +4148,7 @@ dependencies = [ name = "rustc_parse_format" version = "0.0.0" dependencies = [ + "rustc_data_structures", "rustc_lexer", ] diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs index 8b07c11066357..63bc0d552c11e 100644 --- a/compiler/rustc_builtin_macros/src/format.rs +++ b/compiler/rustc_builtin_macros/src/format.rs @@ -333,7 +333,7 @@ pub fn make_format_args( parse::Piece::String(s) => { unfinished_literal.push_str(s); } - parse::Piece::NextArgument(parse::Argument { position, position_span, format }) => { + parse::Piece::NextArgument(box parse::Argument { position, position_span, format }) => { if !unfinished_literal.is_empty() { template.push(FormatArgsPiece::Literal(Symbol::intern(&unfinished_literal))); unfinished_literal.clear(); diff --git a/compiler/rustc_parse_format/Cargo.toml b/compiler/rustc_parse_format/Cargo.toml index fcc68b3a219cc..72da398d3fc19 100644 --- a/compiler/rustc_parse_format/Cargo.toml +++ b/compiler/rustc_parse_format/Cargo.toml @@ -5,3 +5,4 @@ edition = "2021" [dependencies] rustc_lexer = { path = "../rustc_lexer" } +rustc_data_structures = { path = "../rustc_data_structures" } diff --git a/compiler/rustc_parse_format/src/lib.rs b/compiler/rustc_parse_format/src/lib.rs index 0113eb4e3d102..9cbe04c1288ef 100644 --- a/compiler/rustc_parse_format/src/lib.rs +++ b/compiler/rustc_parse_format/src/lib.rs @@ -58,13 +58,13 @@ impl InnerOffset { /// A piece is a portion of the format string which represents the next part /// to emit. These are emitted as a stream by the `Parser` class. -#[derive(Copy, Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq)] pub enum Piece<'a> { /// A literal string which should directly be emitted String(&'a str), /// This describes that formatting should process the next argument (as /// specified inside) for emission. - NextArgument(Argument<'a>), + NextArgument(Box>), } /// Representation of an argument specification. @@ -244,7 +244,7 @@ impl<'a> Iterator for Parser<'a> { } else { self.suggest_positional_arg_instead_of_captured_arg(arg); } - Some(NextArgument(arg)) + Some(NextArgument(Box::new(arg))) } } '}' => { @@ -908,5 +908,9 @@ fn find_skips_from_snippet( (skips, true) } +// Assert a reasonable size for `Piece` +#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] +rustc_data_structures::static_assert_size!(Piece<'_>, 16); + #[cfg(test)] mod tests; diff --git a/compiler/rustc_parse_format/src/tests.rs b/compiler/rustc_parse_format/src/tests.rs index 3f9cb149b53eb..2992ba845ab16 100644 --- a/compiler/rustc_parse_format/src/tests.rs +++ b/compiler/rustc_parse_format/src/tests.rs @@ -76,51 +76,51 @@ fn invalid_precision() { fn format_nothing() { same( "{}", - &[NextArgument(Argument { + &[NextArgument(Box::new(Argument { position: ArgumentImplicitlyIs(0), position_span: InnerSpan { start: 2, end: 2 }, format: fmtdflt(), - })], + }))], ); } #[test] fn format_position() { same( "{3}", - &[NextArgument(Argument { + &[NextArgument(Box::new(Argument { position: ArgumentIs(3), position_span: InnerSpan { start: 2, end: 3 }, format: fmtdflt(), - })], + }))], ); } #[test] fn format_position_nothing_else() { same( "{3:}", - &[NextArgument(Argument { + &[NextArgument(Box::new(Argument { position: ArgumentIs(3), position_span: InnerSpan { start: 2, end: 3 }, format: fmtdflt(), - })], + }))], ); } #[test] fn format_named() { same( "{name}", - &[NextArgument(Argument { + &[NextArgument(Box::new(Argument { position: ArgumentNamed("name"), position_span: InnerSpan { start: 2, end: 6 }, format: fmtdflt(), - })], + }))], ) } #[test] fn format_type() { same( "{3:x}", - &[NextArgument(Argument { + &[NextArgument(Box::new(Argument { position: ArgumentIs(3), position_span: InnerSpan { start: 2, end: 3 }, format: FormatSpec { @@ -134,14 +134,14 @@ fn format_type() { ty: "x", ty_span: None, }, - })], + }))], ); } #[test] fn format_align_fill() { same( "{3:>}", - &[NextArgument(Argument { + &[NextArgument(Box::new(Argument { position: ArgumentIs(3), position_span: InnerSpan { start: 2, end: 3 }, format: FormatSpec { @@ -155,11 +155,11 @@ fn format_align_fill() { ty: "", ty_span: None, }, - })], + }))], ); same( "{3:0<}", - &[NextArgument(Argument { + &[NextArgument(Box::new(Argument { position: ArgumentIs(3), position_span: InnerSpan { start: 2, end: 3 }, format: FormatSpec { @@ -173,11 +173,11 @@ fn format_align_fill() { ty: "", ty_span: None, }, - })], + }))], ); same( "{3:*