Auto merge of #105363 - WaffleLapkin:thin2win_box_next_argument, r=nnethercote
Shrink `rustc_parse_format::Piece` This makes both variants closer together in size (previously they were different by 208 bytes -- 16 vs 224). This may make things worse, but it's worth a try. r? `@nnethercote`
This commit is contained in:
commit
0d5573e6da
5 changed files with 50 additions and 44 deletions
|
@ -4168,6 +4168,7 @@ dependencies = [
|
||||||
name = "rustc_parse_format"
|
name = "rustc_parse_format"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"rustc_data_structures",
|
||||||
"rustc_lexer",
|
"rustc_lexer",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -333,7 +333,7 @@ pub fn make_format_args(
|
||||||
parse::Piece::String(s) => {
|
parse::Piece::String(s) => {
|
||||||
unfinished_literal.push_str(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() {
|
if !unfinished_literal.is_empty() {
|
||||||
template.push(FormatArgsPiece::Literal(Symbol::intern(&unfinished_literal)));
|
template.push(FormatArgsPiece::Literal(Symbol::intern(&unfinished_literal)));
|
||||||
unfinished_literal.clear();
|
unfinished_literal.clear();
|
||||||
|
|
|
@ -5,3 +5,4 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rustc_lexer = { path = "../rustc_lexer" }
|
rustc_lexer = { path = "../rustc_lexer" }
|
||||||
|
rustc_data_structures = { path = "../rustc_data_structures" }
|
||||||
|
|
|
@ -58,13 +58,13 @@ impl InnerOffset {
|
||||||
|
|
||||||
/// A piece is a portion of the format string which represents the next part
|
/// 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.
|
/// 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> {
|
pub enum Piece<'a> {
|
||||||
/// A literal string which should directly be emitted
|
/// A literal string which should directly be emitted
|
||||||
String(&'a str),
|
String(&'a str),
|
||||||
/// This describes that formatting should process the next argument (as
|
/// This describes that formatting should process the next argument (as
|
||||||
/// specified inside) for emission.
|
/// specified inside) for emission.
|
||||||
NextArgument(Argument<'a>),
|
NextArgument(Box<Argument<'a>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Representation of an argument specification.
|
/// Representation of an argument specification.
|
||||||
|
@ -244,7 +244,7 @@ impl<'a> Iterator for Parser<'a> {
|
||||||
} else {
|
} else {
|
||||||
self.suggest_positional_arg_instead_of_captured_arg(arg);
|
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)
|
(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)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
|
@ -76,51 +76,51 @@ fn invalid_precision() {
|
||||||
fn format_nothing() {
|
fn format_nothing() {
|
||||||
same(
|
same(
|
||||||
"{}",
|
"{}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentImplicitlyIs(0),
|
position: ArgumentImplicitlyIs(0),
|
||||||
position_span: InnerSpan { start: 2, end: 2 },
|
position_span: InnerSpan { start: 2, end: 2 },
|
||||||
format: fmtdflt(),
|
format: fmtdflt(),
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_position() {
|
fn format_position() {
|
||||||
same(
|
same(
|
||||||
"{3}",
|
"{3}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
position_span: InnerSpan { start: 2, end: 3 },
|
position_span: InnerSpan { start: 2, end: 3 },
|
||||||
format: fmtdflt(),
|
format: fmtdflt(),
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_position_nothing_else() {
|
fn format_position_nothing_else() {
|
||||||
same(
|
same(
|
||||||
"{3:}",
|
"{3:}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
position_span: InnerSpan { start: 2, end: 3 },
|
position_span: InnerSpan { start: 2, end: 3 },
|
||||||
format: fmtdflt(),
|
format: fmtdflt(),
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_named() {
|
fn format_named() {
|
||||||
same(
|
same(
|
||||||
"{name}",
|
"{name}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentNamed("name"),
|
position: ArgumentNamed("name"),
|
||||||
position_span: InnerSpan { start: 2, end: 6 },
|
position_span: InnerSpan { start: 2, end: 6 },
|
||||||
format: fmtdflt(),
|
format: fmtdflt(),
|
||||||
})],
|
}))],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_type() {
|
fn format_type() {
|
||||||
same(
|
same(
|
||||||
"{3:x}",
|
"{3:x}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
position_span: InnerSpan { start: 2, end: 3 },
|
position_span: InnerSpan { start: 2, end: 3 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -134,14 +134,14 @@ fn format_type() {
|
||||||
ty: "x",
|
ty: "x",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_align_fill() {
|
fn format_align_fill() {
|
||||||
same(
|
same(
|
||||||
"{3:>}",
|
"{3:>}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
position_span: InnerSpan { start: 2, end: 3 },
|
position_span: InnerSpan { start: 2, end: 3 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -155,11 +155,11 @@ fn format_align_fill() {
|
||||||
ty: "",
|
ty: "",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
same(
|
same(
|
||||||
"{3:0<}",
|
"{3:0<}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
position_span: InnerSpan { start: 2, end: 3 },
|
position_span: InnerSpan { start: 2, end: 3 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -173,11 +173,11 @@ fn format_align_fill() {
|
||||||
ty: "",
|
ty: "",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
same(
|
same(
|
||||||
"{3:*<abcd}",
|
"{3:*<abcd}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
position_span: InnerSpan { start: 2, end: 3 },
|
position_span: InnerSpan { start: 2, end: 3 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -191,14 +191,14 @@ fn format_align_fill() {
|
||||||
ty: "abcd",
|
ty: "abcd",
|
||||||
ty_span: Some(InnerSpan::new(6, 10)),
|
ty_span: Some(InnerSpan::new(6, 10)),
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_counts() {
|
fn format_counts() {
|
||||||
same(
|
same(
|
||||||
"{:10x}",
|
"{:10x}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentImplicitlyIs(0),
|
position: ArgumentImplicitlyIs(0),
|
||||||
position_span: InnerSpan { start: 2, end: 2 },
|
position_span: InnerSpan { start: 2, end: 2 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -212,11 +212,11 @@ fn format_counts() {
|
||||||
ty: "x",
|
ty: "x",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
same(
|
same(
|
||||||
"{:10$.10x}",
|
"{:10$.10x}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentImplicitlyIs(0),
|
position: ArgumentImplicitlyIs(0),
|
||||||
position_span: InnerSpan { start: 2, end: 2 },
|
position_span: InnerSpan { start: 2, end: 2 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -230,11 +230,11 @@ fn format_counts() {
|
||||||
ty: "x",
|
ty: "x",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
same(
|
same(
|
||||||
"{1:0$.10x}",
|
"{1:0$.10x}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentIs(1),
|
position: ArgumentIs(1),
|
||||||
position_span: InnerSpan { start: 2, end: 3 },
|
position_span: InnerSpan { start: 2, end: 3 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -248,11 +248,11 @@ fn format_counts() {
|
||||||
ty: "x",
|
ty: "x",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
same(
|
same(
|
||||||
"{:.*x}",
|
"{:.*x}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentImplicitlyIs(1),
|
position: ArgumentImplicitlyIs(1),
|
||||||
position_span: InnerSpan { start: 2, end: 2 },
|
position_span: InnerSpan { start: 2, end: 2 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -266,11 +266,11 @@ fn format_counts() {
|
||||||
ty: "x",
|
ty: "x",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
same(
|
same(
|
||||||
"{:.10$x}",
|
"{:.10$x}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentImplicitlyIs(0),
|
position: ArgumentImplicitlyIs(0),
|
||||||
position_span: InnerSpan { start: 2, end: 2 },
|
position_span: InnerSpan { start: 2, end: 2 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -284,11 +284,11 @@ fn format_counts() {
|
||||||
ty: "x",
|
ty: "x",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
same(
|
same(
|
||||||
"{:a$.b$?}",
|
"{:a$.b$?}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentImplicitlyIs(0),
|
position: ArgumentImplicitlyIs(0),
|
||||||
position_span: InnerSpan { start: 2, end: 2 },
|
position_span: InnerSpan { start: 2, end: 2 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -302,11 +302,11 @@ fn format_counts() {
|
||||||
ty: "?",
|
ty: "?",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
same(
|
same(
|
||||||
"{:.4}",
|
"{:.4}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentImplicitlyIs(0),
|
position: ArgumentImplicitlyIs(0),
|
||||||
position_span: InnerSpan { start: 2, end: 2 },
|
position_span: InnerSpan { start: 2, end: 2 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -320,14 +320,14 @@ fn format_counts() {
|
||||||
ty: "",
|
ty: "",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_flags() {
|
fn format_flags() {
|
||||||
same(
|
same(
|
||||||
"{:-}",
|
"{:-}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentImplicitlyIs(0),
|
position: ArgumentImplicitlyIs(0),
|
||||||
position_span: InnerSpan { start: 2, end: 2 },
|
position_span: InnerSpan { start: 2, end: 2 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -341,11 +341,11 @@ fn format_flags() {
|
||||||
ty: "",
|
ty: "",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
same(
|
same(
|
||||||
"{:+#}",
|
"{:+#}",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentImplicitlyIs(0),
|
position: ArgumentImplicitlyIs(0),
|
||||||
position_span: InnerSpan { start: 2, end: 2 },
|
position_span: InnerSpan { start: 2, end: 2 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -359,7 +359,7 @@ fn format_flags() {
|
||||||
ty: "",
|
ty: "",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -368,7 +368,7 @@ fn format_mixture() {
|
||||||
"abcd {3:x} efg",
|
"abcd {3:x} efg",
|
||||||
&[
|
&[
|
||||||
String("abcd "),
|
String("abcd "),
|
||||||
NextArgument(Argument {
|
NextArgument(Box::new(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
position_span: InnerSpan { start: 7, end: 8 },
|
position_span: InnerSpan { start: 7, end: 8 },
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
|
@ -382,7 +382,7 @@ fn format_mixture() {
|
||||||
ty: "x",
|
ty: "x",
|
||||||
ty_span: None,
|
ty_span: None,
|
||||||
},
|
},
|
||||||
}),
|
})),
|
||||||
String(" efg"),
|
String(" efg"),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
@ -391,18 +391,18 @@ fn format_mixture() {
|
||||||
fn format_whitespace() {
|
fn format_whitespace() {
|
||||||
same(
|
same(
|
||||||
"{ }",
|
"{ }",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentImplicitlyIs(0),
|
position: ArgumentImplicitlyIs(0),
|
||||||
position_span: InnerSpan { start: 2, end: 3 },
|
position_span: InnerSpan { start: 2, end: 3 },
|
||||||
format: fmtdflt(),
|
format: fmtdflt(),
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
same(
|
same(
|
||||||
"{ }",
|
"{ }",
|
||||||
&[NextArgument(Argument {
|
&[NextArgument(Box::new(Argument {
|
||||||
position: ArgumentImplicitlyIs(0),
|
position: ArgumentImplicitlyIs(0),
|
||||||
position_span: InnerSpan { start: 2, end: 4 },
|
position_span: InnerSpan { start: 2, end: 4 },
|
||||||
format: fmtdflt(),
|
format: fmtdflt(),
|
||||||
})],
|
}))],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue