@@ -1827,10 +1827,7 @@ impl<'a> Parser<'a> {
1827
1827
debug ! ( "parse_arg_general parse_pat (require_name:{})" ,
1828
1828
require_name) ;
1829
1829
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" ) ) ?;
1834
1831
1835
1832
if let Err ( mut err) = self . expect ( & token:: Colon ) {
1836
1833
// If we find a pattern followed by an identifier, it could be an (incorrect)
@@ -1879,7 +1876,7 @@ impl<'a> Parser<'a> {
1879
1876
// Recover from attempting to parse the argument as a type without pattern.
1880
1877
err. cancel ( ) ;
1881
1878
mem:: replace ( self , parser_snapshot_before_ty) ;
1882
- let pat = self . parse_pat ( ) ?;
1879
+ let pat = self . parse_pat ( Some ( "argument name" ) ) ?;
1883
1880
self . expect ( & token:: Colon ) ?;
1884
1881
let ty = self . parse_ty ( ) ?;
1885
1882
@@ -1917,7 +1914,7 @@ impl<'a> Parser<'a> {
1917
1914
1918
1915
/// Parse an argument in a lambda header e.g. |arg, arg|
1919
1916
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" ) ) ?;
1921
1918
let t = if self . eat ( & token:: Colon ) {
1922
1919
self . parse_ty ( ) ?
1923
1920
} else {
@@ -3784,7 +3781,7 @@ impl<'a> Parser<'a> {
3784
3781
"`..` can only be used once per tuple or tuple struct pattern" ) ;
3785
3782
}
3786
3783
} else if !self . check ( & token:: CloseDelim ( token:: Paren ) ) {
3787
- fields. push ( self . parse_pat ( ) ?) ;
3784
+ fields. push ( self . parse_pat ( None ) ?) ;
3788
3785
} else {
3789
3786
break
3790
3787
}
@@ -3840,7 +3837,7 @@ impl<'a> Parser<'a> {
3840
3837
}
3841
3838
}
3842
3839
3843
- let subpat = self . parse_pat ( ) ?;
3840
+ let subpat = self . parse_pat ( None ) ?;
3844
3841
if before_slice && self . eat ( & token:: DotDot ) {
3845
3842
slice = Some ( subpat) ;
3846
3843
before_slice = false ;
@@ -3865,7 +3862,7 @@ impl<'a> Parser<'a> {
3865
3862
// Parsing a pattern of the form "fieldname: pat"
3866
3863
let fieldname = self . parse_field_name ( ) ?;
3867
3864
self . bump ( ) ;
3868
- let pat = self . parse_pat ( ) ?;
3865
+ let pat = self . parse_pat ( None ) ?;
3869
3866
hi = pat. span ;
3870
3867
( pat, fieldname, false )
3871
3868
} else {
@@ -4067,7 +4064,7 @@ impl<'a> Parser<'a> {
4067
4064
/// "top-level" patterns in a match arm, `for` loop, `let`, &c. (in contrast
4068
4065
/// to subpatterns within such).
4069
4066
fn parse_top_level_pat ( & mut self ) -> PResult < ' a , P < Pat > > {
4070
- let pat = self . parse_pat ( ) ?;
4067
+ let pat = self . parse_pat ( None ) ?;
4071
4068
if self . token == token:: Comma {
4072
4069
// An unexpected comma after a top-level pattern is a clue that the
4073
4070
// user (perhaps more accustomed to some other language) forgot the
@@ -4099,13 +4096,17 @@ impl<'a> Parser<'a> {
4099
4096
}
4100
4097
4101
4098
/// 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 )
4104
4101
}
4105
4102
4106
4103
/// Parse a pattern, with a setting whether modern range patterns e.g. `a..=b`, `a..b` are
4107
4104
/// 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 > > {
4109
4110
maybe_whole ! ( self , NtPat , |x| x) ;
4110
4111
4111
4112
let lo = self . span ;
@@ -4121,7 +4122,7 @@ impl<'a> Parser<'a> {
4121
4122
err. span_label ( self . span , "unexpected lifetime" ) ;
4122
4123
return Err ( err) ;
4123
4124
}
4124
- let subpat = self . parse_pat_with_range_pat ( false ) ?;
4125
+ let subpat = self . parse_pat_with_range_pat ( false , expected ) ?;
4125
4126
pat = PatKind :: Ref ( subpat, mutbl) ;
4126
4127
}
4127
4128
token:: OpenDelim ( token:: Paren ) => {
@@ -4167,7 +4168,7 @@ impl<'a> Parser<'a> {
4167
4168
pat = self . parse_pat_ident ( BindingMode :: ByRef ( mutbl) ) ?;
4168
4169
} else if self . eat_keyword ( keywords:: Box ) {
4169
4170
// Parse box pat
4170
- let subpat = self . parse_pat_with_range_pat ( false ) ?;
4171
+ let subpat = self . parse_pat_with_range_pat ( false , None ) ?;
4171
4172
pat = PatKind :: Box ( subpat) ;
4172
4173
} else if self . token . is_ident ( ) && !self . token . is_reserved_ident ( ) &&
4173
4174
self . parse_as_ident ( ) {
@@ -4267,9 +4268,14 @@ impl<'a> Parser<'a> {
4267
4268
}
4268
4269
Err ( mut err) => {
4269
4270
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
+ ) ;
4271
4277
let mut err = self . fatal ( & msg) ;
4272
- err. span_label ( self . span , "expected pattern" ) ;
4278
+ err. span_label ( self . span , format ! ( "expected {}" , expected ) ) ;
4273
4279
return Err ( err) ;
4274
4280
}
4275
4281
}
@@ -4313,7 +4319,7 @@ impl<'a> Parser<'a> {
4313
4319
-> PResult < ' a , PatKind > {
4314
4320
let ident = self . parse_ident ( ) ?;
4315
4321
let sub = if self . eat ( & token:: At ) {
4316
- Some ( self . parse_pat ( ) ?)
4322
+ Some ( self . parse_pat ( Some ( "binding pattern" ) ) ?)
4317
4323
} else {
4318
4324
None
4319
4325
} ;
0 commit comments