@@ -18,7 +18,7 @@ pub(super) const PARAM_EXPECTED: Expected = Some("parameter name");
18
18
const WHILE_PARSING_OR_MSG : & str = "while parsing this or-pattern starting here" ;
19
19
20
20
/// Whether or not an or-pattern should be gated when occurring in the current context.
21
- #[ derive( PartialEq ) ]
21
+ #[ derive( PartialEq , Clone , Copy ) ]
22
22
pub ( super ) enum GateOr {
23
23
Yes ,
24
24
No ,
@@ -94,7 +94,7 @@ impl<'a> Parser<'a> {
94
94
) -> PResult < ' a , P < Pat > > {
95
95
// Parse the first pattern (`p_0`).
96
96
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 ) ?;
98
98
99
99
// If the next token is not a `|`,
100
100
// this is not an or-pattern and we should exit here.
@@ -110,7 +110,7 @@ impl<'a> Parser<'a> {
110
110
err. span_label ( lo, WHILE_PARSING_OR_MSG ) ;
111
111
err
112
112
} ) ?;
113
- self . maybe_recover_unexpected_comma ( pat. span , rc) ?;
113
+ self . maybe_recover_unexpected_comma ( pat. span , rc, gate_or ) ?;
114
114
pats. push ( pat) ;
115
115
}
116
116
let or_pattern_span = lo. to ( self . prev_token . span ) ;
@@ -190,7 +190,12 @@ impl<'a> Parser<'a> {
190
190
191
191
/// Some special error handling for the "top-level" patterns in a match arm,
192
192
/// `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 , ( ) > {
194
199
if rc == RecoverComma :: No || self . token != token:: Comma {
195
200
return Ok ( ( ) ) ;
196
201
}
@@ -209,18 +214,24 @@ impl<'a> Parser<'a> {
209
214
let seq_span = lo. to ( self . prev_token . span ) ;
210
215
let mut err = self . struct_span_err ( comma_span, "unexpected `,` in pattern" ) ;
211
216
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) ;
212
221
err. span_suggestion (
213
222
seq_span,
214
- "try adding parentheses to match on a tuple..." ,
223
+ if or_suggestion { MSG } else { MSG . trim_end_matches ( '.' ) } ,
215
224
format ! ( "({})" , seq_snippet) ,
216
225
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 ,
223
226
) ;
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
+ }
224
235
}
225
236
Err ( err)
226
237
}
0 commit comments