-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add help message for incorrect pattern syntax #47232
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @arielb1 (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
src/libsyntax/parse/parser.rs
Outdated
else { | ||
// Accidental use of || instead of | inbetween patterns | ||
if self.token == token::OrOr { | ||
return Err(self.span_fatal_help( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an instance of "confusable tokens" pattern, so we can easily do recovery instead of returning Err
.
See #46763 for the example of how to do it, except that we have |
and ||
instead of ..
and ...
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I just need to emit the error instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, emit an error (non-fatal one) and continue parsing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you take a look now?
This also needs a test (#46763 has an example of this as well). |
src/libsyntax/parse/parser.rs
Outdated
if self.token == token::OrOr { | ||
self.span_err_help(self.span, | ||
"unexpected token `||` after pattern", | ||
"did you mean to use `|` to specify multiple patterns?"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we have the relevant spans and know what code to suggest, the .span_suggestion
method is arguably preferable (compact, attractive output; usable by tooling cc #42823).
let mut err = self.struct_span_err(self.span, "unexpected token `||` after pattern");
err.span_suggestion(self.span, "to specify multiple patterns, use `|`", "|".to_owned());
err.emit();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope it's okay that I switched up the sentence a little bit because the exact text you have looks confusing when it's actually printed to specify multiple patterns use '|': '|'
@bors r+ |
📌 Commit 6aafdc3 has been approved by |
Add help message for incorrect pattern syntax When I was getting started with rust I often made the mistake of using `||` instead of `|` to match multiple patterns and spent a long time staring at my code wondering what was wrong. for example: ``` fn main() { let x = 1; match x { 1 || 2 => println!("1 or 2"), _ => println!("Something else"), } } ``` If you compile this with current rustc you will see ``` error: expected one of `...`, `..=`, `..`, `=>`, `if`, or `|`, found `||` --> test.rs:5:11 | 5 | 1 || 2 => println!("1 or 2"), | -^^ unexpected token | | | expected one of `...`, `..=`, `..`, `=>`, `if`, or `|` here error: aborting due to previous error ``` With my proposed change it will show: ``` error: unexpected token `||` after pattern --> test.rs:5:11 | 5 | 1 || 2 => println!("1 or 2"), | ^^ | = help: did you mean to use `|` to specify multiple patterns instead? error: aborting due to previous error ```
☀️ Test successful - status-appveyor, status-travis |
When I was getting started with rust I often made the mistake of using
||
instead of|
to match multiple patterns and spent a long time staring at my code wondering what was wrong.for example:
If you compile this with current rustc you will see
With my proposed change it will show: