Rollup merge of #150941 - ua/numeric, r=workingjubilee

rustc_parse_format: improve diagnostics for unsupported python numeric grouping

Detect Python-style numeric grouping syntax in format strings (e.g. `{x:,}`)
and emit a clear diagnostic explaining that it is not supported in Rust.
This helps users coming from Python understand the error without exposing
the full set of valid Rust format specifiers.
This commit is contained in:
Matthias Krüger 2026-01-12 13:32:11 +01:00 committed by GitHub
commit 2caeb9ff59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 1 deletions

View file

@ -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`

View file

@ -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
}

View file

@ -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