@@ -36,6 +36,23 @@ impl BoundModifiers {
3636 }
3737}
3838
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+
3956/// Returns `true` if `IDENT t` can start a type -- `IDENT::a::b`, `IDENT<u8, u8>`,
4057/// `IDENT<<u8 as Trait>::AssocTy>`.
4158///
@@ -48,14 +65,14 @@ fn can_continue_type_after_non_fn_ident(t: &Token) -> bool {
4865impl < ' a > Parser < ' a > {
4966 /// Parses a type.
5067 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 )
5269 }
5370
5471 /// Parse a type suitable for a function or function pointer parameter.
5572 /// The difference from `parse_ty` is that this version allows `...`
5673 /// (`CVarArgs`) at the top level of the the type.
5774 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 )
5976 }
6077
6178 /// Parses a type in restricted contexts where `+` is not permitted.
@@ -65,30 +82,31 @@ impl<'a> Parser<'a> {
6582 /// Example 2: `value1 as TYPE + value2`
6683 /// `+` is prohibited to avoid interactions with expression grammar.
6784 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 )
6986 }
7087
7188 /// Parses an optional return type `[ -> TY ]` in a function declaration.
7289 pub ( super ) fn parse_ret_ty (
7390 & mut self ,
74- allow_plus : bool ,
75- allow_qpath_recovery : bool ,
91+ allow_plus : AllowPlus ,
92+ recover_qpath : RecoverQPath ,
7693 ) -> PResult < ' a , FunctionRetTy > {
7794 Ok ( if self . eat ( & token:: RArrow ) {
7895 // 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)
8098 } else {
8199 FunctionRetTy :: Default ( self . token . span . shrink_to_lo ( ) )
82100 } )
83101 }
84102
85103 fn parse_ty_common (
86104 & 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 ,
91108 ) -> PResult < ' a , P < Ty > > {
109+ let allow_qpath_recovery = matches ! ( recover_qpath, RecoverQPath :: Yes ) ;
92110 maybe_recover_from_interpolated_ty_qpath ! ( self , allow_qpath_recovery) ;
93111 maybe_whole ! ( self , NtTy , |x| x) ;
94112
@@ -124,7 +142,7 @@ impl<'a> Parser<'a> {
124142 self . parse_ty_bare_fn ( lifetime_defs) ?
125143 } else {
126144 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 ( ) ;
128146 self . parse_remaining_bounds ( lifetime_defs, path, lo, parse_plus) ?
129147 }
130148 } else if self . eat_keyword ( kw:: Impl ) {
@@ -144,7 +162,7 @@ impl<'a> Parser<'a> {
144162 } else if self . token . is_path_start ( ) {
145163 self . parse_path_start_ty ( lo, allow_plus) ?
146164 } else if self . eat ( & token:: DotDotDot ) {
147- if allow_c_variadic {
165+ if let AllowCVariadic :: Yes = allow_c_variadic {
148166 TyKind :: CVarArgs
149167 } else {
150168 // FIXME(Centril): Should we just allow `...` syntactically
@@ -172,7 +190,7 @@ impl<'a> Parser<'a> {
172190 /// Parses either:
173191 /// - `(TYPE)`, a parenthesized type.
174192 /// - `(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 > {
176194 let mut trailing_plus = false ;
177195 let ( ts, trailing) = self . parse_paren_comma_seq ( |p| {
178196 let ty = p. parse_ty ( ) ?;
@@ -182,7 +200,7 @@ impl<'a> Parser<'a> {
182200
183201 if ts. len ( ) == 1 && !trailing {
184202 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 ( ) ;
186204 match ty. kind {
187205 // `(TY_BOUND_NOPAREN) + BOUND + ...`.
188206 TyKind :: Path ( None , path) if maybe_bounds => {
@@ -288,7 +306,8 @@ impl<'a> Parser<'a> {
288306 let unsafety = self . parse_unsafety ( ) ;
289307 let ext = self . parse_extern ( ) ?;
290308 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 ) ?;
292311 Ok ( TyKind :: BareFn ( P ( BareFnTy { ext, unsafety, generic_params, decl } ) ) )
293312 }
294313
@@ -326,7 +345,7 @@ impl<'a> Parser<'a> {
326345 /// 1. a type macro, `mac!(...)`,
327346 /// 2. a bare trait object, `B0 + ... + Bn`,
328347 /// 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 > {
330349 // Simple path
331350 let path = self . parse_path ( PathStyle :: Type ) ?;
332351 if self . eat ( & token:: Not ) {
@@ -336,7 +355,7 @@ impl<'a> Parser<'a> {
336355 args : self . parse_mac_args ( ) ?,
337356 prior_type_ascription : self . last_type_ascription ,
338357 } ) )
339- } else if allow_plus && self . check_plus ( ) {
358+ } else if matches ! ( allow_plus, AllowPlus :: Yes ) && self . check_plus ( ) {
340359 // `Trait1 + Trait2 + 'a`
341360 self . parse_remaining_bounds ( Vec :: new ( ) , path, lo, true )
342361 } else {
@@ -359,15 +378,15 @@ impl<'a> Parser<'a> {
359378 & mut self ,
360379 colon_span : Option < Span > ,
361380 ) -> PResult < ' a , GenericBounds > {
362- self . parse_generic_bounds_common ( true , colon_span)
381+ self . parse_generic_bounds_common ( AllowPlus :: Yes , colon_span)
363382 }
364383
365384 /// Parses bounds of a type parameter `BOUND + BOUND + ...`, possibly with trailing `+`.
366385 ///
367386 /// See `parse_generic_bound` for the `BOUND` grammar.
368387 fn parse_generic_bounds_common (
369388 & mut self ,
370- allow_plus : bool ,
389+ allow_plus : AllowPlus ,
371390 colon_span : Option < Span > ,
372391 ) -> PResult < ' a , GenericBounds > {
373392 let mut bounds = Vec :: new ( ) ;
@@ -377,7 +396,7 @@ impl<'a> Parser<'a> {
377396 Ok ( bound) => bounds. push ( bound) ,
378397 Err ( neg_sp) => negative_bounds. push ( neg_sp) ,
379398 }
380- if ! allow_plus || !self . eat_plus ( ) {
399+ if matches ! ( allow_plus, AllowPlus :: No ) || !self . eat_plus ( ) {
381400 break ;
382401 }
383402 }
0 commit comments