Skip to content
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

Merged
merged 7 commits into from
Jan 8, 2018
Merged

Conversation

keatinge
Copy link
Contributor

@keatinge keatinge commented Jan 6, 2018

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

@rust-highfive
Copy link
Collaborator

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.

else {
// Accidental use of || instead of | inbetween patterns
if self.token == token::OrOr {
return Err(self.span_fatal_help(
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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?

@petrochenkov petrochenkov assigned petrochenkov and unassigned arielb1 Jan 6, 2018
@petrochenkov
Copy link
Contributor

This also needs a test (#46763 has an example of this as well).

if self.token == token::OrOr {
self.span_err_help(self.span,
"unexpected token `||` after pattern",
"did you mean to use `|` to specify multiple patterns?");
Copy link
Member

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();

Copy link
Contributor Author

@keatinge keatinge Jan 6, 2018

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 '|': '|'

@petrochenkov
Copy link
Contributor

@bors r+
Thanks!

@bors
Copy link
Contributor

bors commented Jan 6, 2018

📌 Commit 6aafdc3 has been approved by petrochenkov

@petrochenkov petrochenkov added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jan 6, 2018
@bors
Copy link
Contributor

bors commented Jan 8, 2018

⌛ Testing commit 6aafdc3 with merge 1b193de...

bors added a commit that referenced this pull request Jan 8, 2018
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
```
@bors
Copy link
Contributor

bors commented Jan 8, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: petrochenkov
Pushing 1b193de to master...

@bors bors merged commit 6aafdc3 into rust-lang:master Jan 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants