@@ -1827,10 +1827,7 @@ impl<'a> Parser<'a> {
18271827 debug ! ( "parse_arg_general parse_pat (require_name:{})" ,
18281828 require_name) ;
18291829 self . eat_incorrect_doc_comment ( "method arguments" ) ;
1830- let pat = self . parse_pat ( ) . map_err ( |mut err| {
1831- err. span_label ( self . span , "expected argument name" ) ;
1832- err
1833- } ) ?;
1830+ let pat = self . parse_pat ( Some ( "argument name" ) ) ?;
18341831
18351832 if let Err ( mut err) = self . expect ( & token:: Colon ) {
18361833 // If we find a pattern followed by an identifier, it could be an (incorrect)
@@ -1879,7 +1876,7 @@ impl<'a> Parser<'a> {
18791876 // Recover from attempting to parse the argument as a type without pattern.
18801877 err. cancel ( ) ;
18811878 mem:: replace ( self , parser_snapshot_before_ty) ;
1882- let pat = self . parse_pat ( ) ?;
1879+ let pat = self . parse_pat ( Some ( "argument name" ) ) ?;
18831880 self . expect ( & token:: Colon ) ?;
18841881 let ty = self . parse_ty ( ) ?;
18851882
@@ -1917,7 +1914,7 @@ impl<'a> Parser<'a> {
19171914
19181915 /// Parse an argument in a lambda header e.g. |arg, arg|
19191916 fn parse_fn_block_arg ( & mut self ) -> PResult < ' a , Arg > {
1920- let pat = self . parse_pat ( ) ?;
1917+ let pat = self . parse_pat ( Some ( "argument name" ) ) ?;
19211918 let t = if self . eat ( & token:: Colon ) {
19221919 self . parse_ty ( ) ?
19231920 } else {
@@ -3784,7 +3781,7 @@ impl<'a> Parser<'a> {
37843781 "`..` can only be used once per tuple or tuple struct pattern" ) ;
37853782 }
37863783 } else if !self . check ( & token:: CloseDelim ( token:: Paren ) ) {
3787- fields. push ( self . parse_pat ( ) ?) ;
3784+ fields. push ( self . parse_pat ( None ) ?) ;
37883785 } else {
37893786 break
37903787 }
@@ -3840,7 +3837,7 @@ impl<'a> Parser<'a> {
38403837 }
38413838 }
38423839
3843- let subpat = self . parse_pat ( ) ?;
3840+ let subpat = self . parse_pat ( None ) ?;
38443841 if before_slice && self . eat ( & token:: DotDot ) {
38453842 slice = Some ( subpat) ;
38463843 before_slice = false ;
@@ -3865,7 +3862,7 @@ impl<'a> Parser<'a> {
38653862 // Parsing a pattern of the form "fieldname: pat"
38663863 let fieldname = self . parse_field_name ( ) ?;
38673864 self . bump ( ) ;
3868- let pat = self . parse_pat ( ) ?;
3865+ let pat = self . parse_pat ( None ) ?;
38693866 hi = pat. span ;
38703867 ( pat, fieldname, false )
38713868 } else {
@@ -4067,7 +4064,7 @@ impl<'a> Parser<'a> {
40674064 /// "top-level" patterns in a match arm, `for` loop, `let`, &c. (in contrast
40684065 /// to subpatterns within such).
40694066 fn parse_top_level_pat ( & mut self ) -> PResult < ' a , P < Pat > > {
4070- let pat = self . parse_pat ( ) ?;
4067+ let pat = self . parse_pat ( None ) ?;
40714068 if self . token == token:: Comma {
40724069 // An unexpected comma after a top-level pattern is a clue that the
40734070 // user (perhaps more accustomed to some other language) forgot the
@@ -4099,13 +4096,17 @@ impl<'a> Parser<'a> {
40994096 }
41004097
41014098 /// Parse a pattern.
4102- pub fn parse_pat ( & mut self ) -> PResult < ' a , P < Pat > > {
4103- self . parse_pat_with_range_pat ( true )
4099+ pub fn parse_pat ( & mut self , expected : Option < & ' static str > ) -> PResult < ' a , P < Pat > > {
4100+ self . parse_pat_with_range_pat ( true , expected )
41044101 }
41054102
41064103 /// Parse a pattern, with a setting whether modern range patterns e.g. `a..=b`, `a..b` are
41074104 /// allowed.
4108- fn parse_pat_with_range_pat ( & mut self , allow_range_pat : bool ) -> PResult < ' a , P < Pat > > {
4105+ fn parse_pat_with_range_pat (
4106+ & mut self ,
4107+ allow_range_pat : bool ,
4108+ expected : Option < & ' static str > ,
4109+ ) -> PResult < ' a , P < Pat > > {
41094110 maybe_whole ! ( self , NtPat , |x| x) ;
41104111
41114112 let lo = self . span ;
@@ -4121,7 +4122,7 @@ impl<'a> Parser<'a> {
41214122 err. span_label ( self . span , "unexpected lifetime" ) ;
41224123 return Err ( err) ;
41234124 }
4124- let subpat = self . parse_pat_with_range_pat ( false ) ?;
4125+ let subpat = self . parse_pat_with_range_pat ( false , expected ) ?;
41254126 pat = PatKind :: Ref ( subpat, mutbl) ;
41264127 }
41274128 token:: OpenDelim ( token:: Paren ) => {
@@ -4167,7 +4168,7 @@ impl<'a> Parser<'a> {
41674168 pat = self . parse_pat_ident ( BindingMode :: ByRef ( mutbl) ) ?;
41684169 } else if self . eat_keyword ( keywords:: Box ) {
41694170 // Parse box pat
4170- let subpat = self . parse_pat_with_range_pat ( false ) ?;
4171+ let subpat = self . parse_pat_with_range_pat ( false , None ) ?;
41714172 pat = PatKind :: Box ( subpat) ;
41724173 } else if self . token . is_ident ( ) && !self . token . is_reserved_ident ( ) &&
41734174 self . parse_as_ident ( ) {
@@ -4267,9 +4268,14 @@ impl<'a> Parser<'a> {
42674268 }
42684269 Err ( mut err) => {
42694270 self . cancel ( & mut err) ;
4270- let msg = format ! ( "expected pattern, found {}" , self . this_token_descr( ) ) ;
4271+ let expected = expected. unwrap_or ( "pattern" ) ;
4272+ let msg = format ! (
4273+ "expected {}, found {}" ,
4274+ expected,
4275+ self . this_token_descr( ) ,
4276+ ) ;
42714277 let mut err = self . fatal ( & msg) ;
4272- err. span_label ( self . span , "expected pattern" ) ;
4278+ err. span_label ( self . span , format ! ( "expected {}" , expected ) ) ;
42734279 return Err ( err) ;
42744280 }
42754281 }
@@ -4313,7 +4319,7 @@ impl<'a> Parser<'a> {
43134319 -> PResult < ' a , PatKind > {
43144320 let ident = self . parse_ident ( ) ?;
43154321 let sub = if self . eat ( & token:: At ) {
4316- Some ( self . parse_pat ( ) ?)
4322+ Some ( self . parse_pat ( Some ( "binding pattern" ) ) ?)
43174323 } else {
43184324 None
43194325 } ;
0 commit comments