Skip to content

Commit 1b193de

Browse files
committed
Auto merge of #47232 - keatinge:master, r=petrochenkov
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 ```
2 parents 795594c + 6aafdc3 commit 1b193de

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/libsyntax/parse/parser.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -3431,8 +3431,20 @@ impl<'a> Parser<'a> {
34313431
let mut pats = Vec::new();
34323432
loop {
34333433
pats.push(self.parse_pat()?);
3434-
if self.check(&token::BinOp(token::Or)) { self.bump();}
3435-
else { return Ok(pats); }
3434+
3435+
if self.token == token::OrOr {
3436+
let mut err = self.struct_span_err(self.span,
3437+
"unexpected token `||` after pattern");
3438+
err.span_suggestion(self.span,
3439+
"use a single `|` to specify multiple patterns",
3440+
"|".to_owned());
3441+
err.emit();
3442+
self.bump();
3443+
} else if self.check(&token::BinOp(token::Or)) {
3444+
self.bump();
3445+
} else {
3446+
return Ok(pats);
3447+
}
34363448
};
34373449
}
34383450

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let x = 3;
13+
match x {
14+
1 | 2 || 3 => (), //~ ERROR unexpected token `||` after pattern
15+
_ => (),
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected token `||` after pattern
2+
--> $DIR/multiple-pattern-typo.rs:14:15
3+
|
4+
14 | 1 | 2 || 3 => (), //~ ERROR unexpected token `||` after pattern
5+
| ^^ help: use a single `|` to specify multiple patterns: `|`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)