@@ -18,7 +18,7 @@ pub(super) const PARAM_EXPECTED: Expected = Some("parameter name");
1818const WHILE_PARSING_OR_MSG : & str = "while parsing this or-pattern starting here" ;
1919
2020/// Whether or not an or-pattern should be gated when occurring in the current context.
21- #[ derive( PartialEq ) ]
21+ #[ derive( PartialEq , Clone , Copy ) ]
2222pub ( super ) enum GateOr {
2323 Yes ,
2424 No ,
@@ -94,7 +94,7 @@ impl<'a> Parser<'a> {
9494 ) -> PResult < ' a , P < Pat > > {
9595 // Parse the first pattern (`p_0`).
9696 let first_pat = self . parse_pat ( expected) ?;
97- self . maybe_recover_unexpected_comma ( first_pat. span , rc) ?;
97+ self . maybe_recover_unexpected_comma ( first_pat. span , rc, gate_or ) ?;
9898
9999 // If the next token is not a `|`,
100100 // this is not an or-pattern and we should exit here.
@@ -110,7 +110,7 @@ impl<'a> Parser<'a> {
110110 err. span_label ( lo, WHILE_PARSING_OR_MSG ) ;
111111 err
112112 } ) ?;
113- self . maybe_recover_unexpected_comma ( pat. span , rc) ?;
113+ self . maybe_recover_unexpected_comma ( pat. span , rc, gate_or ) ?;
114114 pats. push ( pat) ;
115115 }
116116 let or_pattern_span = lo. to ( self . prev_token . span ) ;
@@ -190,7 +190,12 @@ impl<'a> Parser<'a> {
190190
191191 /// Some special error handling for the "top-level" patterns in a match arm,
192192 /// `for` loop, `let`, &c. (in contrast to subpatterns within such).
193- fn maybe_recover_unexpected_comma ( & mut self , lo : Span , rc : RecoverComma ) -> PResult < ' a , ( ) > {
193+ fn maybe_recover_unexpected_comma (
194+ & mut self ,
195+ lo : Span ,
196+ rc : RecoverComma ,
197+ gate_or : GateOr ,
198+ ) -> PResult < ' a , ( ) > {
194199 if rc == RecoverComma :: No || self . token != token:: Comma {
195200 return Ok ( ( ) ) ;
196201 }
@@ -209,18 +214,24 @@ impl<'a> Parser<'a> {
209214 let seq_span = lo. to ( self . prev_token . span ) ;
210215 let mut err = self . struct_span_err ( comma_span, "unexpected `,` in pattern" ) ;
211216 if let Ok ( seq_snippet) = self . span_to_snippet ( seq_span) {
217+ const MSG : & str = "try adding parentheses to match on a tuple..." ;
218+
219+ let or_suggestion =
220+ gate_or == GateOr :: No || !self . sess . gated_spans . is_ungated ( sym:: or_patterns) ;
212221 err. span_suggestion (
213222 seq_span,
214- "try adding parentheses to match on a tuple..." ,
223+ if or_suggestion { MSG } else { MSG . trim_end_matches ( '.' ) } ,
215224 format ! ( "({})" , seq_snippet) ,
216225 Applicability :: MachineApplicable ,
217- )
218- . span_suggestion (
219- seq_span,
220- "...or a vertical bar to match on multiple alternatives" ,
221- seq_snippet. replace ( "," , " |" ) ,
222- Applicability :: MachineApplicable ,
223226 ) ;
227+ if or_suggestion {
228+ err. span_suggestion (
229+ seq_span,
230+ "...or a vertical bar to match on multiple alternatives" ,
231+ seq_snippet. replace ( "," , " |" ) ,
232+ Applicability :: MachineApplicable ,
233+ ) ;
234+ }
224235 }
225236 Err ( err)
226237 }
0 commit comments