@@ -80,7 +80,8 @@ enum EatOrResult {
8080}
8181
8282/// The syntax location of a given pattern. Used for diagnostics.
83- pub ( super ) enum PatternLocation {
83+ #[ derive( Clone , Copy ) ]
84+ pub enum PatternLocation {
8485 LetBinding ,
8586 FunctionParameter ,
8687}
@@ -91,8 +92,12 @@ impl<'a> Parser<'a> {
9192 /// Corresponds to `pat<no_top_alt>` in RFC 2535 and does not admit or-patterns
9293 /// at the top level. Used when parsing the parameters of lambda expressions,
9394 /// 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)
96101 }
97102
98103 /// Parses a pattern.
@@ -110,7 +115,7 @@ impl<'a> Parser<'a> {
110115 ra : RecoverColon ,
111116 rt : CommaRecoveryMode ,
112117 ) -> 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)
114119 }
115120
116121 /// Returns the pattern and a bool indicating whether we recovered from a trailing vert (true =
@@ -121,6 +126,7 @@ impl<'a> Parser<'a> {
121126 rc : RecoverComma ,
122127 ra : RecoverColon ,
123128 rt : CommaRecoveryMode ,
129+ syntax_loc : Option < PatternLocation > ,
124130 ) -> PResult < ' a , ( P < Pat > , bool ) > {
125131 // Keep track of whether we recovered from a trailing vert so that we can avoid duplicated
126132 // suggestions (which bothers rustfix).
@@ -133,7 +139,7 @@ impl<'a> Parser<'a> {
133139 } ;
134140
135141 // 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 ( ) ) ?;
137143 if rc == RecoverComma :: Yes {
138144 self . maybe_recover_unexpected_comma ( first_pat. span , rt) ?;
139145 }
@@ -172,7 +178,7 @@ impl<'a> Parser<'a> {
172178 break ;
173179 }
174180 }
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| {
176182 err. span_label ( lo, WHILE_PARSING_OR_MSG ) ;
177183 err
178184 } ) ?;
@@ -208,6 +214,7 @@ impl<'a> Parser<'a> {
208214 rc,
209215 RecoverColon :: No ,
210216 CommaRecoveryMode :: LikelyTuple ,
217+ Some ( syntax_loc) ,
211218 ) ?;
212219 let colon = self . eat ( & token:: Colon ) ;
213220
@@ -319,6 +326,7 @@ impl<'a> Parser<'a> {
319326 & mut self ,
320327 allow_range_pat : bool ,
321328 expected : Option < Expected > ,
329+ syntax_loc : Option < PatternLocation > ,
322330 ) -> PResult < ' a , P < Pat > > {
323331 maybe_recover_from_interpolated_ty_qpath ! ( self , true ) ;
324332 maybe_whole ! ( self , NtPat , |x| x) ;
@@ -393,7 +401,7 @@ impl<'a> Parser<'a> {
393401 ( Some ( qself) , path)
394402 } else {
395403 // Parse an unqualified path
396- ( None , self . parse_path ( PathStyle :: Pat ) ?)
404+ ( None , self . parse_path ( PathStyle :: Pat , syntax_loc ) ?)
397405 } ;
398406 let span = lo. to ( self . prev_token . span ) ;
399407
@@ -485,7 +493,7 @@ impl<'a> Parser<'a> {
485493
486494 // At this point we attempt to parse `@ $pat_rhs` and emit an error.
487495 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 ) ?;
489497 let whole_span = lhs. span . to ( rhs. span ) ;
490498
491499 if let PatKind :: Ident ( _, _, sub @ None ) = & mut rhs. kind {
@@ -541,7 +549,7 @@ impl<'a> Parser<'a> {
541549 }
542550
543551 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 ) ?;
545553 Ok ( PatKind :: Ref ( subpat, mutbl) )
546554 }
547555
@@ -584,7 +592,7 @@ impl<'a> Parser<'a> {
584592 }
585593
586594 // 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 ) ?;
588596
589597 // If we don't have `mut $ident (@ pat)?`, error.
590598 if let PatKind :: Ident ( BindingAnnotation ( ByRef :: No , m @ Mutability :: Not ) , ..) = & mut pat. kind
@@ -776,7 +784,7 @@ impl<'a> Parser<'a> {
776784 ( Some ( qself) , path)
777785 } else {
778786 // Parse an unqualified path
779- ( None , self . parse_path ( PathStyle :: Pat ) ?)
787+ ( None , self . parse_path ( PathStyle :: Pat , None ) ?)
780788 } ;
781789 let hi = self . prev_token . span ;
782790 Ok ( self . mk_expr ( lo. to ( hi) , ExprKind :: Path ( qself, path) ) )
@@ -814,7 +822,7 @@ impl<'a> Parser<'a> {
814822 fn parse_pat_ident ( & mut self , binding_annotation : BindingAnnotation ) -> PResult < ' a , PatKind > {
815823 let ident = self . parse_ident ( ) ?;
816824 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 ) ?)
818826 } else {
819827 None
820828 } ;
@@ -903,14 +911,14 @@ impl<'a> Parser<'a> {
903911 // We cannot use `parse_pat_ident()` since it will complain `box`
904912 // is not an identifier.
905913 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 ) ?)
907915 } else {
908916 None
909917 } ;
910918
911919 Ok ( PatKind :: Ident ( BindingAnnotation :: NONE , Ident :: new ( kw:: Box , box_span) , sub) )
912920 } else {
913- let pat = self . parse_pat_with_range_pat ( false , None ) ?;
921+ let pat = self . parse_pat_with_range_pat ( false , None , None ) ?;
914922 self . sess . gated_spans . gate ( sym:: box_patterns, box_span. to ( self . prev_token . span ) ) ;
915923 Ok ( PatKind :: Box ( pat) )
916924 }
0 commit comments