@@ -24,10 +24,26 @@ use rustc_session::errors::ExprParenthesesNeeded;
2424use rustc_span:: source_map:: { respan, Span , Spanned } ;
2525use rustc_span:: symbol:: { kw, sym, Ident } ;
2626
27- pub ( super ) type Expected = Option < & ' static str > ;
27+ #[ derive( PartialEq , Copy , Clone ) ]
28+ pub enum Expected {
29+ ParameterName ,
30+ ArgumentName ,
31+ Identifier ,
32+ BindingPattern ,
33+ }
2834
29- /// `Expected` for function and lambda parameter patterns.
30- pub ( super ) const PARAM_EXPECTED : Expected = Some ( "parameter name" ) ;
35+ impl Expected {
36+ // FIXME(#100717): migrate users of this to proper localization
37+ fn to_string_or_fallback ( expected : Option < Expected > ) -> & ' static str {
38+ match expected {
39+ Some ( Expected :: ParameterName ) => "parameter name" ,
40+ Some ( Expected :: ArgumentName ) => "argument name" ,
41+ Some ( Expected :: Identifier ) => "identifier" ,
42+ Some ( Expected :: BindingPattern ) => "binding pattern" ,
43+ None => "pattern" ,
44+ }
45+ }
46+ }
3147
3248const WHILE_PARSING_OR_MSG : & str = "while parsing this or-pattern starting here" ;
3349
@@ -76,7 +92,7 @@ impl<'a> Parser<'a> {
7692 /// Corresponds to `pat<no_top_alt>` in RFC 2535 and does not admit or-patterns
7793 /// at the top level. Used when parsing the parameters of lambda expressions,
7894 /// functions, function pointers, and `pat` macro fragments.
79- pub fn parse_pat_no_top_alt ( & mut self , expected : Expected ) -> PResult < ' a , P < Pat > > {
95+ pub fn parse_pat_no_top_alt ( & mut self , expected : Option < Expected > ) -> PResult < ' a , P < Pat > > {
8096 self . parse_pat_with_range_pat ( true , expected)
8197 }
8298
@@ -90,7 +106,7 @@ impl<'a> Parser<'a> {
90106 /// simplify the grammar somewhat.
91107 pub fn parse_pat_allow_top_alt (
92108 & mut self ,
93- expected : Expected ,
109+ expected : Option < Expected > ,
94110 rc : RecoverComma ,
95111 ra : RecoverColon ,
96112 rt : CommaRecoveryMode ,
@@ -102,7 +118,7 @@ impl<'a> Parser<'a> {
102118 /// recovered).
103119 fn parse_pat_allow_top_alt_inner (
104120 & mut self ,
105- expected : Expected ,
121+ expected : Option < Expected > ,
106122 rc : RecoverComma ,
107123 ra : RecoverColon ,
108124 rt : CommaRecoveryMode ,
@@ -181,7 +197,7 @@ impl<'a> Parser<'a> {
181197 /// otherwise).
182198 pub ( super ) fn parse_pat_before_ty (
183199 & mut self ,
184- expected : Expected ,
200+ expected : Option < Expected > ,
185201 rc : RecoverComma ,
186202 syntax_loc : PatternLocation ,
187203 ) -> PResult < ' a , ( P < Pat > , bool ) > {
@@ -253,7 +269,7 @@ impl<'a> Parser<'a> {
253269 }
254270
255271 self . parse_pat_before_ty (
256- PARAM_EXPECTED ,
272+ Some ( Expected :: ParameterName ) ,
257273 RecoverComma :: No ,
258274 PatternLocation :: FunctionParameter ,
259275 )
@@ -319,7 +335,7 @@ impl<'a> Parser<'a> {
319335 fn parse_pat_with_range_pat (
320336 & mut self ,
321337 allow_range_pat : bool ,
322- expected : Expected ,
338+ expected : Option < Expected > ,
323339 ) -> PResult < ' a , P < Pat > > {
324340 maybe_recover_from_interpolated_ty_qpath ! ( self , true ) ;
325341 maybe_whole ! ( self , NtPat , |x| x) ;
@@ -415,7 +431,7 @@ impl<'a> Parser<'a> {
415431 let lt = self . expect_lifetime ( ) ;
416432 let ( lit, _) =
417433 self . recover_unclosed_char ( lt. ident , Parser :: mk_token_lit_char, |self_| {
418- let expected = expected . unwrap_or ( "pattern" ) ;
434+ let expected = Expected :: to_string_or_fallback ( expected ) ;
419435 let msg = format ! (
420436 "expected {}, found {}" ,
421437 expected,
@@ -526,7 +542,7 @@ impl<'a> Parser<'a> {
526542 }
527543
528544 /// Parse `&pat` / `&mut pat`.
529- fn parse_pat_deref ( & mut self , expected : Expected ) -> PResult < ' a , PatKind > {
545+ fn parse_pat_deref ( & mut self , expected : Option < Expected > ) -> PResult < ' a , PatKind > {
530546 self . expect_and ( ) ?;
531547 if let token:: Lifetime ( name) = self . token . kind {
532548 self . bump ( ) ; // `'a`
@@ -579,7 +595,7 @@ impl<'a> Parser<'a> {
579595 }
580596
581597 // Parse the pattern we hope to be an identifier.
582- let mut pat = self . parse_pat_no_top_alt ( Some ( "identifier" ) ) ?;
598+ let mut pat = self . parse_pat_no_top_alt ( Some ( Expected :: Identifier ) ) ?;
583599
584600 // If we don't have `mut $ident (@ pat)?`, error.
585601 if let PatKind :: Ident ( BindingAnnotation ( ByRef :: No , m @ Mutability :: Not ) , ..) = & mut pat. kind
@@ -651,11 +667,11 @@ impl<'a> Parser<'a> {
651667 fn fatal_unexpected_non_pat (
652668 & mut self ,
653669 err : DiagnosticBuilder < ' a , ErrorGuaranteed > ,
654- expected : Expected ,
670+ expected : Option < Expected > ,
655671 ) -> PResult < ' a , P < Pat > > {
656672 err. cancel ( ) ;
657673
658- let expected = expected . unwrap_or ( "pattern" ) ;
674+ let expected = Expected :: to_string_or_fallback ( expected ) ;
659675 let msg = format ! ( "expected {}, found {}" , expected, super :: token_descr( & self . token) ) ;
660676
661677 let mut err = self . struct_span_err ( self . token . span , & msg) ;
@@ -799,7 +815,7 @@ impl<'a> Parser<'a> {
799815 fn parse_pat_ident ( & mut self , binding_annotation : BindingAnnotation ) -> PResult < ' a , PatKind > {
800816 let ident = self . parse_ident ( ) ?;
801817 let sub = if self . eat ( & token:: At ) {
802- Some ( self . parse_pat_no_top_alt ( Some ( "binding pattern" ) ) ?)
818+ Some ( self . parse_pat_no_top_alt ( Some ( Expected :: BindingPattern ) ) ?)
803819 } else {
804820 None
805821 } ;
@@ -893,7 +909,7 @@ impl<'a> Parser<'a> {
893909 // We cannot use `parse_pat_ident()` since it will complain `box`
894910 // is not an identifier.
895911 let sub = if self . eat ( & token:: At ) {
896- Some ( self . parse_pat_no_top_alt ( Some ( "binding pattern" ) ) ?)
912+ Some ( self . parse_pat_no_top_alt ( Some ( Expected :: BindingPattern ) ) ?)
897913 } else {
898914 None
899915 } ;
0 commit comments