Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrink rustc_parse_format::Piece #105363

Merged
merged 2 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4148,6 +4148,7 @@ dependencies = [
name = "rustc_parse_format"
version = "0.0.0"
dependencies = [
"rustc_data_structures",
"rustc_lexer",
]

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_parse_format/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ edition = "2021"

[dependencies]
rustc_lexer = { path = "../rustc_lexer" }
rustc_data_structures = { path = "../rustc_data_structures" }
10 changes: 7 additions & 3 deletions compiler/rustc_parse_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Argument<'a>>),
}

/// Representation of an argument specification.
Expand Down Expand Up @@ -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)))
}
}
'}' => {
Expand Down Expand Up @@ -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;
80 changes: 40 additions & 40 deletions compiler/rustc_parse_format/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -173,11 +173,11 @@ fn format_align_fill() {
ty: "",
ty_span: None,
},
})],
}))],
);
same(
"{3:*<abcd}",
&[NextArgument(Argument {
&[NextArgument(Box::new(Argument {
position: ArgumentIs(3),
position_span: InnerSpan { start: 2, end: 3 },
format: FormatSpec {
Expand All @@ -191,14 +191,14 @@ fn format_align_fill() {
ty: "abcd",
ty_span: Some(InnerSpan::new(6, 10)),
},
})],
}))],
);
}
#[test]
fn format_counts() {
same(
"{:10x}",
&[NextArgument(Argument {
&[NextArgument(Box::new(Argument {
position: ArgumentImplicitlyIs(0),
position_span: InnerSpan { start: 2, end: 2 },
format: FormatSpec {
Expand All @@ -212,11 +212,11 @@ fn format_counts() {
ty: "x",
ty_span: None,
},
})],
}))],
);
same(
"{:10$.10x}",
&[NextArgument(Argument {
&[NextArgument(Box::new(Argument {
position: ArgumentImplicitlyIs(0),
position_span: InnerSpan { start: 2, end: 2 },
format: FormatSpec {
Expand All @@ -230,11 +230,11 @@ fn format_counts() {
ty: "x",
ty_span: None,
},
})],
}))],
);
same(
"{1:0$.10x}",
&[NextArgument(Argument {
&[NextArgument(Box::new(Argument {
position: ArgumentIs(1),
position_span: InnerSpan { start: 2, end: 3 },
format: FormatSpec {
Expand All @@ -248,11 +248,11 @@ fn format_counts() {
ty: "x",
ty_span: None,
},
})],
}))],
);
same(
"{:.*x}",
&[NextArgument(Argument {
&[NextArgument(Box::new(Argument {
position: ArgumentImplicitlyIs(1),
position_span: InnerSpan { start: 2, end: 2 },
format: FormatSpec {
Expand All @@ -266,11 +266,11 @@ fn format_counts() {
ty: "x",
ty_span: None,
},
})],
}))],
);
same(
"{:.10$x}",
&[NextArgument(Argument {
&[NextArgument(Box::new(Argument {
position: ArgumentImplicitlyIs(0),
position_span: InnerSpan { start: 2, end: 2 },
format: FormatSpec {
Expand All @@ -284,11 +284,11 @@ fn format_counts() {
ty: "x",
ty_span: None,
},
})],
}))],
);
same(
"{:a$.b$?}",
&[NextArgument(Argument {
&[NextArgument(Box::new(Argument {
position: ArgumentImplicitlyIs(0),
position_span: InnerSpan { start: 2, end: 2 },
format: FormatSpec {
Expand All @@ -302,11 +302,11 @@ fn format_counts() {
ty: "?",
ty_span: None,
},
})],
}))],
);
same(
"{:.4}",
&[NextArgument(Argument {
&[NextArgument(Box::new(Argument {
position: ArgumentImplicitlyIs(0),
position_span: InnerSpan { start: 2, end: 2 },
format: FormatSpec {
Expand All @@ -320,14 +320,14 @@ fn format_counts() {
ty: "",
ty_span: None,
},
})],
}))],
)
}
#[test]
fn format_flags() {
same(
"{:-}",
&[NextArgument(Argument {
&[NextArgument(Box::new(Argument {
position: ArgumentImplicitlyIs(0),
position_span: InnerSpan { start: 2, end: 2 },
format: FormatSpec {
Expand All @@ -341,11 +341,11 @@ fn format_flags() {
ty: "",
ty_span: None,
},
})],
}))],
);
same(
"{:+#}",
&[NextArgument(Argument {
&[NextArgument(Box::new(Argument {
position: ArgumentImplicitlyIs(0),
position_span: InnerSpan { start: 2, end: 2 },
format: FormatSpec {
Expand All @@ -359,7 +359,7 @@ fn format_flags() {
ty: "",
ty_span: None,
},
})],
}))],
);
}
#[test]
Expand All @@ -368,7 +368,7 @@ fn format_mixture() {
"abcd {3:x} efg",
&[
String("abcd "),
NextArgument(Argument {
NextArgument(Box::new(Argument {
position: ArgumentIs(3),
position_span: InnerSpan { start: 7, end: 8 },
format: FormatSpec {
Expand All @@ -382,7 +382,7 @@ fn format_mixture() {
ty: "x",
ty_span: None,
},
}),
})),
String(" efg"),
],
);
Expand All @@ -391,18 +391,18 @@ fn format_mixture() {
fn format_whitespace() {
same(
"{ }",
&[NextArgument(Argument {
&[NextArgument(Box::new(Argument {
position: ArgumentImplicitlyIs(0),
position_span: InnerSpan { start: 2, end: 3 },
format: fmtdflt(),
})],
}))],
);
same(
"{ }",
&[NextArgument(Argument {
&[NextArgument(Box::new(Argument {
position: ArgumentImplicitlyIs(0),
position_span: InnerSpan { start: 2, end: 4 },
format: fmtdflt(),
})],
}))],
);
}