@@ -24,10 +24,26 @@ use rustc_session::errors::ExprParenthesesNeeded;
24
24
use rustc_span:: source_map:: { respan, Span , Spanned } ;
25
25
use rustc_span:: symbol:: { kw, sym, Ident } ;
26
26
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
+ }
28
34
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
+ }
31
47
32
48
const WHILE_PARSING_OR_MSG : & str = "while parsing this or-pattern starting here" ;
33
49
@@ -76,7 +92,7 @@ impl<'a> Parser<'a> {
76
92
/// Corresponds to `pat<no_top_alt>` in RFC 2535 and does not admit or-patterns
77
93
/// at the top level. Used when parsing the parameters of lambda expressions,
78
94
/// 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 > > {
80
96
self . parse_pat_with_range_pat ( true , expected)
81
97
}
82
98
@@ -90,7 +106,7 @@ impl<'a> Parser<'a> {
90
106
/// simplify the grammar somewhat.
91
107
pub fn parse_pat_allow_top_alt (
92
108
& mut self ,
93
- expected : Expected ,
109
+ expected : Option < Expected > ,
94
110
rc : RecoverComma ,
95
111
ra : RecoverColon ,
96
112
rt : CommaRecoveryMode ,
@@ -102,7 +118,7 @@ impl<'a> Parser<'a> {
102
118
/// recovered).
103
119
fn parse_pat_allow_top_alt_inner (
104
120
& mut self ,
105
- expected : Expected ,
121
+ expected : Option < Expected > ,
106
122
rc : RecoverComma ,
107
123
ra : RecoverColon ,
108
124
rt : CommaRecoveryMode ,
@@ -181,7 +197,7 @@ impl<'a> Parser<'a> {
181
197
/// otherwise).
182
198
pub ( super ) fn parse_pat_before_ty (
183
199
& mut self ,
184
- expected : Expected ,
200
+ expected : Option < Expected > ,
185
201
rc : RecoverComma ,
186
202
syntax_loc : PatternLocation ,
187
203
) -> PResult < ' a , ( P < Pat > , bool ) > {
@@ -253,7 +269,7 @@ impl<'a> Parser<'a> {
253
269
}
254
270
255
271
self . parse_pat_before_ty (
256
- PARAM_EXPECTED ,
272
+ Some ( Expected :: ParameterName ) ,
257
273
RecoverComma :: No ,
258
274
PatternLocation :: FunctionParameter ,
259
275
)
@@ -319,7 +335,7 @@ impl<'a> Parser<'a> {
319
335
fn parse_pat_with_range_pat (
320
336
& mut self ,
321
337
allow_range_pat : bool ,
322
- expected : Expected ,
338
+ expected : Option < Expected > ,
323
339
) -> PResult < ' a , P < Pat > > {
324
340
maybe_recover_from_interpolated_ty_qpath ! ( self , true ) ;
325
341
maybe_whole ! ( self , NtPat , |x| x) ;
@@ -415,7 +431,7 @@ impl<'a> Parser<'a> {
415
431
let lt = self . expect_lifetime ( ) ;
416
432
let ( lit, _) =
417
433
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 ) ;
419
435
let msg = format ! (
420
436
"expected {}, found {}" ,
421
437
expected,
@@ -526,7 +542,7 @@ impl<'a> Parser<'a> {
526
542
}
527
543
528
544
/// 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 > {
530
546
self . expect_and ( ) ?;
531
547
if let token:: Lifetime ( name) = self . token . kind {
532
548
self . bump ( ) ; // `'a`
@@ -579,7 +595,7 @@ impl<'a> Parser<'a> {
579
595
}
580
596
581
597
// 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 ) ) ?;
583
599
584
600
// If we don't have `mut $ident (@ pat)?`, error.
585
601
if let PatKind :: Ident ( BindingAnnotation ( ByRef :: No , m @ Mutability :: Not ) , ..) = & mut pat. kind
@@ -651,11 +667,11 @@ impl<'a> Parser<'a> {
651
667
fn fatal_unexpected_non_pat (
652
668
& mut self ,
653
669
err : DiagnosticBuilder < ' a , ErrorGuaranteed > ,
654
- expected : Expected ,
670
+ expected : Option < Expected > ,
655
671
) -> PResult < ' a , P < Pat > > {
656
672
err. cancel ( ) ;
657
673
658
- let expected = expected . unwrap_or ( "pattern" ) ;
674
+ let expected = Expected :: to_string_or_fallback ( expected ) ;
659
675
let msg = format ! ( "expected {}, found {}" , expected, super :: token_descr( & self . token) ) ;
660
676
661
677
let mut err = self . struct_span_err ( self . token . span , & msg) ;
@@ -799,7 +815,7 @@ impl<'a> Parser<'a> {
799
815
fn parse_pat_ident ( & mut self , binding_annotation : BindingAnnotation ) -> PResult < ' a , PatKind > {
800
816
let ident = self . parse_ident ( ) ?;
801
817
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 ) ) ?)
803
819
} else {
804
820
None
805
821
} ;
@@ -893,7 +909,7 @@ impl<'a> Parser<'a> {
893
909
// We cannot use `parse_pat_ident()` since it will complain `box`
894
910
// is not an identifier.
895
911
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 ) ) ?)
897
913
} else {
898
914
None
899
915
} ;
0 commit comments