@@ -80,7 +80,8 @@ enum EatOrResult {
80
80
}
81
81
82
82
/// The syntax location of a given pattern. Used for diagnostics.
83
- pub ( super ) enum PatternLocation {
83
+ #[ derive( Clone , Copy ) ]
84
+ pub enum PatternLocation {
84
85
LetBinding ,
85
86
FunctionParameter ,
86
87
}
@@ -91,8 +92,12 @@ impl<'a> Parser<'a> {
91
92
/// Corresponds to `pat<no_top_alt>` in RFC 2535 and does not admit or-patterns
92
93
/// at the top level. Used when parsing the parameters of lambda expressions,
93
94
/// functions, function pointers, and `pat` macro fragments.
94
- pub fn parse_pat_no_top_alt ( & mut self , expected : Option < Expected > ) -> PResult < ' a , P < Pat > > {
95
- self . parse_pat_with_range_pat ( true , expected)
95
+ pub fn parse_pat_no_top_alt (
96
+ & mut self ,
97
+ expected : Option < Expected > ,
98
+ syntax_loc : Option < PatternLocation > ,
99
+ ) -> PResult < ' a , P < Pat > > {
100
+ self . parse_pat_with_range_pat ( true , expected, syntax_loc)
96
101
}
97
102
98
103
/// Parses a pattern.
@@ -110,7 +115,7 @@ impl<'a> Parser<'a> {
110
115
ra : RecoverColon ,
111
116
rt : CommaRecoveryMode ,
112
117
) -> PResult < ' a , P < Pat > > {
113
- self . parse_pat_allow_top_alt_inner ( expected, rc, ra, rt) . map ( |( pat, _) | pat)
118
+ self . parse_pat_allow_top_alt_inner ( expected, rc, ra, rt, None ) . map ( |( pat, _) | pat)
114
119
}
115
120
116
121
/// Returns the pattern and a bool indicating whether we recovered from a trailing vert (true =
@@ -121,6 +126,7 @@ impl<'a> Parser<'a> {
121
126
rc : RecoverComma ,
122
127
ra : RecoverColon ,
123
128
rt : CommaRecoveryMode ,
129
+ syntax_loc : Option < PatternLocation > ,
124
130
) -> PResult < ' a , ( P < Pat > , bool ) > {
125
131
// Keep track of whether we recovered from a trailing vert so that we can avoid duplicated
126
132
// suggestions (which bothers rustfix).
@@ -133,7 +139,7 @@ impl<'a> Parser<'a> {
133
139
} ;
134
140
135
141
// Parse the first pattern (`p_0`).
136
- let mut first_pat = self . parse_pat_no_top_alt ( expected) ?;
142
+ let mut first_pat = self . parse_pat_no_top_alt ( expected, syntax_loc . clone ( ) ) ?;
137
143
if rc == RecoverComma :: Yes {
138
144
self . maybe_recover_unexpected_comma ( first_pat. span , rt) ?;
139
145
}
@@ -172,7 +178,7 @@ impl<'a> Parser<'a> {
172
178
break ;
173
179
}
174
180
}
175
- let pat = self . parse_pat_no_top_alt ( expected) . map_err ( |mut err| {
181
+ let pat = self . parse_pat_no_top_alt ( expected, syntax_loc ) . map_err ( |mut err| {
176
182
err. span_label ( lo, WHILE_PARSING_OR_MSG ) ;
177
183
err
178
184
} ) ?;
@@ -208,6 +214,7 @@ impl<'a> Parser<'a> {
208
214
rc,
209
215
RecoverColon :: No ,
210
216
CommaRecoveryMode :: LikelyTuple ,
217
+ Some ( syntax_loc) ,
211
218
) ?;
212
219
let colon = self . eat ( & token:: Colon ) ;
213
220
@@ -319,6 +326,7 @@ impl<'a> Parser<'a> {
319
326
& mut self ,
320
327
allow_range_pat : bool ,
321
328
expected : Option < Expected > ,
329
+ syntax_loc : Option < PatternLocation > ,
322
330
) -> PResult < ' a , P < Pat > > {
323
331
maybe_recover_from_interpolated_ty_qpath ! ( self , true ) ;
324
332
maybe_whole ! ( self , NtPat , |x| x) ;
@@ -393,7 +401,7 @@ impl<'a> Parser<'a> {
393
401
( Some ( qself) , path)
394
402
} else {
395
403
// Parse an unqualified path
396
- ( None , self . parse_path ( PathStyle :: Pat ) ?)
404
+ ( None , self . parse_path ( PathStyle :: Pat , syntax_loc ) ?)
397
405
} ;
398
406
let span = lo. to ( self . prev_token . span ) ;
399
407
@@ -485,7 +493,7 @@ impl<'a> Parser<'a> {
485
493
486
494
// At this point we attempt to parse `@ $pat_rhs` and emit an error.
487
495
self . bump ( ) ; // `@`
488
- let mut rhs = self . parse_pat_no_top_alt ( None ) ?;
496
+ let mut rhs = self . parse_pat_no_top_alt ( None , None ) ?;
489
497
let whole_span = lhs. span . to ( rhs. span ) ;
490
498
491
499
if let PatKind :: Ident ( _, _, sub @ None ) = & mut rhs. kind {
@@ -541,7 +549,7 @@ impl<'a> Parser<'a> {
541
549
}
542
550
543
551
let mutbl = self . parse_mutability ( ) ;
544
- let subpat = self . parse_pat_with_range_pat ( false , expected) ?;
552
+ let subpat = self . parse_pat_with_range_pat ( false , expected, None ) ?;
545
553
Ok ( PatKind :: Ref ( subpat, mutbl) )
546
554
}
547
555
@@ -584,7 +592,7 @@ impl<'a> Parser<'a> {
584
592
}
585
593
586
594
// Parse the pattern we hope to be an identifier.
587
- let mut pat = self . parse_pat_no_top_alt ( Some ( Expected :: Identifier ) ) ?;
595
+ let mut pat = self . parse_pat_no_top_alt ( Some ( Expected :: Identifier ) , None ) ?;
588
596
589
597
// If we don't have `mut $ident (@ pat)?`, error.
590
598
if let PatKind :: Ident ( BindingAnnotation ( ByRef :: No , m @ Mutability :: Not ) , ..) = & mut pat. kind
@@ -776,7 +784,7 @@ impl<'a> Parser<'a> {
776
784
( Some ( qself) , path)
777
785
} else {
778
786
// Parse an unqualified path
779
- ( None , self . parse_path ( PathStyle :: Pat ) ?)
787
+ ( None , self . parse_path ( PathStyle :: Pat , None ) ?)
780
788
} ;
781
789
let hi = self . prev_token . span ;
782
790
Ok ( self . mk_expr ( lo. to ( hi) , ExprKind :: Path ( qself, path) ) )
@@ -814,7 +822,7 @@ impl<'a> Parser<'a> {
814
822
fn parse_pat_ident ( & mut self , binding_annotation : BindingAnnotation ) -> PResult < ' a , PatKind > {
815
823
let ident = self . parse_ident ( ) ?;
816
824
let sub = if self . eat ( & token:: At ) {
817
- Some ( self . parse_pat_no_top_alt ( Some ( Expected :: BindingPattern ) ) ?)
825
+ Some ( self . parse_pat_no_top_alt ( Some ( Expected :: BindingPattern ) , None ) ?)
818
826
} else {
819
827
None
820
828
} ;
@@ -903,14 +911,14 @@ impl<'a> Parser<'a> {
903
911
// We cannot use `parse_pat_ident()` since it will complain `box`
904
912
// is not an identifier.
905
913
let sub = if self . eat ( & token:: At ) {
906
- Some ( self . parse_pat_no_top_alt ( Some ( Expected :: BindingPattern ) ) ?)
914
+ Some ( self . parse_pat_no_top_alt ( Some ( Expected :: BindingPattern ) , None ) ?)
907
915
} else {
908
916
None
909
917
} ;
910
918
911
919
Ok ( PatKind :: Ident ( BindingAnnotation :: NONE , Ident :: new ( kw:: Box , box_span) , sub) )
912
920
} else {
913
- let pat = self . parse_pat_with_range_pat ( false , None ) ?;
921
+ let pat = self . parse_pat_with_range_pat ( false , None , None ) ?;
914
922
self . sess . gated_spans . gate ( sym:: box_patterns, box_span. to ( self . prev_token . span ) ) ;
915
923
Ok ( PatKind :: Box ( pat) )
916
924
}
0 commit comments