@@ -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 ,
@@ -182,7 +198,7 @@ impl<'a> Parser<'a> {
182198 /// otherwise).
183199 pub ( super ) fn parse_pat_before_ty (
184200 & mut self ,
185- expected : Expected ,
201+ expected : Option < Expected > ,
186202 rc : RecoverComma ,
187203 syntax_loc : PatternLocation ,
188204 ) -> PResult < ' a , ( P < Pat > , bool ) > {
@@ -254,7 +270,7 @@ impl<'a> Parser<'a> {
254270 }
255271
256272 self . parse_pat_before_ty (
257- PARAM_EXPECTED ,
273+ Some ( Expected :: ParameterName ) ,
258274 RecoverComma :: No ,
259275 PatternLocation :: FunctionParameter ,
260276 )
@@ -320,7 +336,7 @@ impl<'a> Parser<'a> {
320336 fn parse_pat_with_range_pat (
321337 & mut self ,
322338 allow_range_pat : bool ,
323- expected : Expected ,
339+ expected : Option < Expected > ,
324340 ) -> PResult < ' a , P < Pat > > {
325341 maybe_recover_from_interpolated_ty_qpath ! ( self , true ) ;
326342 maybe_whole ! ( self , NtPat , |x| x) ;
@@ -416,7 +432,7 @@ impl<'a> Parser<'a> {
416432 let lt = self . expect_lifetime ( ) ;
417433 let ( lit, _) =
418434 self . recover_unclosed_char ( lt. ident , Parser :: mk_token_lit_char, |self_| {
419- let expected = expected . unwrap_or ( "pattern" ) ;
435+ let expected = Expected :: to_string_or_fallback ( expected ) ;
420436 let msg = format ! (
421437 "expected {}, found {}" ,
422438 expected,
@@ -527,7 +543,7 @@ impl<'a> Parser<'a> {
527543 }
528544
529545 /// Parse `&pat` / `&mut pat`.
530- fn parse_pat_deref ( & mut self , expected : Expected ) -> PResult < ' a , PatKind > {
546+ fn parse_pat_deref ( & mut self , expected : Option < Expected > ) -> PResult < ' a , PatKind > {
531547 self . expect_and ( ) ?;
532548 if let token:: Lifetime ( name) = self . token . kind {
533549 self . bump ( ) ; // `'a`
@@ -580,7 +596,7 @@ impl<'a> Parser<'a> {
580596 }
581597
582598 // Parse the pattern we hope to be an identifier.
583- let mut pat = self . parse_pat_no_top_alt ( Some ( "identifier" ) ) ?;
599+ let mut pat = self . parse_pat_no_top_alt ( Some ( Expected :: Identifier ) ) ?;
584600
585601 // If we don't have `mut $ident (@ pat)?`, error.
586602 if let PatKind :: Ident ( BindingAnnotation ( ByRef :: No , m @ Mutability :: Not ) , ..) = & mut pat. kind
@@ -652,11 +668,11 @@ impl<'a> Parser<'a> {
652668 fn fatal_unexpected_non_pat (
653669 & mut self ,
654670 err : DiagnosticBuilder < ' a , ErrorGuaranteed > ,
655- expected : Expected ,
671+ expected : Option < Expected > ,
656672 ) -> PResult < ' a , P < Pat > > {
657673 err. cancel ( ) ;
658674
659- let expected = expected . unwrap_or ( "pattern" ) ;
675+ let expected = Expected :: to_string_or_fallback ( expected ) ;
660676 let msg = format ! ( "expected {}, found {}" , expected, super :: token_descr( & self . token) ) ;
661677
662678 let mut err = self . struct_span_err ( self . token . span , & msg) ;
@@ -809,7 +825,7 @@ impl<'a> Parser<'a> {
809825 fn parse_pat_ident ( & mut self , binding_annotation : BindingAnnotation ) -> PResult < ' a , PatKind > {
810826 let ident = self . parse_ident ( ) ?;
811827 let sub = if self . eat ( & token:: At ) {
812- Some ( self . parse_pat_no_top_alt ( Some ( "binding pattern" ) ) ?)
828+ Some ( self . parse_pat_no_top_alt ( Some ( Expected :: BindingPattern ) ) ?)
813829 } else {
814830 None
815831 } ;
@@ -903,7 +919,7 @@ impl<'a> Parser<'a> {
903919 // We cannot use `parse_pat_ident()` since it will complain `box`
904920 // is not an identifier.
905921 let sub = if self . eat ( & token:: At ) {
906- Some ( self . parse_pat_no_top_alt ( Some ( "binding pattern" ) ) ?)
922+ Some ( self . parse_pat_no_top_alt ( Some ( Expected :: BindingPattern ) ) ?)
907923 } else {
908924 None
909925 } ;
0 commit comments