@@ -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 ,
@@ -182,7 +198,7 @@ impl<'a> Parser<'a> {
182
198
/// otherwise).
183
199
pub ( super ) fn parse_pat_before_ty (
184
200
& mut self ,
185
- expected : Expected ,
201
+ expected : Option < Expected > ,
186
202
rc : RecoverComma ,
187
203
syntax_loc : PatternLocation ,
188
204
) -> PResult < ' a , ( P < Pat > , bool ) > {
@@ -254,7 +270,7 @@ impl<'a> Parser<'a> {
254
270
}
255
271
256
272
self . parse_pat_before_ty (
257
- PARAM_EXPECTED ,
273
+ Some ( Expected :: ParameterName ) ,
258
274
RecoverComma :: No ,
259
275
PatternLocation :: FunctionParameter ,
260
276
)
@@ -320,7 +336,7 @@ impl<'a> Parser<'a> {
320
336
fn parse_pat_with_range_pat (
321
337
& mut self ,
322
338
allow_range_pat : bool ,
323
- expected : Expected ,
339
+ expected : Option < Expected > ,
324
340
) -> PResult < ' a , P < Pat > > {
325
341
maybe_recover_from_interpolated_ty_qpath ! ( self , true ) ;
326
342
maybe_whole ! ( self , NtPat , |x| x) ;
@@ -416,7 +432,7 @@ impl<'a> Parser<'a> {
416
432
let lt = self . expect_lifetime ( ) ;
417
433
let ( lit, _) =
418
434
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 ) ;
420
436
let msg = format ! (
421
437
"expected {}, found {}" ,
422
438
expected,
@@ -527,7 +543,7 @@ impl<'a> Parser<'a> {
527
543
}
528
544
529
545
/// 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 > {
531
547
self . expect_and ( ) ?;
532
548
if let token:: Lifetime ( name) = self . token . kind {
533
549
self . bump ( ) ; // `'a`
@@ -580,7 +596,7 @@ impl<'a> Parser<'a> {
580
596
}
581
597
582
598
// 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 ) ) ?;
584
600
585
601
// If we don't have `mut $ident (@ pat)?`, error.
586
602
if let PatKind :: Ident ( BindingAnnotation ( ByRef :: No , m @ Mutability :: Not ) , ..) = & mut pat. kind
@@ -652,11 +668,11 @@ impl<'a> Parser<'a> {
652
668
fn fatal_unexpected_non_pat (
653
669
& mut self ,
654
670
err : DiagnosticBuilder < ' a , ErrorGuaranteed > ,
655
- expected : Expected ,
671
+ expected : Option < Expected > ,
656
672
) -> PResult < ' a , P < Pat > > {
657
673
err. cancel ( ) ;
658
674
659
- let expected = expected . unwrap_or ( "pattern" ) ;
675
+ let expected = Expected :: to_string_or_fallback ( expected ) ;
660
676
let msg = format ! ( "expected {}, found {}" , expected, super :: token_descr( & self . token) ) ;
661
677
662
678
let mut err = self . struct_span_err ( self . token . span , & msg) ;
@@ -809,7 +825,7 @@ impl<'a> Parser<'a> {
809
825
fn parse_pat_ident ( & mut self , binding_annotation : BindingAnnotation ) -> PResult < ' a , PatKind > {
810
826
let ident = self . parse_ident ( ) ?;
811
827
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 ) ) ?)
813
829
} else {
814
830
None
815
831
} ;
@@ -903,7 +919,7 @@ impl<'a> Parser<'a> {
903
919
// We cannot use `parse_pat_ident()` since it will complain `box`
904
920
// is not an identifier.
905
921
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 ) ) ?)
907
923
} else {
908
924
None
909
925
} ;
0 commit comments