Skip to content

Commit 2648798

Browse files
committed
Extend format arg help for simple tuple index access expression
1 parent accc516 commit 2648798

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

compiler/rustc_parse_format/src/lib.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,8 @@ 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
}
@@ -925,6 +925,18 @@ impl<'a> Parser<'a> {
925925
suggestion: Suggestion::UsePositional,
926926
},
927927
);
928+
} else if let ArgumentIs(_) = field.position {
929+
self.errors.insert(
930+
0,
931+
ParseError {
932+
description: "tuple index access isn't supported".to_string(),
933+
note: None,
934+
label: "not supported".to_string(),
935+
span: InnerSpan::new(arg.position_span.start, field.position_span.end),
936+
secondary_label: None,
937+
suggestion: Suggestion::UsePositional,
938+
},
939+
);
928940
}
929941
}
930942
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@ run-rustfix
2+
3+
fn main() {
4+
let x = (1,);
5+
println!("{0}", x.0);
6+
//~^ ERROR invalid format string
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@ run-rustfix
2+
3+
fn main() {
4+
let x = (1,);
5+
println!("{x.0}");
6+
//~^ ERROR invalid format string
7+
}
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:5: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)