diff --git a/compiler/rustc_parse_format/src/lib.rs b/compiler/rustc_parse_format/src/lib.rs index 143fd0c4436cb..490af1257760d 100644 --- a/compiler/rustc_parse_format/src/lib.rs +++ b/compiler/rustc_parse_format/src/lib.rs @@ -461,6 +461,7 @@ impl<'input> Parser<'input> { ('?', '}') => self.missing_colon_before_debug_formatter(), ('?', _) => self.suggest_format_debug(), ('<' | '^' | '>', _) => self.suggest_format_align(c), + (',', _) => self.suggest_unsupported_python_numeric_grouping(), _ => self.suggest_positional_arg_instead_of_captured_arg(arg), } } @@ -934,6 +935,27 @@ impl<'input> Parser<'input> { } } } + + fn suggest_unsupported_python_numeric_grouping(&mut self) { + if let Some((range, _)) = self.consume_pos(',') { + self.errors.insert( + 0, + ParseError { + description: + "python's numeric grouping `,` is not supported in rust format strings" + .to_owned(), + note: Some(format!("to print `{{`, you can escape it using `{{{{`",)), + label: "expected `}`".to_owned(), + span: range, + secondary_label: self + .last_open_brace + .clone() + .map(|sp| ("because of this opening brace".to_owned(), sp)), + suggestion: Suggestion::None, + }, + ); + } + } } // Assert a reasonable size for `Piece` diff --git a/tests/ui/fmt/format-string-error-2.rs b/tests/ui/fmt/format-string-error-2.rs index e14a4064aa833..63d65023eb78b 100644 --- a/tests/ui/fmt/format-string-error-2.rs +++ b/tests/ui/fmt/format-string-error-2.rs @@ -86,4 +86,6 @@ raw { \n println!("{x?}, world!",); //~^ ERROR invalid format string: expected `}`, found `?` + println!("{x,}, world!",); + //~^ ERROR invalid format string: python's numeric grouping `,` is not supported in rust format strings } diff --git a/tests/ui/fmt/format-string-error-2.stderr b/tests/ui/fmt/format-string-error-2.stderr index b117fb57cf07e..e7fbc2e81fc6b 100644 --- a/tests/ui/fmt/format-string-error-2.stderr +++ b/tests/ui/fmt/format-string-error-2.stderr @@ -188,5 +188,15 @@ LL | println!("{x?}, world!",); | = note: to print `{`, you can escape it using `{{` -error: aborting due to 19 previous errors +error: invalid format string: python's numeric grouping `,` is not supported in rust format strings + --> $DIR/format-string-error-2.rs:89:17 + | +LL | println!("{x,}, world!",); + | - ^ expected `}` in format string + | | + | because of this opening brace + | + = note: to print `{`, you can escape it using `{{` + +error: aborting due to 20 previous errors