Skip to content

Commit

Permalink
Extend format arg help for simple tuple index access expression
Browse files Browse the repository at this point in the history
  • Loading branch information
jieyouxu committed Mar 15, 2024
1 parent accc516 commit 2648798
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
16 changes: 14 additions & 2 deletions compiler/rustc_parse_format/src/lib.rs
Expand Up @@ -907,8 +907,8 @@ impl<'a> Parser<'a> {
let byte_pos = self.to_span_index(end);
let start = InnerOffset(byte_pos.0 + 1);
let field = self.argument(start);
// We can only parse `foo.bar` field access, any deeper nesting,
// or another type of expression, like method calls, are not supported
// We can only parse simple `foo.bar` field access or `foo.0` tuple index access, any
// deeper nesting, or another type of expression, like method calls, are not supported
if !self.consume('}') {
return;
}
Expand All @@ -925,6 +925,18 @@ impl<'a> Parser<'a> {
suggestion: Suggestion::UsePositional,
},
);
} else if let ArgumentIs(_) = field.position {
self.errors.insert(
0,
ParseError {
description: "tuple index access isn't supported".to_string(),
note: None,
label: "not supported".to_string(),
span: InnerSpan::new(arg.position_span.start, field.position_span.end),
secondary_label: None,
suggestion: Suggestion::UsePositional,
},
);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/fmt/format-args-non-identifier-diagnostics.fixed
@@ -0,0 +1,7 @@
//@ run-rustfix

fn main() {
let x = (1,);
println!("{0}", x.0);
//~^ ERROR invalid format string
}
7 changes: 7 additions & 0 deletions tests/ui/fmt/format-args-non-identifier-diagnostics.rs
@@ -0,0 +1,7 @@
//@ run-rustfix

fn main() {
let x = (1,);
println!("{x.0}");
//~^ ERROR invalid format string
}
13 changes: 13 additions & 0 deletions tests/ui/fmt/format-args-non-identifier-diagnostics.stderr
@@ -0,0 +1,13 @@
error: invalid format string: tuple index access isn't supported
--> $DIR/format-args-non-identifier-diagnostics.rs:5:16
|
LL | println!("{x.0}");
| ^^^ not supported in format string
|
help: consider using a positional formatting argument instead
|
LL | println!("{0}", x.0);
| ~ +++++

error: aborting due to 1 previous error

0 comments on commit 2648798

Please sign in to comment.