Skip to content

Commit b452230

Browse files
authored
Unrolled build for #150941
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.
2 parents 1377169 + 76ad528 commit b452230

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

compiler/rustc_parse_format/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ impl<'input> Parser<'input> {
461461
('?', '}') => self.missing_colon_before_debug_formatter(),
462462
('?', _) => self.suggest_format_debug(),
463463
('<' | '^' | '>', _) => self.suggest_format_align(c),
464+
(',', _) => self.suggest_unsupported_python_numeric_grouping(),
464465
_ => self.suggest_positional_arg_instead_of_captured_arg(arg),
465466
}
466467
}
@@ -934,6 +935,27 @@ impl<'input> Parser<'input> {
934935
}
935936
}
936937
}
938+
939+
fn suggest_unsupported_python_numeric_grouping(&mut self) {
940+
if let Some((range, _)) = self.consume_pos(',') {
941+
self.errors.insert(
942+
0,
943+
ParseError {
944+
description:
945+
"python's numeric grouping `,` is not supported in rust format strings"
946+
.to_owned(),
947+
note: Some(format!("to print `{{`, you can escape it using `{{{{`",)),
948+
label: "expected `}`".to_owned(),
949+
span: range,
950+
secondary_label: self
951+
.last_open_brace
952+
.clone()
953+
.map(|sp| ("because of this opening brace".to_owned(), sp)),
954+
suggestion: Suggestion::None,
955+
},
956+
);
957+
}
958+
}
937959
}
938960

939961
// Assert a reasonable size for `Piece`

tests/ui/fmt/format-string-error-2.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,6 @@ raw { \n
8686

8787
println!("{x?}, world!",);
8888
//~^ ERROR invalid format string: expected `}`, found `?`
89+
println!("{x,}, world!",);
90+
//~^ ERROR invalid format string: python's numeric grouping `,` is not supported in rust format strings
8991
}

tests/ui/fmt/format-string-error-2.stderr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,15 @@ LL | println!("{x?}, world!",);
188188
|
189189
= note: to print `{`, you can escape it using `{{`
190190

191-
error: aborting due to 19 previous errors
191+
error: invalid format string: python's numeric grouping `,` is not supported in rust format strings
192+
--> $DIR/format-string-error-2.rs:89:17
193+
|
194+
LL | println!("{x,}, world!",);
195+
| - ^ expected `}` in format string
196+
| |
197+
| because of this opening brace
198+
|
199+
= note: to print `{`, you can escape it using `{{`
200+
201+
error: aborting due to 20 previous errors
192202

0 commit comments

Comments
 (0)