From 264879867d48d20e7e4bc7f66565e7a3a2672d90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9D=B0=E5=8F=8B=20Jieyou=20Xu=20=28Joe=29?= Date: Fri, 15 Mar 2024 16:23:17 +0000 Subject: [PATCH] Extend format arg help for simple tuple index access expression --- compiler/rustc_parse_format/src/lib.rs | 16 ++++++++++++++-- .../format-args-non-identifier-diagnostics.fixed | 7 +++++++ .../format-args-non-identifier-diagnostics.rs | 7 +++++++ ...format-args-non-identifier-diagnostics.stderr | 13 +++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/ui/fmt/format-args-non-identifier-diagnostics.fixed create mode 100644 tests/ui/fmt/format-args-non-identifier-diagnostics.rs create mode 100644 tests/ui/fmt/format-args-non-identifier-diagnostics.stderr diff --git a/compiler/rustc_parse_format/src/lib.rs b/compiler/rustc_parse_format/src/lib.rs index 79aab47fbe717..6c51163d06af5 100644 --- a/compiler/rustc_parse_format/src/lib.rs +++ b/compiler/rustc_parse_format/src/lib.rs @@ -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; } @@ -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, + }, + ); } } } diff --git a/tests/ui/fmt/format-args-non-identifier-diagnostics.fixed b/tests/ui/fmt/format-args-non-identifier-diagnostics.fixed new file mode 100644 index 0000000000000..6fb89c480e0e1 --- /dev/null +++ b/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 +} diff --git a/tests/ui/fmt/format-args-non-identifier-diagnostics.rs b/tests/ui/fmt/format-args-non-identifier-diagnostics.rs new file mode 100644 index 0000000000000..d2eaa4d9de8d9 --- /dev/null +++ b/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 +} diff --git a/tests/ui/fmt/format-args-non-identifier-diagnostics.stderr b/tests/ui/fmt/format-args-non-identifier-diagnostics.stderr new file mode 100644 index 0000000000000..9fc6d27a7b29c --- /dev/null +++ b/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 +