Skip to content

Commit 45e005d

Browse files
authored
Rollup merge of rust-lang#122556 - jieyouxu:non-identifier-format-arg, r=petrochenkov
Extend format arg help for simple tuple index access expression The help is only applicable for simple field access `a.b` and (with this PR) simple tuple index access expressions `a.0`. Closes rust-lang#122535.
2 parents 489c2e9 + 52a1125 commit 45e005d

4 files changed

+70
-15
lines changed

compiler/rustc_parse_format/src/lib.rs

+37-15
Original file line numberDiff line numberDiff line change
@@ -907,25 +907,47 @@ impl<'a> Parser<'a> {
907907
let byte_pos = self.to_span_index(end);
908908
let start = InnerOffset(byte_pos.0 + 1);
909909
let field = self.argument(start);
910-
// We can only parse `foo.bar` field access, any deeper nesting,
911-
// or another type of expression, like method calls, are not supported
910+
// We can only parse simple `foo.bar` field access or `foo.0` tuple index access, any
911+
// deeper nesting, or another type of expression, like method calls, are not supported
912912
if !self.consume('}') {
913913
return;
914914
}
915915
if let ArgumentNamed(_) = arg.position {
916-
if let ArgumentNamed(_) = field.position {
917-
self.errors.insert(
918-
0,
919-
ParseError {
920-
description: "field access isn't supported".to_string(),
921-
note: None,
922-
label: "not supported".to_string(),
923-
span: InnerSpan::new(arg.position_span.start, field.position_span.end),
924-
secondary_label: None,
925-
suggestion: Suggestion::UsePositional,
926-
},
927-
);
928-
}
916+
match field.position {
917+
ArgumentNamed(_) => {
918+
self.errors.insert(
919+
0,
920+
ParseError {
921+
description: "field access isn't supported".to_string(),
922+
note: None,
923+
label: "not supported".to_string(),
924+
span: InnerSpan::new(
925+
arg.position_span.start,
926+
field.position_span.end,
927+
),
928+
secondary_label: None,
929+
suggestion: Suggestion::UsePositional,
930+
},
931+
);
932+
}
933+
ArgumentIs(_) => {
934+
self.errors.insert(
935+
0,
936+
ParseError {
937+
description: "tuple index access isn't supported".to_string(),
938+
note: None,
939+
label: "not supported".to_string(),
940+
span: InnerSpan::new(
941+
arg.position_span.start,
942+
field.position_span.end,
943+
),
944+
secondary_label: None,
945+
suggestion: Suggestion::UsePositional,
946+
},
947+
);
948+
}
949+
_ => {}
950+
};
929951
}
930952
}
931953
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Checks that there is a suggestion for simple tuple index access expression (used where an
2+
// identifier is expected in a format arg) to use positional arg instead.
3+
// Issue: <https://github.com/rust-lang/rust/issues/122535>.
4+
//@ run-rustfix
5+
6+
fn main() {
7+
let x = (1,);
8+
println!("{0}", x.0);
9+
//~^ ERROR invalid format string
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Checks that there is a suggestion for simple tuple index access expression (used where an
2+
// identifier is expected in a format arg) to use positional arg instead.
3+
// Issue: <https://github.com/rust-lang/rust/issues/122535>.
4+
//@ run-rustfix
5+
6+
fn main() {
7+
let x = (1,);
8+
println!("{x.0}");
9+
//~^ ERROR invalid format string
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: invalid format string: tuple index access isn't supported
2+
--> $DIR/format-args-non-identifier-diagnostics.rs:8:16
3+
|
4+
LL | println!("{x.0}");
5+
| ^^^ not supported in format string
6+
|
7+
help: consider using a positional formatting argument instead
8+
|
9+
LL | println!("{0}", x.0);
10+
| ~ +++++
11+
12+
error: aborting due to 1 previous error
13+

0 commit comments

Comments
 (0)