Skip to content

Add help message for incorrect pattern syntax #47232

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

Merged
merged 7 commits into from
Jan 8, 2018
Merged
Changes from 2 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
11 changes: 10 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3432,7 +3432,16 @@ impl<'a> Parser<'a> {
loop {
pats.push(self.parse_pat()?);
if self.check(&token::BinOp(token::Or)) { self.bump();}
else { return Ok(pats); }
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?

self.span, "unexpected token `||` after pattern",
"did you mean to use `|` to specify multiple patterns instead?"));
}

return Ok(pats);
}
};
}

Expand Down