@@ -36,6 +36,23 @@ impl BoundModifiers {
36
36
}
37
37
}
38
38
39
+ #[ derive( Copy , Clone ) ]
40
+ pub ( super ) enum AllowPlus {
41
+ Yes ,
42
+ No ,
43
+ }
44
+
45
+ pub ( super ) enum RecoverQPath {
46
+ Yes ,
47
+ No ,
48
+ }
49
+
50
+ // Is `...` (`CVarArgs`) legal at this level of type parsing?
51
+ enum AllowCVariadic {
52
+ Yes ,
53
+ No ,
54
+ }
55
+
39
56
/// Returns `true` if `IDENT t` can start a type -- `IDENT::a::b`, `IDENT<u8, u8>`,
40
57
/// `IDENT<<u8 as Trait>::AssocTy>`.
41
58
///
@@ -48,14 +65,14 @@ fn can_continue_type_after_non_fn_ident(t: &Token) -> bool {
48
65
impl < ' a > Parser < ' a > {
49
66
/// Parses a type.
50
67
pub fn parse_ty ( & mut self ) -> PResult < ' a , P < Ty > > {
51
- self . parse_ty_common ( true , true , false )
68
+ self . parse_ty_common ( AllowPlus :: Yes , RecoverQPath :: Yes , AllowCVariadic :: No )
52
69
}
53
70
54
71
/// Parse a type suitable for a function or function pointer parameter.
55
72
/// The difference from `parse_ty` is that this version allows `...`
56
73
/// (`CVarArgs`) at the top level of the the type.
57
74
pub ( super ) fn parse_ty_for_param ( & mut self ) -> PResult < ' a , P < Ty > > {
58
- self . parse_ty_common ( true , true , true )
75
+ self . parse_ty_common ( AllowPlus :: Yes , RecoverQPath :: Yes , AllowCVariadic :: Yes )
59
76
}
60
77
61
78
/// Parses a type in restricted contexts where `+` is not permitted.
@@ -65,30 +82,31 @@ impl<'a> Parser<'a> {
65
82
/// Example 2: `value1 as TYPE + value2`
66
83
/// `+` is prohibited to avoid interactions with expression grammar.
67
84
pub ( super ) fn parse_ty_no_plus ( & mut self ) -> PResult < ' a , P < Ty > > {
68
- self . parse_ty_common ( false , true , false )
85
+ self . parse_ty_common ( AllowPlus :: No , RecoverQPath :: Yes , AllowCVariadic :: No )
69
86
}
70
87
71
88
/// Parses an optional return type `[ -> TY ]` in a function declaration.
72
89
pub ( super ) fn parse_ret_ty (
73
90
& mut self ,
74
- allow_plus : bool ,
75
- allow_qpath_recovery : bool ,
91
+ allow_plus : AllowPlus ,
92
+ recover_qpath : RecoverQPath ,
76
93
) -> PResult < ' a , FunctionRetTy > {
77
94
Ok ( if self . eat ( & token:: RArrow ) {
78
95
// FIXME(Centril): Can we unconditionally `allow_plus`?
79
- FunctionRetTy :: Ty ( self . parse_ty_common ( allow_plus, allow_qpath_recovery, false ) ?)
96
+ let ty = self . parse_ty_common ( allow_plus, recover_qpath, AllowCVariadic :: No ) ?;
97
+ FunctionRetTy :: Ty ( ty)
80
98
} else {
81
99
FunctionRetTy :: Default ( self . token . span . shrink_to_lo ( ) )
82
100
} )
83
101
}
84
102
85
103
fn parse_ty_common (
86
104
& mut self ,
87
- allow_plus : bool ,
88
- allow_qpath_recovery : bool ,
89
- // Is `...` (`CVarArgs`) legal in the immediate top level call?
90
- allow_c_variadic : bool ,
105
+ allow_plus : AllowPlus ,
106
+ recover_qpath : RecoverQPath ,
107
+ allow_c_variadic : AllowCVariadic ,
91
108
) -> PResult < ' a , P < Ty > > {
109
+ let allow_qpath_recovery = matches ! ( recover_qpath, RecoverQPath :: Yes ) ;
92
110
maybe_recover_from_interpolated_ty_qpath ! ( self , allow_qpath_recovery) ;
93
111
maybe_whole ! ( self , NtTy , |x| x) ;
94
112
@@ -124,7 +142,7 @@ impl<'a> Parser<'a> {
124
142
self . parse_ty_bare_fn ( lifetime_defs) ?
125
143
} else {
126
144
let path = self . parse_path ( PathStyle :: Type ) ?;
127
- let parse_plus = allow_plus && self . check_plus ( ) ;
145
+ let parse_plus = matches ! ( allow_plus, AllowPlus :: Yes ) && self . check_plus ( ) ;
128
146
self . parse_remaining_bounds ( lifetime_defs, path, lo, parse_plus) ?
129
147
}
130
148
} else if self . eat_keyword ( kw:: Impl ) {
@@ -144,7 +162,7 @@ impl<'a> Parser<'a> {
144
162
} else if self . token . is_path_start ( ) {
145
163
self . parse_path_start_ty ( lo, allow_plus) ?
146
164
} else if self . eat ( & token:: DotDotDot ) {
147
- if allow_c_variadic {
165
+ if let AllowCVariadic :: Yes = allow_c_variadic {
148
166
TyKind :: CVarArgs
149
167
} else {
150
168
// FIXME(Centril): Should we just allow `...` syntactically
@@ -172,7 +190,7 @@ impl<'a> Parser<'a> {
172
190
/// Parses either:
173
191
/// - `(TYPE)`, a parenthesized type.
174
192
/// - `(TYPE,)`, a tuple with a single field of type TYPE.
175
- fn parse_ty_tuple_or_parens ( & mut self , lo : Span , allow_plus : bool ) -> PResult < ' a , TyKind > {
193
+ fn parse_ty_tuple_or_parens ( & mut self , lo : Span , allow_plus : AllowPlus ) -> PResult < ' a , TyKind > {
176
194
let mut trailing_plus = false ;
177
195
let ( ts, trailing) = self . parse_paren_comma_seq ( |p| {
178
196
let ty = p. parse_ty ( ) ?;
@@ -182,7 +200,7 @@ impl<'a> Parser<'a> {
182
200
183
201
if ts. len ( ) == 1 && !trailing {
184
202
let ty = ts. into_iter ( ) . nth ( 0 ) . unwrap ( ) . into_inner ( ) ;
185
- let maybe_bounds = allow_plus && self . token . is_like_plus ( ) ;
203
+ let maybe_bounds = matches ! ( allow_plus, AllowPlus :: Yes ) && self . token . is_like_plus ( ) ;
186
204
match ty. kind {
187
205
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
188
206
TyKind :: Path ( None , path) if maybe_bounds => {
@@ -288,7 +306,8 @@ impl<'a> Parser<'a> {
288
306
let unsafety = self . parse_unsafety ( ) ;
289
307
let ext = self . parse_extern ( ) ?;
290
308
self . expect_keyword ( kw:: Fn ) ?;
291
- let decl = self . parse_fn_decl ( & ParamCfg { is_name_required : |_| false } , false ) ?;
309
+ let cfg = ParamCfg { is_name_required : |_| false } ;
310
+ let decl = self . parse_fn_decl ( & cfg, AllowPlus :: No ) ?;
292
311
Ok ( TyKind :: BareFn ( P ( BareFnTy { ext, unsafety, generic_params, decl } ) ) )
293
312
}
294
313
@@ -326,7 +345,7 @@ impl<'a> Parser<'a> {
326
345
/// 1. a type macro, `mac!(...)`,
327
346
/// 2. a bare trait object, `B0 + ... + Bn`,
328
347
/// 3. or a path, `path::to::MyType`.
329
- fn parse_path_start_ty ( & mut self , lo : Span , allow_plus : bool ) -> PResult < ' a , TyKind > {
348
+ fn parse_path_start_ty ( & mut self , lo : Span , allow_plus : AllowPlus ) -> PResult < ' a , TyKind > {
330
349
// Simple path
331
350
let path = self . parse_path ( PathStyle :: Type ) ?;
332
351
if self . eat ( & token:: Not ) {
@@ -336,7 +355,7 @@ impl<'a> Parser<'a> {
336
355
args : self . parse_mac_args ( ) ?,
337
356
prior_type_ascription : self . last_type_ascription ,
338
357
} ) )
339
- } else if allow_plus && self . check_plus ( ) {
358
+ } else if matches ! ( allow_plus, AllowPlus :: Yes ) && self . check_plus ( ) {
340
359
// `Trait1 + Trait2 + 'a`
341
360
self . parse_remaining_bounds ( Vec :: new ( ) , path, lo, true )
342
361
} else {
@@ -359,15 +378,15 @@ impl<'a> Parser<'a> {
359
378
& mut self ,
360
379
colon_span : Option < Span > ,
361
380
) -> PResult < ' a , GenericBounds > {
362
- self . parse_generic_bounds_common ( true , colon_span)
381
+ self . parse_generic_bounds_common ( AllowPlus :: Yes , colon_span)
363
382
}
364
383
365
384
/// Parses bounds of a type parameter `BOUND + BOUND + ...`, possibly with trailing `+`.
366
385
///
367
386
/// See `parse_generic_bound` for the `BOUND` grammar.
368
387
fn parse_generic_bounds_common (
369
388
& mut self ,
370
- allow_plus : bool ,
389
+ allow_plus : AllowPlus ,
371
390
colon_span : Option < Span > ,
372
391
) -> PResult < ' a , GenericBounds > {
373
392
let mut bounds = Vec :: new ( ) ;
@@ -377,7 +396,7 @@ impl<'a> Parser<'a> {
377
396
Ok ( bound) => bounds. push ( bound) ,
378
397
Err ( neg_sp) => negative_bounds. push ( neg_sp) ,
379
398
}
380
- if ! allow_plus || !self . eat_plus ( ) {
399
+ if matches ! ( allow_plus, AllowPlus :: No ) || !self . eat_plus ( ) {
381
400
break ;
382
401
}
383
402
}
0 commit comments