-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error message when typo is made in format! #75779
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
5cf5197
to
0d16b39
Compare
I thought highfive had some heuristic based on who has touched the files in question. |
0d16b39
to
10ee451
Compare
So, this PR looks significantly more complicated than it needs to be. There are two orthogonal issues here:
I suggest addressing them independently starting from the first one.
|
Thanks for your feedback, I'm working on it :) Side question: should I open a new PR, or is it okay to modify this one? |
@scileo |
10ee451
to
d7fa594
Compare
@petrochenkov There are a few changes that may need to be discussed:
|
Yes, but it's better to do it in a separate PR later. |
Thanks for the clarification. My initial understandings about how to do it were wrong. I'll work on it once this PR is merged. |
Thanks! |
📌 Commit 1001c76c6c449e27d90409ff5cd8865d510f5341 has been approved by |
⌛ Testing commit 1001c76c6c449e27d90409ff5cd8865d510f5341 with merge 1a5c36a0e53086af131b47592bbaaea4cf254c62... |
This comment has been minimized.
This comment has been minimized.
⌛ Testing commit 1001c76c6c449e27d90409ff5cd8865d510f5341 with merge c57047b5fd90f5aaafb6028350ba44356c20f56f... |
This comment has been minimized.
This comment has been minimized.
@bors retry |
@bors r- |
Needs a rebase. |
Previous implementation used the `Parser::parse_expr` function in order to extract the format expression. If the first comma following the format expression was mistakenly replaced with a dot, then the next format expression was eaten by the function, because it looked as a syntactically valid expression, which resulted in incorrectly spanned error messages. The way the format expression is exctracted is changed: we first look at the first available token in the first argument supplied to the `format!` macro call. If it is a string literal, then it is promoted as a format expression immediatly, otherwise we fall back to the original `parse_expr`-related method. This allows us to ensure that the parser won't consume too much tokens when a typo is made. A test has been created so that it is ensured that the issue is properly fixed.
1001c76
to
f6d18db
Compare
@bors r+ |
📌 Commit f6d18db has been approved by |
Thank you very much for your help @petrochenkov! |
☀️ Test successful - checks-actions, checks-azure |
The expansion of the format! built-in macro is roughly done in two steps:
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 :)