Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions compiler/rustc_parse_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -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`
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/fmt/format-string-error-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 11 additions & 1 deletion tests/ui/fmt/format-string-error-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Loading