Skip to content

Check if format argument is identifier to avoid error err-emit #140286

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

Merged
merged 3 commits into from
May 4, 2025
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
29 changes: 29 additions & 0 deletions compiler/rustc_parse_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,30 @@ pub struct Argument<'a> {
pub format: FormatSpec<'a>,
}

impl<'a> Argument<'a> {
pub fn is_identifier(&self) -> bool {
matches!(self.position, Position::ArgumentNamed(_))
&& matches!(
self.format,
FormatSpec {
fill: None,
fill_span: None,
align: AlignUnknown,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
precision_span: None,
width: CountImplied,
width_span: None,
ty: "",
ty_span: None,
},
)
}
}

/// Specification for the formatting of an argument in the format string.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct FormatSpec<'a> {
Expand Down Expand Up @@ -894,6 +918,11 @@ impl<'a> Parser<'a> {
}

fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: Argument<'a>) {
// If the argument is not an identifier, it is not a field access.
if !arg.is_identifier() {
return;
}

if let Some(end) = self.consume_pos('.') {
let byte_pos = self.to_span_index(end);
let start = InnerOffset(byte_pos.0 + 1);
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/parser/issues/invalid-parse-format-issue-139104.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() {
println!("{foo:_1.4}", foo = 3.14); //~ ERROR invalid format string: expected `}`, found `.`
println!("{0:_1.4}", 1.11); //~ ERROR invalid format string: expected `}`, found `.`
println!("{:_1.4}", 3.14); //~ ERROR invalid format string: expected `}`, found `.`

println!("{foo:_1.4", foo = 3.14); //~ ERROR invalid format string: expected `}`, found `.`
println!("{0:_1.4", 1.11); //~ ERROR invalid format string: expected `}`, found `.`
println!("{:_1.4", 3.14); //~ ERROR invalid format string: expected `}`, found `.`

println!("{ 0", 1.11); //~ ERROR invalid format string: expected `}`, found `0`
println!("{foo:1.4_1.4}", foo = 3.14); //~ ERROR invalid format string: expected `}`, found `.`
println!("{0:1.4_1.4}", 3.14); //~ ERROR invalid format string: expected `}`, found `.`
}
92 changes: 92 additions & 0 deletions tests/ui/parser/issues/invalid-parse-format-issue-139104.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
error: invalid format string: expected `}`, found `.`
--> $DIR/invalid-parse-format-issue-139104.rs:2:22
|
LL | println!("{foo:_1.4}", foo = 3.14);
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`

error: invalid format string: expected `}`, found `.`
--> $DIR/invalid-parse-format-issue-139104.rs:3:20
|
LL | println!("{0:_1.4}", 1.11);
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`

error: invalid format string: expected `}`, found `.`
--> $DIR/invalid-parse-format-issue-139104.rs:4:19
|
LL | println!("{:_1.4}", 3.14);
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`

error: invalid format string: expected `}`, found `.`
--> $DIR/invalid-parse-format-issue-139104.rs:6:22
|
LL | println!("{foo:_1.4", foo = 3.14);
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`

error: invalid format string: expected `}`, found `.`
--> $DIR/invalid-parse-format-issue-139104.rs:7:20
|
LL | println!("{0:_1.4", 1.11);
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`

error: invalid format string: expected `}`, found `.`
--> $DIR/invalid-parse-format-issue-139104.rs:8:19
|
LL | println!("{:_1.4", 3.14);
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`

error: invalid format string: expected `}`, found `0`
--> $DIR/invalid-parse-format-issue-139104.rs:10:18
|
LL | println!("{ 0", 1.11);
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`

error: invalid format string: expected `}`, found `.`
--> $DIR/invalid-parse-format-issue-139104.rs:11:25
|
LL | println!("{foo:1.4_1.4}", foo = 3.14);
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`

error: invalid format string: expected `}`, found `.`
--> $DIR/invalid-parse-format-issue-139104.rs:12:23
|
LL | println!("{0:1.4_1.4}", 3.14);
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`

error: aborting due to 9 previous errors

Loading