From 88453aaccfe308a30a30dc4dee95fe51edc367e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 29 Nov 2023 18:45:36 +0000 Subject: [PATCH] Avoid unnecessary pattern parse errors on `ref box` --- compiler/rustc_parse/src/parser/pat.rs | 6 +++--- .../pattern/pattern-bad-ref-box-order.fixed | 4 +--- tests/ui/pattern/pattern-bad-ref-box-order.rs | 2 -- .../pattern/pattern-bad-ref-box-order.stderr | 20 +------------------ 4 files changed, 5 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index ec7c312d03f00..3d1d1ec8108c1 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -391,10 +391,10 @@ impl<'a> Parser<'a> { self.parse_pat_ident_mut(syntax_loc)? } else if self.eat_keyword(kw::Ref) { if self.check_keyword(kw::Box) { - // Suggest `box ref` and quit parsing pattern to prevent series of - // misguided diagnostics from later stages of the compiler. + // Suggest `box ref`. let span = self.prev_token.span.to(self.token.span); - return Err(self.sess.create_err(SwitchRefBoxOrder { span })); + self.bump(); + self.sess.emit_err(SwitchRefBoxOrder { span }); } // Parse ref ident @ pat / ref mut ident @ pat let mutbl = self.parse_mutability(); diff --git a/tests/ui/pattern/pattern-bad-ref-box-order.fixed b/tests/ui/pattern/pattern-bad-ref-box-order.fixed index e99ab449a6cfc..8825744a08b1d 100644 --- a/tests/ui/pattern/pattern-bad-ref-box-order.fixed +++ b/tests/ui/pattern/pattern-bad-ref-box-order.fixed @@ -5,10 +5,8 @@ fn foo(f: Option>) { match f { - Some(box ref, _i) => {}, + Some(box ref _i) => {}, //~^ ERROR switch the order of `ref` and `box` - //~| ERROR expected one of `)`, `,`, or `|`, found `_i` - //~| ERROR this pattern has 2 fields, but the corresponding tuple variant has 1 field None => {} } } diff --git a/tests/ui/pattern/pattern-bad-ref-box-order.rs b/tests/ui/pattern/pattern-bad-ref-box-order.rs index 3632bb438de0f..f3fcf0ceacf0b 100644 --- a/tests/ui/pattern/pattern-bad-ref-box-order.rs +++ b/tests/ui/pattern/pattern-bad-ref-box-order.rs @@ -7,8 +7,6 @@ fn foo(f: Option>) { match f { Some(ref box _i) => {}, //~^ ERROR switch the order of `ref` and `box` - //~| ERROR expected one of `)`, `,`, or `|`, found `_i` - //~| ERROR this pattern has 2 fields, but the corresponding tuple variant has 1 field None => {} } } diff --git a/tests/ui/pattern/pattern-bad-ref-box-order.stderr b/tests/ui/pattern/pattern-bad-ref-box-order.stderr index 0d3ab6394469c..a49f05c1028d8 100644 --- a/tests/ui/pattern/pattern-bad-ref-box-order.stderr +++ b/tests/ui/pattern/pattern-bad-ref-box-order.stderr @@ -4,23 +4,5 @@ error: switch the order of `ref` and `box` LL | Some(ref box _i) => {}, | ^^^^^^^ help: swap them: `box ref` -error: expected one of `)`, `,`, or `|`, found `_i` - --> $DIR/pattern-bad-ref-box-order.rs:8:22 - | -LL | Some(ref box _i) => {}, - | -^^ expected one of `)`, `,`, or `|` - | | - | help: missing `,` - -error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field - --> $DIR/pattern-bad-ref-box-order.rs:8:22 - | -LL | Some(ref box _i) => {}, - | ^^ expected 1 field, found 2 - --> $SRC_DIR/core/src/option.rs:LL:COL - | - = note: tuple variant has 1 field - -error: aborting due to 3 previous errors +error: aborting due to 1 previous error -For more information about this error, try `rustc --explain E0023`.