Skip to content

Commit cfa2921

Browse files
committed
Avoid unnecessary pattern parse errors on ref box
1 parent afbba42 commit cfa2921

File tree

4 files changed

+5
-27
lines changed

4 files changed

+5
-27
lines changed

compiler/rustc_parse/src/parser/pat.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,10 @@ impl<'a> Parser<'a> {
391391
self.parse_pat_ident_mut(syntax_loc)?
392392
} else if self.eat_keyword(kw::Ref) {
393393
if self.check_keyword(kw::Box) {
394-
// Suggest `box ref` and quit parsing pattern to prevent series of
395-
// misguided diagnostics from later stages of the compiler.
394+
// Suggest `box ref`.
396395
let span = self.prev_token.span.to(self.token.span);
397-
return Err(self.sess.create_err(SwitchRefBoxOrder { span }));
396+
self.bump();
397+
self.sess.emit_err(SwitchRefBoxOrder { span });
398398
}
399399
// Parse ref ident @ pat / ref mut ident @ pat
400400
let mutbl = self.parse_mutability();

tests/ui/pattern/pattern-bad-ref-box-order.fixed

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
fn foo(f: Option<Box<i32>>) {
77
match f {
8-
Some(box ref, _i) => {},
8+
Some(box ref _i) => {},
99
//~^ ERROR switch the order of `ref` and `box`
10-
//~| ERROR expected one of `)`, `,`, or `|`, found `_i`
11-
//~| ERROR this pattern has 2 fields, but the corresponding tuple variant has 1 field
1210
None => {}
1311
}
1412
}

tests/ui/pattern/pattern-bad-ref-box-order.rs

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ fn foo(f: Option<Box<i32>>) {
77
match f {
88
Some(ref box _i) => {},
99
//~^ ERROR switch the order of `ref` and `box`
10-
//~| ERROR expected one of `)`, `,`, or `|`, found `_i`
11-
//~| ERROR this pattern has 2 fields, but the corresponding tuple variant has 1 field
1210
None => {}
1311
}
1412
}

tests/ui/pattern/pattern-bad-ref-box-order.stderr

+1-19
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,5 @@ error: switch the order of `ref` and `box`
44
LL | Some(ref box _i) => {},
55
| ^^^^^^^ help: swap them: `box ref`
66

7-
error: expected one of `)`, `,`, or `|`, found `_i`
8-
--> $DIR/pattern-bad-ref-box-order.rs:8:22
9-
|
10-
LL | Some(ref box _i) => {},
11-
| -^^ expected one of `)`, `,`, or `|`
12-
| |
13-
| help: missing `,`
14-
15-
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
16-
--> $DIR/pattern-bad-ref-box-order.rs:8:22
17-
|
18-
LL | Some(ref box _i) => {},
19-
| ^^ expected 1 field, found 2
20-
--> $SRC_DIR/core/src/option.rs:LL:COL
21-
|
22-
= note: tuple variant has 1 field
23-
24-
error: aborting due to 3 previous errors
7+
error: aborting due to 1 previous error
258

26-
For more information about this error, try `rustc --explain E0023`.

0 commit comments

Comments
 (0)