-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #75779 - scileo:fix-issue-75492, r=petrochenkov
Improve error message when typo is made in format! The expansion of the format! built-in macro is roughly done in two steps: - the format expression is parsed, the arguments are parsed, - the format expression is checked to be a string literal, code is expanded. The problem is that the expression parser can eat too much tokens, which invalidates the parsing of the next format arguments. As the format expression check happens next, the error emitted concerns the format arguments, whereas the problem is about the format expression. This PR contains two commits. The first one actually checks that the formatting expression is a string literal before raising any error about the formatting arguments, and the second one contains some simple heuristics which allow to suggest, when the format expression is followed by a dot instead of a comma, to suggest to replace the dot with a comma. This pull request should fix #75492. Note: this is my first non-doc contribution to the rust ecosystem. Feel free to make any comment about my code, or whatever. I'll be very happy to fix it :)
- Loading branch information
Showing
4 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Allows to track issue #75492: | ||
// https://github.com/rust-lang/rust/issues/75492 | ||
|
||
use std::iter; | ||
|
||
fn main() { | ||
format!("A number: {}". iter::once(42).next().unwrap()); | ||
//~^ ERROR expected token: `,` | ||
|
||
// Other kind of types are also checked: | ||
|
||
format!("A number: {}" / iter::once(42).next().unwrap()); | ||
//~^ ERROR expected token: `,` | ||
|
||
format!("A number: {}"; iter::once(42).next().unwrap()); | ||
//~^ ERROR expected token: `,` | ||
|
||
// Note: this character is an COMBINING COMMA BELOW unicode char | ||
format!("A number: {}" ̦ iter::once(42).next().unwrap()); | ||
//~^ ERROR expected token: `,` | ||
//~^^ ERROR unknown start of token: \u{326} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
error: unknown start of token: \u{326} | ||
--> $DIR/incorrect-first-separator.rs:19:28 | ||
| | ||
LL | format!("A number: {}" ̦ iter::once(42).next().unwrap()); | ||
| ^ | ||
|
||
error: expected token: `,` | ||
--> $DIR/incorrect-first-separator.rs:7:27 | ||
| | ||
LL | format!("A number: {}". iter::once(42).next().unwrap()); | ||
| ^ expected `,` | ||
|
||
error: expected token: `,` | ||
--> $DIR/incorrect-first-separator.rs:12:28 | ||
| | ||
LL | format!("A number: {}" / iter::once(42).next().unwrap()); | ||
| ^ expected `,` | ||
|
||
error: expected token: `,` | ||
--> $DIR/incorrect-first-separator.rs:15:27 | ||
| | ||
LL | format!("A number: {}"; iter::once(42).next().unwrap()); | ||
| ^ expected `,` | ||
|
||
error: expected token: `,` | ||
--> $DIR/incorrect-first-separator.rs:19:30 | ||
| | ||
LL | format!("A number: {}" ̦ iter::once(42).next().unwrap()); | ||
| ^^^^ expected `,` | ||
|
||
error: aborting due to 5 previous errors | ||
|