Skip to main content

rustc_parse/parser/
ty.rs

1use rustc_ast::token::{self, IdentIsRaw, MetaVarKind, Token, TokenKind};
2use rustc_ast::util::case::Case;
3use rustc_ast::{
4    self as ast, BoundAsyncness, BoundConstness, BoundPolarity, DUMMY_NODE_ID, FnPtrTy, FnRetTy,
5    GenericBound, GenericBounds, GenericParam, Generics, Lifetime, MacCall, MutTy, Mutability,
6    Pinnedness, PolyTraitRef, PreciseCapturingArg, TraitBoundModifiers, TraitObjectSyntax, Ty,
7    TyKind, UnsafeBinderTy,
8};
9use rustc_errors::{Applicability, Diag, E0516, PResult};
10use rustc_span::{ErrorGuaranteed, Ident, Span, kw, sym};
11use thin_vec::{ThinVec, thin_vec};
12
13use super::{Parser, PathStyle, SeqSep, TokenType, Trailing};
14use crate::diagnostics::{
15    self, DynAfterMut, ExpectedFnPathFoundFnKeyword, ExpectedMutOrConstInRawPointerType,
16    FnPtrWithGenerics, FnPtrWithGenericsSugg, HelpUseLatestEdition, InvalidCVariadicType,
17    InvalidDynKeyword, LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime, NestedCVariadicType,
18    ReturnTypesUseThinArrow,
19};
20use crate::parser::{FnContext, FnParseMode, FrontMatterParsingMode};
21use crate::{exp, maybe_recover_from_interpolated_ty_qpath};
22
23/// Signals whether parsing a type should allow `+`.
24///
25/// For example, let T be the type `impl Default + 'static`
26/// With `AllowPlus::Yes`, T will be parsed successfully
27/// With `AllowPlus::No`, parsing T will return a parse error
28#[derive(#[automatically_derived]
impl ::core::marker::Copy for AllowPlus { }Copy, #[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for AllowPlus { }
#[automatically_derived]
impl ::core::clone::Clone for AllowPlus {
    #[inline]
    fn clone(&self) -> AllowPlus { *self }
}Clone, #[automatically_derived]
impl ::core::marker::StructuralPartialEq for AllowPlus { }
#[automatically_derived]
impl ::core::cmp::PartialEq for AllowPlus {
    #[inline]
    fn eq(&self, other: &AllowPlus) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq)]
29pub(super) enum AllowPlus {
30    Yes,
31    No,
32}
33
34#[derive(#[automatically_derived]
impl ::core::marker::StructuralPartialEq for RecoverQPath { }
#[automatically_derived]
impl ::core::cmp::PartialEq for RecoverQPath {
    #[inline]
    fn eq(&self, other: &RecoverQPath) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq)]
35pub(super) enum RecoverQPath {
36    Yes,
37    No,
38}
39
40pub(super) enum RecoverQuestionMark {
41    Yes,
42    No,
43}
44
45/// Signals whether parsing a type should recover `->`.
46///
47/// More specifically, when parsing a function like:
48/// ```compile_fail
49/// fn foo() => u8 { 0 }
50/// fn bar(): u8 { 0 }
51/// ```
52/// The compiler will try to recover interpreting `foo() => u8` as `foo() -> u8` when calling
53/// `parse_ty` with anything except `RecoverReturnSign::No`, and it will try to recover `bar(): u8`
54/// as `bar() -> u8` when passing `RecoverReturnSign::Yes` to `parse_ty`
55#[derive(#[automatically_derived]
impl ::core::marker::Copy for RecoverReturnSign { }Copy, #[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for RecoverReturnSign { }
#[automatically_derived]
impl ::core::clone::Clone for RecoverReturnSign {
    #[inline]
    fn clone(&self) -> RecoverReturnSign { *self }
}Clone, #[automatically_derived]
impl ::core::marker::StructuralPartialEq for RecoverReturnSign { }
#[automatically_derived]
impl ::core::cmp::PartialEq for RecoverReturnSign {
    #[inline]
    fn eq(&self, other: &RecoverReturnSign) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq)]
56pub(super) enum RecoverReturnSign {
57    Yes,
58    OnlyFatArrow,
59    No,
60}
61
62impl RecoverReturnSign {
63    /// [RecoverReturnSign::Yes] allows for recovering `fn foo() => u8` and `fn foo(): u8`,
64    /// [RecoverReturnSign::OnlyFatArrow] allows for recovering only `fn foo() => u8` (recovering
65    /// colons can cause problems when parsing where clauses), and
66    /// [RecoverReturnSign::No] doesn't allow for any recovery of the return type arrow
67    fn can_recover(self, token: &TokenKind) -> bool {
68        match self {
69            Self::Yes => #[allow(non_exhaustive_omitted_patterns)] match token {
    token::FatArrow | token::Colon => true,
    _ => false,
}matches!(token, token::FatArrow | token::Colon),
70            Self::OnlyFatArrow => #[allow(non_exhaustive_omitted_patterns)] match token {
    token::FatArrow => true,
    _ => false,
}matches!(token, token::FatArrow),
71            Self::No => false,
72        }
73    }
74}
75
76// Is `...` (`CVarArgs`) legal at this level of type parsing?
77#[derive(#[automatically_derived]
impl ::core::marker::StructuralPartialEq for AllowCVariadic { }
#[automatically_derived]
impl ::core::cmp::PartialEq for AllowCVariadic {
    #[inline]
    fn eq(&self, other: &AllowCVariadic) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq)]
78enum AllowCVariadic {
79    Yes,
80    No,
81}
82
83/// Determine if the given token can begin a bound assuming it follows Rust 2015 identifier `dyn`.
84///
85/// In Rust 2015, `dyn` is a contextual keyword, not a full one.
86fn can_begin_dyn_bound_in_edition_2015(t: Token) -> bool {
87    if t.is_path_start() {
88        // In `dyn::x`, `dyn<X>` and `dyn<<X>::Y>`, `dyn` should (continue to) denote a regular path
89        // segment for backward compatibility. We make an exception for `dyn(X)` which used to be
90        // interpreted as a path with parenthesized generic arguments which can be semantically
91        // well-formed (consider: `use std::ops::Fn as dyn;`). Instead, we treat it as a trait
92        // object type whose first bound is parenthesized.
93        return t != token::PathSep && t != token::Lt && t != token::Shl;
94    }
95
96    // Contrary to `Parser::can_begin_bound`, `!`, `const`, `[` and `async` are deliberately not
97    // part of this list to contain the number of potential regressions esp. in MBE code.
98    // `const` and `[` would regress UI test `macro-dyn-const-2015.rs` and
99    // `!` would regress `dyn!(...)` macro calls in Rust 2015 for example.
100    t == token::OpenParen || t == token::Question || t.is_lifetime() || t.is_keyword(kw::For)
101}
102
103impl<'a> Parser<'a> {
104    /// Parses a type.
105    pub fn parse_ty(&mut self) -> PResult<'a, Box<Ty>> {
106        if self.token == token::DotDotDot {
107            // We special case this so that we don't talk about "nested C-variadics" in types.
108            // We still pass in `AllowCVariadic::No` so that `parse_ty_common` can complain about
109            // things like `Vec<...>`.
110            let span = self.token.span;
111            self.bump();
112            let kind = TyKind::Err(self.dcx().emit_err(InvalidCVariadicType { span }));
113            return Ok(self.mk_ty(span, kind));
114        }
115        self.parse_ty_common(
116            AllowPlus::Yes,
117            AllowCVariadic::No,
118            RecoverQPath::Yes,
119            RecoverReturnSign::Yes,
120            None,
121            RecoverQuestionMark::Yes,
122        )
123    }
124
125    pub(super) fn parse_ty_with_generics_recovery(
126        &mut self,
127        ty_params: &Generics,
128    ) -> PResult<'a, Box<Ty>> {
129        self.parse_ty_common(
130            AllowPlus::Yes,
131            AllowCVariadic::No,
132            RecoverQPath::Yes,
133            RecoverReturnSign::Yes,
134            Some(ty_params),
135            RecoverQuestionMark::Yes,
136        )
137    }
138
139    /// Parse a type suitable for a function or function pointer parameter.
140    /// The difference from `parse_ty` is that this version allows `...`
141    /// (`CVarArgs`) at the top level of the type.
142    pub(super) fn parse_ty_for_param(&mut self) -> PResult<'a, Box<Ty>> {
143        let ty = self.parse_ty_common(
144            AllowPlus::Yes,
145            AllowCVariadic::Yes,
146            RecoverQPath::Yes,
147            RecoverReturnSign::Yes,
148            None,
149            RecoverQuestionMark::Yes,
150        )?;
151
152        // Recover a trailing `= EXPR` if present.
153        if self.may_recover()
154            && self.check_noexpect(&token::Eq)
155            && self.look_ahead(1, |tok| tok.can_begin_expr())
156        {
157            let snapshot = self.create_snapshot_for_diagnostic();
158            self.bump();
159            let eq_span = self.prev_token.span;
160            match self.parse_expr() {
161                Ok(e) => {
162                    self.dcx()
163                        .struct_span_err(eq_span.to(e.span), "parameter defaults are not supported")
164                        .emit();
165                }
166                Err(diag) => {
167                    diag.cancel();
168                    self.restore_snapshot(snapshot);
169                }
170            }
171        }
172
173        Ok(ty)
174    }
175
176    /// Parses a type in restricted contexts where `+` is not permitted.
177    ///
178    /// Example 1: `&'a TYPE`
179    ///     `+` is prohibited to maintain operator priority (P(+) < P(&)).
180    /// Example 2: `value1 as TYPE + value2`
181    ///     `+` is prohibited to avoid interactions with expression grammar.
182    pub(super) fn parse_ty_no_plus(&mut self) -> PResult<'a, Box<Ty>> {
183        self.parse_ty_common(
184            AllowPlus::No,
185            AllowCVariadic::No,
186            RecoverQPath::Yes,
187            RecoverReturnSign::Yes,
188            None,
189            RecoverQuestionMark::Yes,
190        )
191    }
192
193    /// Parses a type following an `as` cast. Similar to `parse_ty_no_plus`, but signaling origin
194    /// for better diagnostics involving `?`.
195    pub(super) fn parse_as_cast_ty(&mut self) -> PResult<'a, Box<Ty>> {
196        self.parse_ty_common(
197            AllowPlus::No,
198            AllowCVariadic::No,
199            RecoverQPath::Yes,
200            RecoverReturnSign::Yes,
201            None,
202            RecoverQuestionMark::No,
203        )
204    }
205
206    pub(super) fn parse_ty_no_question_mark_recover(&mut self) -> PResult<'a, Box<Ty>> {
207        self.parse_ty_common(
208            AllowPlus::Yes,
209            AllowCVariadic::No,
210            RecoverQPath::Yes,
211            RecoverReturnSign::Yes,
212            None,
213            RecoverQuestionMark::No,
214        )
215    }
216
217    /// Parse a type without recovering `:` as `->` to avoid breaking code such
218    /// as `where fn() : for<'a>`.
219    pub(super) fn parse_ty_for_where_clause(&mut self) -> PResult<'a, Box<Ty>> {
220        self.parse_ty_common(
221            AllowPlus::Yes,
222            AllowCVariadic::No,
223            RecoverQPath::Yes,
224            RecoverReturnSign::OnlyFatArrow,
225            None,
226            RecoverQuestionMark::Yes,
227        )
228    }
229
230    /// Parses an optional return type `[ -> TY ]` in a function declaration.
231    pub(super) fn parse_ret_ty(
232        &mut self,
233        allow_plus: AllowPlus,
234        recover_qpath: RecoverQPath,
235        recover_return_sign: RecoverReturnSign,
236    ) -> PResult<'a, FnRetTy> {
237        let lo = self.prev_token.span;
238        Ok(if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::RArrow,
    token_type: crate::parser::token_type::TokenType::RArrow,
}exp!(RArrow)) {
239            // FIXME(Centril): Can we unconditionally `allow_plus`?
240            let ty = self.parse_ty_common(
241                allow_plus,
242                AllowCVariadic::No,
243                recover_qpath,
244                recover_return_sign,
245                None,
246                RecoverQuestionMark::Yes,
247            )?;
248            FnRetTy::Ty(ty)
249        } else if recover_return_sign.can_recover(&self.token.kind) {
250            // Don't `eat` to prevent `=>` from being added as an expected token which isn't
251            // actually expected and could only confuse users
252            self.bump();
253            self.dcx().emit_err(ReturnTypesUseThinArrow {
254                span: self.prev_token.span,
255                suggestion: lo.between(self.token.span),
256            });
257            let ty = self.parse_ty_common(
258                allow_plus,
259                AllowCVariadic::No,
260                recover_qpath,
261                recover_return_sign,
262                None,
263                RecoverQuestionMark::Yes,
264            )?;
265            FnRetTy::Ty(ty)
266        } else {
267            FnRetTy::Default(self.prev_token.span.shrink_to_hi())
268        })
269    }
270
271    fn parse_ty_common(
272        &mut self,
273        allow_plus: AllowPlus,
274        allow_c_variadic: AllowCVariadic,
275        recover_qpath: RecoverQPath,
276        recover_return_sign: RecoverReturnSign,
277        ty_generics: Option<&Generics>,
278        recover_question_mark: RecoverQuestionMark,
279    ) -> PResult<'a, Box<Ty>> {
280        let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes;
281        if allow_qpath_recovery && self.may_recover() &&
                let Some(mv_kind) = self.token.is_metavar_seq() &&
            let token::MetaVarKind::Ty { .. } = mv_kind &&
        self.check_noexpect_past_close_delim(&token::PathSep) {
    let ty =
        self.eat_metavar_seq(mv_kind,
                |this|
                    this.parse_ty_no_question_mark_recover()).expect("metavar seq ty");
    return self.maybe_recover_from_bad_qpath_stage_2(self.prev_token.span,
            ty);
};maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);
282
283        if let Some(ty) = self.eat_metavar_seq_with_matcher(
284            |mv_kind| #[allow(non_exhaustive_omitted_patterns)] match mv_kind {
    MetaVarKind::Ty { .. } => true,
    _ => false,
}matches!(mv_kind, MetaVarKind::Ty { .. }),
285            |this| this.parse_ty_no_question_mark_recover(),
286        ) {
287            return Ok(ty);
288        }
289
290        self.recover_from_outer_attributes("types")?;
291
292        let lo = self.token.span;
293        let mut impl_dyn_multi = false;
294        let kind = if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
295            self.parse_paren_start_ty(lo, allow_plus)?
296        } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Bang,
    token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) {
297            // Never type `!`
298            TyKind::Never
299        } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Star,
    token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star)) {
300            self.parse_ty_ptr()?
301        } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBracket,
    token_type: crate::parser::token_type::TokenType::OpenBracket,
}exp!(OpenBracket)) {
302            self.parse_array_or_slice_ty()?
303        } else if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::And,
    token_type: crate::parser::token_type::TokenType::And,
}exp!(And)) || self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::AndAnd,
    token_type: crate::parser::token_type::TokenType::AndAnd,
}exp!(AndAnd)) {
304            // Reference
305            self.expect_and()?;
306            self.parse_borrowed_pointee()?
307        } else if self.eat_keyword_noexpect(kw::Typeof) {
308            self.parse_typeof_ty(lo)?
309        } else if self.is_builtin() {
310            self.parse_builtin_ty()?
311        } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Underscore,
    token_type: crate::parser::token_type::TokenType::KwUnderscore,
}exp!(Underscore)) {
312            // A type to be inferred `_`
313            TyKind::Infer
314        } else if self.check_fn_front_matter(false, Case::Sensitive) {
315            // Function pointer type
316            self.parse_ty_fn_ptr(lo, ThinVec::new(), None, recover_return_sign)?
317        } else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::For,
    token_type: crate::parser::token_type::TokenType::KwFor,
}exp!(For)) {
318            // Function pointer type or bound list (trait object type) starting with a poly-trait.
319            //   `for<'lt> [unsafe] [extern "ABI"] fn (&'lt S) -> T`
320            //   `for<'lt> Trait1<'lt> + Trait2 + 'a`
321            let (bound_vars, _) = self.parse_higher_ranked_binder()?;
322            if self.check_fn_front_matter(false, Case::Sensitive) {
323                self.parse_ty_fn_ptr(
324                    lo,
325                    bound_vars,
326                    Some(self.prev_token.span.shrink_to_lo()),
327                    recover_return_sign,
328                )?
329            } else {
330                // Try to recover `for<'a> dyn Trait` or `for<'a> impl Trait`.
331                if self.may_recover()
332                    && (self.eat_keyword_noexpect(kw::Impl) || self.eat_keyword_noexpect(kw::Dyn))
333                {
334                    let kw = self.prev_token.ident().unwrap().0;
335                    let removal_span = kw.span.with_hi(self.token.span.lo());
336                    let path = self.parse_path(PathStyle::Type)?;
337                    let mut bounds = {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(GenericBound::Trait(PolyTraitRef::new(bound_vars, path,
                TraitBoundModifiers::NONE, lo.to(self.prev_token.span),
                ast::Parens::No)));
    vec
}thin_vec![GenericBound::Trait(PolyTraitRef::new(
338                        bound_vars,
339                        path,
340                        TraitBoundModifiers::NONE,
341                        lo.to(self.prev_token.span),
342                        ast::Parens::No,
343                    ))];
344                    if allow_plus == AllowPlus::Yes && self.check_plus() {
345                        self.eat_plus();
346                        bounds.append(&mut self.parse_generic_bounds()?);
347                    }
348                    self.dcx().emit_err(diagnostics::TransposeDynOrImpl {
349                        span: kw.span,
350                        kw: kw.name.as_str(),
351                        sugg: diagnostics::TransposeDynOrImplSugg {
352                            removal_span,
353                            insertion_span: lo.shrink_to_lo(),
354                            kw: kw.name.as_str(),
355                        },
356                    });
357                    match kw.name {
358                        kw::Dyn => TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn),
359                        kw::Impl => TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds),
360                        _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
361                    }
362                } else {
363                    let path = self.parse_path(PathStyle::Type)?;
364                    let parse_plus = allow_plus == AllowPlus::Yes && self.check_plus();
365                    self.finish_parsing_bare_trait_object_ty(
366                        bound_vars,
367                        path,
368                        lo,
369                        parse_plus,
370                        ast::Parens::No,
371                    )?
372                }
373            }
374        } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Impl,
    token_type: crate::parser::token_type::TokenType::KwImpl,
}exp!(Impl)) {
375            self.parse_impl_ty(&mut impl_dyn_multi)?
376        } else if self.is_explicit_dyn_type() {
377            self.parse_dyn_ty(&mut impl_dyn_multi)?
378        } else if self.eat_lt() {
379            // Qualified path
380            let (qself, path) = self.parse_qpath(PathStyle::Type)?;
381            TyKind::Path(Some(qself), path)
382        } else if (self.token.is_keyword(kw::Const) || self.token.is_keyword(kw::Mut))
383            && self.look_ahead(1, |t| *t == token::Star)
384        {
385            self.parse_ty_c_style_pointer()?
386        } else if self.check_path() {
387            self.parse_path_start_ty(lo, allow_plus, ty_generics)?
388        } else if self.can_begin_bound() {
389            self.parse_bare_trait_object_ty(lo, allow_plus)?
390        } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::DotDotDot,
    token_type: crate::parser::token_type::TokenType::DotDotDot,
}exp!(DotDotDot)) {
391            match allow_c_variadic {
392                AllowCVariadic::Yes => TyKind::CVarArgs,
393                AllowCVariadic::No => {
394                    // FIXME(c_variadic): Should we just allow `...` syntactically
395                    // anywhere in a type and use semantic restrictions instead?
396                    // NOTE: This may regress certain MBE calls if done incorrectly.
397                    let guar = self.dcx().emit_err(NestedCVariadicType { span: lo });
398                    TyKind::Err(guar)
399                }
400            }
401        } else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Unsafe,
    token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe))
402            && self.look_ahead(1, |tok| tok.kind == token::Lt)
403        {
404            self.parse_unsafe_binder_ty()?
405        } else {
406            let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected type, found {0}",
                super::token_descr(&self.token)))
    })format!("expected type, found {}", super::token_descr(&self.token));
407            let mut err = self.dcx().struct_span_err(lo, msg);
408            err.span_label(lo, "expected type");
409            return Err(err);
410        };
411
412        let span = lo.to(self.prev_token.span);
413        let mut ty = self.mk_ty(span, kind);
414
415        // Try to recover from use of `+` with incorrect priority.
416        match allow_plus {
417            AllowPlus::Yes => self.maybe_recover_from_bad_type_plus(&ty)?,
418            AllowPlus::No => self.maybe_report_ambiguous_plus(impl_dyn_multi, &ty),
419        }
420        if let RecoverQuestionMark::Yes = recover_question_mark {
421            ty = self.maybe_recover_from_question_mark(ty);
422        }
423        if allow_qpath_recovery { self.maybe_recover_from_bad_qpath(ty) } else { Ok(ty) }
424    }
425
426    fn parse_unsafe_binder_ty(&mut self) -> PResult<'a, TyKind> {
427        let lo = self.token.span;
428        if !self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
                kw: rustc_span::symbol::kw::Unsafe,
                token_type: crate::parser::token_type::TokenType::KwUnsafe,
            }) {
    ::core::panicking::panic("assertion failed: self.eat_keyword(exp!(Unsafe))")
};assert!(self.eat_keyword(exp!(Unsafe)));
429        self.expect_lt()?;
430        let generic_params = self.parse_generic_params()?;
431        self.expect_gt()?;
432        let inner_ty = self.parse_ty()?;
433        let span = lo.to(self.prev_token.span);
434        self.psess.gated_spans.gate(sym::unsafe_binders, span);
435
436        Ok(TyKind::UnsafeBinder(Box::new(UnsafeBinderTy { generic_params, inner_ty })))
437    }
438
439    /// Parse a type that begins with an opening parenthesis `(`.
440    ///
441    /// More specifically, it parses one of the following:
442    ///
443    /// 1. parenthesized type
444    /// 2. tuple type
445    /// 3. bare trait object type where the first trait bound is parenthesized
446    fn parse_paren_start_ty(&mut self, lo: Span, allow_plus: AllowPlus) -> PResult<'a, TyKind> {
447        let mut trailing_plus = false;
448        let (ts, trailing) = self.parse_paren_comma_seq(|p| {
449            let ty = p.parse_ty()?;
450            trailing_plus = p.prev_token == TokenKind::Plus;
451            Ok(ty)
452        })?;
453
454        if ts.len() == 1 && #[allow(non_exhaustive_omitted_patterns)] match trailing {
    Trailing::No => true,
    _ => false,
}matches!(trailing, Trailing::No) {
455            let ty = ts.into_iter().next().unwrap();
456
457            // Let's check if we actually have a bare trait object type where the first trait bound
458            // is parenthesized. That's the case if the parentheses are followed by a `+` and if
459            // what's contained between the parentheses resembles a *BareTraitBound*.
460            //
461            // For context, looking at bounds in general (see *Bound*), only trait bounds are
462            // allowed to be wrapped in parentheses, not however lifetime and use bounds.
463            let maybe_bounds = allow_plus == AllowPlus::Yes && self.token.is_like_plus();
464            match ty.kind {
465                // `"(" TypePath ")" "+"`
466                TyKind::Path(None, path) if maybe_bounds => self
467                    .finish_parsing_bare_trait_object_ty(
468                        ThinVec::new(),
469                        path,
470                        lo,
471                        true,
472                        ast::Parens::Yes,
473                    ),
474                // `"(" BareTraitBound\TypePath | UseBound ")" "+"`
475                //
476                // * FIXME: As alluded to above, only trait bounds are meant to allow parens.
477                //   Arguably, it's an accident that we're permitting *UseBound*s and thus types
478                //   like `(use<>)+`. Might need a T-lang FCP to change this.
479                // * We're checking `!trailing_plus` to prevent us from accepting code like
480                //   `(T+)+` or `('a+)+`.
481                // * While we could be looking at `('a)+` which we don't want to accept, we
482                //   know that the `parse_ty` above has already emitted an error since the
483                //   lifetime isn't immediately followed by a `+`.
484                TyKind::TraitObject(mut bounds, TraitObjectSyntax::None)
485                    if maybe_bounds && bounds.len() == 1 && !trailing_plus =>
486                {
487                    self.eat_plus();
488                    bounds.append(&mut self.parse_generic_bounds()?);
489                    Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None))
490                }
491                _ => Ok(TyKind::Paren(ty)),
492            }
493        } else {
494            Ok(TyKind::Tup(ts))
495        }
496    }
497
498    fn parse_bare_trait_object_ty(
499        &mut self,
500        lo: Span,
501        allow_plus: AllowPlus,
502    ) -> PResult<'a, TyKind> {
503        // A lifetime only begins a bare trait object type if it is followed by `+`!
504        if self.token.is_lifetime() && !self.look_ahead(1, |t| t.is_like_plus()) {
505            // In Rust 2021 and beyond, we assume that the user didn't intend to write a bare trait
506            // object type with a leading lifetime bound since that seems very unlikely given the
507            // fact that `dyn`-less trait objects are *semantically* invalid.
508            if self.psess.edition.at_least_rust_2021() {
509                let lt = self.expect_lifetime();
510                let mut err = self.dcx().struct_span_err(lo, "expected type, found lifetime");
511                err.span_label(lo, "expected type");
512                return Ok(match self.maybe_recover_ref_ty_no_leading_ampersand(lt, lo, err) {
513                    Ok(ref_ty) => ref_ty,
514                    Err(err) => TyKind::Err(err.emit()),
515                });
516            }
517
518            self.dcx().emit_err(NeedPlusAfterTraitObjectLifetime {
519                span: lo,
520                suggestion: lo.shrink_to_hi(),
521            });
522        }
523        Ok(TyKind::TraitObject(
524            self.parse_generic_bounds_common(allow_plus)?,
525            TraitObjectSyntax::None,
526        ))
527    }
528
529    fn maybe_recover_ref_ty_no_leading_ampersand<'cx>(
530        &mut self,
531        lt: Lifetime,
532        lo: Span,
533        mut err: Diag<'cx>,
534    ) -> Result<TyKind, Diag<'cx>> {
535        if !self.may_recover() {
536            return Err(err);
537        }
538        let snapshot = self.create_snapshot_for_diagnostic();
539        let mutbl = self.parse_mutability();
540        match self.parse_ty_no_plus() {
541            Ok(ty) => {
542                err.span_suggestion_verbose(
543                    lo.shrink_to_lo(),
544                    "you might have meant to write a reference type here",
545                    "&",
546                    Applicability::MaybeIncorrect,
547                );
548                err.emit();
549                Ok(TyKind::Ref(Some(lt), MutTy { ty, mutbl }))
550            }
551            Err(diag) => {
552                diag.cancel();
553                self.restore_snapshot(snapshot);
554                Err(err)
555            }
556        }
557    }
558
559    fn finish_parsing_bare_trait_object_ty(
560        &mut self,
561        generic_params: ThinVec<GenericParam>,
562        path: ast::Path,
563        lo: Span,
564        parse_plus: bool,
565        parens: ast::Parens,
566    ) -> PResult<'a, TyKind> {
567        let mut bounds = {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(GenericBound::Trait(PolyTraitRef::new(generic_params, path,
                TraitBoundModifiers::NONE, lo.to(self.prev_token.span),
                parens)));
    vec
}thin_vec![GenericBound::Trait(PolyTraitRef::new(
568            generic_params,
569            path,
570            TraitBoundModifiers::NONE,
571            lo.to(self.prev_token.span),
572            parens,
573        ))];
574        if parse_plus {
575            self.eat_plus();
576            bounds.append(&mut self.parse_generic_bounds()?);
577        }
578        Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None))
579    }
580
581    /// Parses a raw pointer with a C-style typo
582    fn parse_ty_c_style_pointer(&mut self) -> PResult<'a, TyKind> {
583        let kw_span = self.token.span;
584        let mutbl = self.parse_mut_or_const();
585
586        if let Some(mutbl) = mutbl
587            && self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Star,
    token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star))
588        {
589            let star_span = self.prev_token.span;
590
591            let mutability = match mutbl {
592                Mutability::Not => "const",
593                Mutability::Mut => "mut",
594            };
595
596            let ty = self.parse_ty_no_question_mark_recover()?;
597
598            self.dcx()
599                .struct_span_err(
600                    kw_span,
601                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("raw pointer types must be written as `*{0} T`",
                mutability))
    })format!("raw pointer types must be written as `*{mutability} T`"),
602                )
603                .with_multipart_suggestion(
604                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("put the `*` before `{0}`",
                mutability))
    })format!("put the `*` before `{mutability}`"),
605                    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(star_span, String::new()),
                (kw_span.shrink_to_lo(), "*".to_string())]))vec![(star_span, String::new()), (kw_span.shrink_to_lo(), "*".to_string())],
606                    Applicability::MachineApplicable,
607                )
608                .emit();
609
610            return Ok(TyKind::Ptr(MutTy { ty, mutbl }));
611        }
612        // This is unreachable because we always get into if above and return from it
613        {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("this could never happen")));
}unreachable!("this could never happen")
614    }
615
616    /// Parses a raw pointer type: `*[const | mut] $type`.
617    fn parse_ty_ptr(&mut self) -> PResult<'a, TyKind> {
618        let mutbl = self.parse_mut_or_const().unwrap_or_else(|| {
619            let span = self.prev_token.span;
620            self.dcx().emit_err(ExpectedMutOrConstInRawPointerType {
621                span,
622                after_asterisk: span.shrink_to_hi(),
623            });
624            Mutability::Not
625        });
626        let ty = self.parse_ty_no_plus()?;
627        Ok(TyKind::Ptr(MutTy { ty, mutbl }))
628    }
629
630    /// Parses an array (`[TYPE; EXPR]`) or slice (`[TYPE]`) type.
631    /// The opening `[` bracket is already eaten.
632    fn parse_array_or_slice_ty(&mut self) -> PResult<'a, TyKind> {
633        let elt_ty = self.parse_ty()?;
634
635        let ty = if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Semi,
    token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
636            let mut length = self.parse_expr_anon_const()?;
637
638            if let Err(e) = self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseBracket,
    token_type: crate::parser::token_type::TokenType::CloseBracket,
}exp!(CloseBracket)) {
639                // Try to recover from `X<Y, ...>` when `X::<Y, ...>` works
640                self.check_mistyped_turbofish_with_multiple_type_params(e, &mut length.value)?;
641                self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseBracket,
    token_type: crate::parser::token_type::TokenType::CloseBracket,
}exp!(CloseBracket))?;
642            }
643            TyKind::Array(elt_ty, length)
644        } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseBracket,
    token_type: crate::parser::token_type::TokenType::CloseBracket,
}exp!(CloseBracket)) {
645            TyKind::Slice(elt_ty)
646        } else {
647            self.maybe_recover_array_ty_without_semi(elt_ty)?
648        };
649
650        Ok(ty)
651    }
652
653    /// Recover from malformed array type syntax.
654    ///
655    /// This method attempts to recover from cases like:
656    /// - `[u8, 5]` → suggests using `;`, return a Array type
657    /// - `[u8 5]` → suggests using `;`, return a Array type
658    /// Consider to add more cases in the future.
659    fn maybe_recover_array_ty_without_semi(&mut self, elt_ty: Box<Ty>) -> PResult<'a, TyKind> {
660        let span = self.token.span;
661        let token_descr = super::token_descr(&self.token);
662        let mut err =
663            self.dcx().struct_span_err(span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected `;` or `]`, found {0}",
                token_descr))
    })format!("expected `;` or `]`, found {}", token_descr));
664        err.span_label(span, "expected `;` or `]`");
665
666        // If we cannot recover, return the error immediately.
667        if !self.may_recover() {
668            return Err(err);
669        }
670
671        let snapshot = self.create_snapshot_for_diagnostic();
672
673        // Consume common erroneous separators.
674        let hi = self.prev_token.span.hi();
675        _ = self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma)) || self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Colon,
    token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) || self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Star,
    token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star));
676        let suggestion_span = self.prev_token.span.with_lo(hi);
677
678        // FIXME(mgca): recovery is broken for `const {` args
679        // we first try to parse pattern like `[u8 5]`
680        let length = match self.parse_expr_anon_const() {
681            Ok(length) => length,
682            Err(e) => {
683                e.cancel();
684                self.restore_snapshot(snapshot);
685                return Err(err);
686            }
687        };
688
689        if let Err(e) = self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseBracket,
    token_type: crate::parser::token_type::TokenType::CloseBracket,
}exp!(CloseBracket)) {
690            e.cancel();
691            self.restore_snapshot(snapshot);
692            return Err(err);
693        }
694
695        err.span_suggestion_verbose(
696            suggestion_span,
697            "you might have meant to use `;` as the separator",
698            ";",
699            Applicability::MaybeIncorrect,
700        );
701        err.emit();
702        Ok(TyKind::Array(elt_ty, length))
703    }
704
705    fn parse_borrowed_pointee(&mut self) -> PResult<'a, TyKind> {
706        let and_span = self.prev_token.span;
707        let mut opt_lifetime = self.check_lifetime().then(|| self.expect_lifetime());
708        let (pinned, mut mutbl) = self.parse_pin_and_mut();
709        if self.token.is_lifetime() && mutbl == Mutability::Mut && opt_lifetime.is_none() {
710            // A lifetime is invalid here: it would be part of a bare trait bound, which requires
711            // it to be followed by a plus, but we disallow plus in the pointee type.
712            // So we can handle this case as an error here, and suggest `'a mut`.
713            // If there *is* a plus next though, handling the error later provides better suggestions
714            // (like adding parentheses)
715            if !self.look_ahead(1, |t| t.is_like_plus()) {
716                let lifetime_span = self.token.span;
717                let span = and_span.to(lifetime_span);
718
719                let (suggest_lifetime, snippet) =
720                    if let Ok(lifetime_src) = self.span_to_snippet(lifetime_span) {
721                        (Some(span), lifetime_src)
722                    } else {
723                        (None, String::new())
724                    };
725                self.dcx().emit_err(LifetimeAfterMut { span, suggest_lifetime, snippet });
726
727                opt_lifetime = Some(self.expect_lifetime());
728            }
729        } else if self.token.is_keyword(kw::Dyn)
730            && mutbl == Mutability::Not
731            && self.look_ahead(1, |t| t.is_keyword(kw::Mut))
732        {
733            // We have `&dyn mut ...`, which is invalid and should be `&mut dyn ...`.
734            let span = and_span.to(self.look_ahead(1, |t| t.span));
735            self.dcx().emit_err(DynAfterMut { span });
736
737            // Recovery
738            mutbl = Mutability::Mut;
739            let (dyn_tok, dyn_tok_sp) = (self.token, self.token_spacing);
740            self.bump();
741            self.bump_with((dyn_tok, dyn_tok_sp));
742        }
743        let ty = self.parse_ty_no_plus()?;
744        Ok(match pinned {
745            Pinnedness::Not => TyKind::Ref(opt_lifetime, MutTy { ty, mutbl }),
746            Pinnedness::Pinned => TyKind::PinnedRef(opt_lifetime, MutTy { ty, mutbl }),
747        })
748    }
749
750    /// Parse nothing, mutability or `pin` followed by "explicit" mutability.
751    ///
752    /// ```ebnf
753    /// PinAndMut = "pin" MutOrConst | "mut"
754    /// ```
755    pub(crate) fn parse_pin_and_mut(&mut self) -> (Pinnedness, Mutability) {
756        if self.token.is_keyword(kw::Pin) && self.look_ahead(1, Token::is_mutability) {
757            self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
758            if !self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
                kw: rustc_span::symbol::kw::Pin,
                token_type: crate::parser::token_type::TokenType::KwMut,
            }) {
    ::core::panicking::panic("assertion failed: self.eat_keyword(exp!(Pin))")
};assert!(self.eat_keyword(exp!(Pin)));
759            let mutbl = self.parse_mut_or_const().unwrap();
760            (Pinnedness::Pinned, mutbl)
761        } else {
762            (Pinnedness::Not, self.parse_mutability())
763        }
764    }
765
766    /// Parses the `typeof(EXPR)` for better diagnostics before returning
767    /// an error type.
768    fn parse_typeof_ty(&mut self, lo: Span) -> PResult<'a, TyKind> {
769        self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen))?;
770        let _expr = self.parse_expr_anon_const()?;
771        self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?;
772        let span = lo.to(self.prev_token.span);
773        let guar = self
774            .dcx()
775            .struct_span_err(span, "`typeof` is a reserved keyword but unimplemented")
776            .with_note("consider replacing `typeof(...)` with an actual type")
777            .with_code(E0516)
778            .emit();
779        Ok(TyKind::Err(guar))
780    }
781
782    fn parse_builtin_ty(&mut self) -> PResult<'a, TyKind> {
783        self.parse_builtin(|this, lo, ident| {
784            Ok(match ident.name {
785                sym::field_of => Some(this.parse_ty_field_of(lo)?),
786                _ => None,
787            })
788        })
789    }
790
791    pub(crate) fn parse_ty_field_of(&mut self, _lo: Span) -> PResult<'a, TyKind> {
792        let container = self.parse_ty()?;
793        self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma))?;
794
795        let fields = self.parse_floating_field_access()?;
796        let trailing_comma = self.eat_noexpect(&TokenKind::Comma);
797
798        if let Err(mut e) = self.expect_one_of(&[], &[crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen)]) {
799            if trailing_comma {
800                e.note("unexpected third argument to field_of");
801            } else {
802                e.note("field_of expects dot-separated field and variant names");
803            }
804            e.emit();
805        }
806
807        // Eat tokens until the macro call ends.
808        if self.may_recover() {
809            while !self.token.kind.is_close_delim_or_eof() {
810                self.bump();
811            }
812        }
813
814        match *fields {
815            [] => Err(self.dcx().struct_span_err(
816                self.token.span,
817                "`field_of!` expects dot-separated field and variant names",
818            )),
819            [field] => Ok(TyKind::FieldOf(container, None, field)),
820            [variant, field] => Ok(TyKind::FieldOf(container, Some(variant), field)),
821            _ => Err(self.dcx().struct_span_err(
822                fields.iter().map(|f| f.span).collect::<Vec<_>>(),
823                "`field_of!` only supports a single field or a variant with a field",
824            )),
825        }
826    }
827
828    /// Parses a function pointer type (`TyKind::FnPtr`).
829    /// ```ignore (illustrative)
830    ///    [unsafe] [extern "ABI"] fn (S) -> T
831    /// //  ^~~~~^          ^~~~^     ^~^    ^
832    /// //    |               |        |     |
833    /// //    |               |        |   Return type
834    /// // Function Style    ABI  Parameter types
835    /// ```
836    /// We actually parse `FnHeader FnDecl`, but we error on `const` and `async` qualifiers.
837    fn parse_ty_fn_ptr(
838        &mut self,
839        lo: Span,
840        mut params: ThinVec<GenericParam>,
841        param_insertion_point: Option<Span>,
842        recover_return_sign: RecoverReturnSign,
843    ) -> PResult<'a, TyKind> {
844        let inherited_vis = rustc_ast::Visibility {
845            span: rustc_span::DUMMY_SP,
846            kind: rustc_ast::VisibilityKind::Inherited,
847        };
848        let span_start = self.token.span;
849        let ast::FnHeader { ext, safety, .. } = self.parse_fn_front_matter(
850            &inherited_vis,
851            Case::Sensitive,
852            FrontMatterParsingMode::FunctionPtrType,
853        )?;
854        if self.may_recover() && self.token == TokenKind::Lt {
855            self.recover_fn_ptr_with_generics(lo, &mut params, param_insertion_point)?;
856        }
857        let mode = crate::parser::FnParseMode {
858            req_name: |_, _| false,
859            context: FnContext::FunctionPtrType,
860            req_body: false,
861        };
862        let decl = self.parse_fn_decl(&mode, AllowPlus::No, recover_return_sign)?;
863
864        let decl_span = span_start.to(self.prev_token.span);
865        Ok(TyKind::FnPtr(Box::new(FnPtrTy {
866            ext,
867            safety,
868            generic_params: params,
869            decl,
870            decl_span,
871        })))
872    }
873
874    /// Recover from function pointer types with a generic parameter list (e.g. `fn<'a>(&'a str)`).
875    fn recover_fn_ptr_with_generics(
876        &mut self,
877        lo: Span,
878        params: &mut ThinVec<GenericParam>,
879        param_insertion_point: Option<Span>,
880    ) -> PResult<'a, ()> {
881        let generics = self.parse_generics()?;
882        let arity = generics.params.len();
883
884        let mut lifetimes: ThinVec<_> = generics
885            .params
886            .into_iter()
887            .filter(|param| #[allow(non_exhaustive_omitted_patterns)] match param.kind {
    ast::GenericParamKind::Lifetime => true,
    _ => false,
}matches!(param.kind, ast::GenericParamKind::Lifetime))
888            .collect();
889
890        let sugg = if !lifetimes.is_empty() {
891            let snippet =
892                lifetimes.iter().map(|param| param.ident.as_str()).intersperse(", ").collect();
893
894            let (left, snippet) = if let Some(span) = param_insertion_point {
895                (span, if params.is_empty() { snippet } else { ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(", {0}", snippet))
    })format!(", {snippet}") })
896            } else {
897                (lo.shrink_to_lo(), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("for<{0}> ", snippet))
    })format!("for<{snippet}> "))
898            };
899
900            Some(FnPtrWithGenericsSugg {
901                left,
902                snippet,
903                right: generics.span,
904                arity,
905                for_param_list_exists: param_insertion_point.is_some(),
906            })
907        } else {
908            None
909        };
910
911        self.dcx().emit_err(FnPtrWithGenerics { span: generics.span, sugg });
912        params.append(&mut lifetimes);
913        Ok(())
914    }
915
916    /// Parses an `impl B0 + ... + Bn` type.
917    fn parse_impl_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> {
918        // If we encounter a type like `impl 'a Sized`, suggest `impl 'a + Sized`.
919        if self.token.is_lifetime()
920            && let Some(ident) = self.look_ahead(1, |t| t.non_reserved_ident())
921        {
922            self.dcx().emit_err(diagnostics::MissingPlusBounds {
923                span: self.token.span,
924                hi: self.token.span.shrink_to_hi(),
925                sym: ident.name,
926            });
927        }
928
929        // Always parse bounds greedily for better error recovery.
930        let bounds = self.parse_generic_bounds()?;
931
932        *impl_dyn_multi = bounds.len() > 1 || self.prev_token == TokenKind::Plus;
933
934        Ok(TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds))
935    }
936
937    /// Parse a use-bound aka precise capturing list.
938    ///
939    /// ```ebnf
940    /// UseBound = "use" "<" (PreciseCapture ("," PreciseCapture)* ","?)? ">"
941    /// PreciseCapture = "Self" | Ident | Lifetime
942    /// ```
943    fn parse_use_bound(&mut self, lo: Span, parens: ast::Parens) -> PResult<'a, GenericBound> {
944        self.expect_lt()?;
945        let (args, _, _) = self.parse_seq_to_before_tokens(
946            &[crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Gt,
    token_type: crate::parser::token_type::TokenType::Gt,
}exp!(Gt)],
947            &[&TokenKind::Ge, &TokenKind::Shr, &TokenKind::Shr],
948            SeqSep::trailing_allowed(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma)),
949            |self_| {
950                if self_.check_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::SelfUpper,
    token_type: crate::parser::token_type::TokenType::KwSelfUpper,
}exp!(SelfUpper)) {
951                    self_.bump();
952                    Ok(PreciseCapturingArg::Arg(
953                        ast::Path::from_ident(self_.prev_token.ident().unwrap().0),
954                        DUMMY_NODE_ID,
955                    ))
956                } else if self_.check_ident() {
957                    Ok(PreciseCapturingArg::Arg(
958                        ast::Path::from_ident(self_.parse_ident()?),
959                        DUMMY_NODE_ID,
960                    ))
961                } else if self_.check_lifetime() {
962                    Ok(PreciseCapturingArg::Lifetime(self_.expect_lifetime()))
963                } else {
964                    self_.unexpected_any()
965                }
966            },
967        )?;
968        self.expect_gt()?;
969
970        if let ast::Parens::Yes = parens {
971            self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?;
972            self.report_parenthesized_bound(lo, self.prev_token.span, "precise capturing lists");
973        }
974
975        Ok(GenericBound::Use(args, lo.to(self.prev_token.span)))
976    }
977
978    /// Is a `dyn B0 + ... + Bn` type allowed here?
979    fn is_explicit_dyn_type(&mut self) -> bool {
980        self.check_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Dyn,
    token_type: crate::parser::token_type::TokenType::KwDyn,
}exp!(Dyn))
981            && (self.token_uninterpolated_span().at_least_rust_2018()
982                || self.look_ahead(1, |&t| can_begin_dyn_bound_in_edition_2015(t)))
983    }
984
985    /// Parses a `dyn B0 + ... + Bn` type.
986    ///
987    /// Note that this does *not* parse bare trait objects.
988    fn parse_dyn_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> {
989        self.bump(); // `dyn`
990
991        // Always parse bounds greedily for better error recovery.
992        let bounds = self.parse_generic_bounds()?;
993        *impl_dyn_multi = bounds.len() > 1 || self.prev_token == TokenKind::Plus;
994
995        Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn))
996    }
997
998    /// Parse a type that begins with a path.
999    ///
1000    /// This can be:
1001    /// 1. a type macro, `mac!(...)`,
1002    /// 2. a bare trait object type, `B0 + ... + Bn`,
1003    /// 3. or a path, `path::to::MyType`.
1004    fn parse_path_start_ty(
1005        &mut self,
1006        lo: Span,
1007        allow_plus: AllowPlus,
1008        ty_generics: Option<&Generics>,
1009    ) -> PResult<'a, TyKind> {
1010        // Simple path
1011        let path = self.parse_path_inner(PathStyle::Type, ty_generics)?;
1012        if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Bang,
    token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) {
1013            // Macro invocation in type position
1014            Ok(TyKind::MacCall(Box::new(MacCall { path, args: self.parse_delim_args()? })))
1015        } else if allow_plus == AllowPlus::Yes && self.check_plus() {
1016            // `Trait1 + Trait2 + 'a`
1017            self.finish_parsing_bare_trait_object_ty(
1018                ThinVec::new(),
1019                path,
1020                lo,
1021                true,
1022                ast::Parens::No,
1023            )
1024        } else {
1025            // Just a type path.
1026            Ok(TyKind::Path(None, path))
1027        }
1028    }
1029
1030    pub(super) fn parse_generic_bounds(&mut self) -> PResult<'a, GenericBounds> {
1031        self.parse_generic_bounds_common(AllowPlus::Yes)
1032    }
1033
1034    /// Parse generic bounds.
1035    ///
1036    /// Only if `allow_plus` this parses a `+`-separated list of bounds (trailing `+` is admitted).
1037    /// Otherwise, this only parses a single bound or none.
1038    fn parse_generic_bounds_common(&mut self, allow_plus: AllowPlus) -> PResult<'a, GenericBounds> {
1039        let mut bounds = ThinVec::new();
1040
1041        // In addition to looping while we find generic bounds:
1042        // We continue even if we find a keyword. This is necessary for error recovery on,
1043        // for example, `impl fn()`. The only keyword that can go after generic bounds is
1044        // `where`, so stop if it's it.
1045        // We also continue if we find types (not traits), again for error recovery.
1046        while self.can_begin_bound()
1047            || (self.may_recover()
1048                && (self.token.can_begin_type()
1049                    || (self.token.is_reserved_ident() && !self.token.is_keyword(kw::Where))))
1050        {
1051            if self.token.is_keyword(kw::Dyn) && self.token.span.edition().at_least_rust_2018() {
1052                // Account for `&dyn Trait + dyn Other`.
1053                self.bump();
1054                self.dcx().emit_err(InvalidDynKeyword {
1055                    span: self.prev_token.span,
1056                    suggestion: self.prev_token.span.until(self.token.span),
1057                });
1058            }
1059            bounds.push(self.parse_generic_bound()?);
1060            if allow_plus == AllowPlus::No || !self.eat_plus() {
1061                break;
1062            }
1063        }
1064
1065        Ok(bounds)
1066    }
1067
1068    /// Can the current token begin a bound?
1069    fn can_begin_bound(&mut self) -> bool {
1070        // NOTE: Tokens `!`, `~`, `const` & `async` which represent the start of currently unstable
1071        //       trait bound modifiers are intentionally not included in `Token::can_begin_type` to
1072        //       avoid affecting stable macro matching behavior.
1073
1074        self.check_path()
1075            || self.check_lifetime()
1076            || self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Bang,
    token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang))
1077            || self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Question,
    token_type: crate::parser::token_type::TokenType::Question,
}exp!(Question))
1078            || self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Tilde,
    token_type: crate::parser::token_type::TokenType::Tilde,
}exp!(Tilde))
1079            || self.check_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::For,
    token_type: crate::parser::token_type::TokenType::KwFor,
}exp!(For))
1080            || self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen))
1081            || self.can_begin_maybe_const_bound()
1082            || self.check_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Const,
    token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const))
1083            || self.check_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Async,
    token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async))
1084            || self.check_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Use,
    token_type: crate::parser::token_type::TokenType::KwUse,
}exp!(Use))
1085    }
1086
1087    fn can_begin_maybe_const_bound(&mut self) -> bool {
1088        self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBracket,
    token_type: crate::parser::token_type::TokenType::OpenBracket,
}exp!(OpenBracket))
1089            && self.look_ahead(1, |t| t.is_keyword(kw::Const))
1090            && self.look_ahead(2, |t| *t == token::CloseBracket)
1091    }
1092
1093    /// Parse a bound.
1094    ///
1095    /// ```ebnf
1096    /// Bound = LifetimeBound | UseBound | TraitBound
1097    /// ```
1098    fn parse_generic_bound(&mut self) -> PResult<'a, GenericBound> {
1099        let leading_token = self.prev_token;
1100        let lo = self.token.span;
1101
1102        // We only admit parenthesized *trait* bounds. However, we want to gracefully recover from
1103        // other kinds of parenthesized bounds, so parse the opening parenthesis *here*.
1104        //
1105        // In the future we might want to lift this syntactic restriction and
1106        // introduce "`GenericBound::Paren(Box<GenericBound>)`".
1107        let parens = if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) { ast::Parens::Yes } else { ast::Parens::No };
1108
1109        if self.token.is_lifetime() {
1110            self.parse_lifetime_bound(lo, parens)
1111        } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Use,
    token_type: crate::parser::token_type::TokenType::KwUse,
}exp!(Use)) {
1112            self.parse_use_bound(lo, parens)
1113        } else {
1114            self.parse_trait_bound(lo, parens, &leading_token)
1115        }
1116    }
1117
1118    /// Parse a lifetime-bound aka outlives-bound.
1119    ///
1120    /// ```ebnf
1121    /// LifetimeBound = Lifetime
1122    /// ```
1123    fn parse_lifetime_bound(&mut self, lo: Span, parens: ast::Parens) -> PResult<'a, GenericBound> {
1124        let lt = self.expect_lifetime();
1125
1126        if let ast::Parens::Yes = parens {
1127            self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?;
1128            self.report_parenthesized_bound(lo, self.prev_token.span, "lifetime bounds");
1129        }
1130
1131        Ok(GenericBound::Outlives(lt))
1132    }
1133
1134    fn report_parenthesized_bound(&self, lo: Span, hi: Span, kind: &str) -> ErrorGuaranteed {
1135        let mut diag =
1136            self.dcx().struct_span_err(lo.to(hi), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} may not be parenthesized",
                kind))
    })format!("{kind} may not be parenthesized"));
1137        diag.multipart_suggestion(
1138            "remove the parentheses",
1139            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(lo, String::new()), (hi, String::new())]))vec![(lo, String::new()), (hi, String::new())],
1140            Applicability::MachineApplicable,
1141        );
1142        diag.emit()
1143    }
1144
1145    /// Emits an error if any trait bound modifiers were present.
1146    fn error_lt_bound_with_modifiers(
1147        &self,
1148        modifiers: TraitBoundModifiers,
1149        binder_span: Option<Span>,
1150    ) -> ErrorGuaranteed {
1151        let TraitBoundModifiers { constness, asyncness, polarity } = modifiers;
1152
1153        match constness {
1154            BoundConstness::Never => {}
1155            BoundConstness::Always(span) | BoundConstness::Maybe(span) => {
1156                return self.dcx().emit_err(diagnostics::ModifierLifetime {
1157                    span,
1158                    modifier: constness.as_str(),
1159                });
1160            }
1161        }
1162
1163        match polarity {
1164            BoundPolarity::Positive => {}
1165            BoundPolarity::Negative(span) | BoundPolarity::Maybe(span) => {
1166                return self
1167                    .dcx()
1168                    .emit_err(diagnostics::ModifierLifetime { span, modifier: polarity.as_str() });
1169            }
1170        }
1171
1172        match asyncness {
1173            BoundAsyncness::Normal => {}
1174            BoundAsyncness::Async(span) => {
1175                return self.dcx().emit_err(diagnostics::ModifierLifetime {
1176                    span,
1177                    modifier: asyncness.as_str(),
1178                });
1179            }
1180        }
1181
1182        if let Some(span) = binder_span {
1183            return self
1184                .dcx()
1185                .emit_err(diagnostics::ModifierLifetime { span, modifier: "for<...>" });
1186        }
1187
1188        {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("lifetime bound intercepted in `parse_generic_ty_bound` but no modifiers?")));
}unreachable!("lifetime bound intercepted in `parse_generic_ty_bound` but no modifiers?")
1189    }
1190
1191    /// Parses the modifiers that may precede a trait in a bound, e.g. `?Trait` or `[const] Trait`.
1192    ///
1193    /// If no modifiers are present, this does not consume any tokens.
1194    ///
1195    /// ```ebnf
1196    /// Constness = ("const" | "[" "const" "]")?
1197    /// Asyncness = "async"?
1198    /// Polarity = ("?" | "!")?
1199    /// ```
1200    ///
1201    /// See `parse_trait_bound` for more context.
1202    fn parse_trait_bound_modifiers(&mut self) -> PResult<'a, TraitBoundModifiers> {
1203        let modifier_lo = self.token.span;
1204        let constness = self.parse_bound_constness()?;
1205
1206        let asyncness = if self.token_uninterpolated_span().at_least_rust_2018()
1207            && self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Async,
    token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async))
1208        {
1209            self.psess.gated_spans.gate(sym::async_trait_bounds, self.prev_token.span);
1210            BoundAsyncness::Async(self.prev_token.span)
1211        } else if self.may_recover()
1212            && self.token_uninterpolated_span().is_rust_2015()
1213            && self.is_kw_followed_by_ident(kw::Async)
1214        {
1215            self.bump(); // eat `async`
1216            self.dcx().emit_err(diagnostics::AsyncBoundModifierIn2015 {
1217                span: self.prev_token.span,
1218                help: HelpUseLatestEdition::new(),
1219            });
1220            self.psess.gated_spans.gate(sym::async_trait_bounds, self.prev_token.span);
1221            BoundAsyncness::Async(self.prev_token.span)
1222        } else {
1223            BoundAsyncness::Normal
1224        };
1225        let modifier_hi = self.prev_token.span;
1226
1227        let polarity = if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Question,
    token_type: crate::parser::token_type::TokenType::Question,
}exp!(Question)) {
1228            BoundPolarity::Maybe(self.prev_token.span)
1229        } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Bang,
    token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) {
1230            self.psess.gated_spans.gate(sym::negative_bounds, self.prev_token.span);
1231            BoundPolarity::Negative(self.prev_token.span)
1232        } else {
1233            BoundPolarity::Positive
1234        };
1235
1236        // Enforce the mutual-exclusivity of `const`/`async` and `?`/`!`.
1237        match polarity {
1238            BoundPolarity::Positive => {
1239                // All trait bound modifiers allowed to combine with positive polarity
1240            }
1241            BoundPolarity::Maybe(polarity_span) | BoundPolarity::Negative(polarity_span) => {
1242                match (asyncness, constness) {
1243                    (BoundAsyncness::Normal, BoundConstness::Never) => {
1244                        // Ok, no modifiers.
1245                    }
1246                    (_, _) => {
1247                        let constness = constness.as_str();
1248                        let asyncness = asyncness.as_str();
1249                        let glue =
1250                            if !constness.is_empty() && !asyncness.is_empty() { " " } else { "" };
1251                        let modifiers_concatenated = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}{2}", constness, glue,
                asyncness))
    })format!("{constness}{glue}{asyncness}");
1252                        self.dcx().emit_err(diagnostics::PolarityAndModifiers {
1253                            polarity_span,
1254                            polarity: polarity.as_str(),
1255                            modifiers_span: modifier_lo.to(modifier_hi),
1256                            modifiers_concatenated,
1257                        });
1258                    }
1259                }
1260            }
1261        }
1262
1263        Ok(TraitBoundModifiers { constness, asyncness, polarity })
1264    }
1265
1266    pub fn parse_bound_constness(&mut self) -> PResult<'a, BoundConstness> {
1267        // FIXME(const_trait_impl): remove `~const` parser support once bootstrap has the new syntax
1268        // in rustfmt
1269        Ok(if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Tilde,
    token_type: crate::parser::token_type::TokenType::Tilde,
}exp!(Tilde)) {
1270            let tilde = self.prev_token.span;
1271            self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Const,
    token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const))?;
1272            let span = tilde.to(self.prev_token.span);
1273            self.psess.gated_spans.gate(sym::const_trait_impl, span);
1274            BoundConstness::Maybe(span)
1275        } else if self.can_begin_maybe_const_bound() {
1276            let start = self.token.span;
1277            self.bump();
1278            self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Const,
    token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const)).unwrap();
1279            self.bump();
1280            let span = start.to(self.prev_token.span);
1281            self.psess.gated_spans.gate(sym::const_trait_impl, span);
1282            BoundConstness::Maybe(span)
1283        } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Const,
    token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const)) {
1284            self.psess.gated_spans.gate(sym::const_trait_impl, self.prev_token.span);
1285            BoundConstness::Always(self.prev_token.span)
1286        } else {
1287            BoundConstness::Never
1288        })
1289    }
1290
1291    /// Parse a trait bound.
1292    ///
1293    /// ```ebnf
1294    /// TraitBound = BareTraitBound | "(" BareTraitBound ")"
1295    /// BareTraitBound =
1296    ///     (HigherRankedBinder Constness Asyncness | Polarity)
1297    ///     TypePath
1298    /// ```
1299    fn parse_trait_bound(
1300        &mut self,
1301        lo: Span,
1302        parens: ast::Parens,
1303        leading_token: &Token,
1304    ) -> PResult<'a, GenericBound> {
1305        let (mut bound_vars, binder_span) = self.parse_higher_ranked_binder()?;
1306
1307        let modifiers_lo = self.token.span;
1308        let modifiers = self.parse_trait_bound_modifiers()?;
1309        let modifiers_span = modifiers_lo.to(self.prev_token.span);
1310
1311        if let Some(binder_span) = binder_span {
1312            match modifiers.polarity {
1313                BoundPolarity::Negative(polarity_span) | BoundPolarity::Maybe(polarity_span) => {
1314                    self.dcx().emit_err(diagnostics::BinderAndPolarity {
1315                        binder_span,
1316                        polarity_span,
1317                        polarity: modifiers.polarity.as_str(),
1318                    });
1319                }
1320                BoundPolarity::Positive => {}
1321            }
1322        }
1323
1324        // Recover erroneous lifetime bound with modifiers or binder.
1325        // e.g. `T: for<'a> 'a` or `T: [const] 'a`.
1326        if self.token.is_lifetime() {
1327            let _: ErrorGuaranteed = self.error_lt_bound_with_modifiers(modifiers, binder_span);
1328            return self.parse_lifetime_bound(lo, parens);
1329        }
1330
1331        if let (more_bound_vars, Some(binder_span)) = self.parse_higher_ranked_binder()? {
1332            bound_vars.extend(more_bound_vars);
1333            self.dcx().emit_err(diagnostics::BinderBeforeModifiers { binder_span, modifiers_span });
1334        }
1335
1336        let mut path = if self.token.is_keyword(kw::Fn)
1337            && self.look_ahead(1, |t| *t == TokenKind::OpenParen)
1338            && let Some(path) = self.recover_path_from_fn()
1339        {
1340            path
1341        } else if !self.token.is_path_start() && self.token.can_begin_type() {
1342            let ty = self.parse_ty_no_plus()?;
1343            // Instead of finding a path (a trait), we found a type.
1344            let mut err = self.dcx().struct_span_err(ty.span, "expected a trait, found type");
1345
1346            // If we can recover, try to extract a path from the type. Note
1347            // that we do not use the try operator when parsing the type because
1348            // if it fails then we get a parser error which we don't want (we're trying
1349            // to recover from errors, not make more).
1350            let path = if self.may_recover() {
1351                let (span, message, sugg, path, applicability) = match &ty.kind {
1352                    TyKind::Ptr(..) | TyKind::Ref(..)
1353                        if let TyKind::Path(_, path) = &ty.peel_refs().kind =>
1354                    {
1355                        (
1356                            ty.span.until(path.span),
1357                            "consider removing the indirection",
1358                            "",
1359                            path,
1360                            Applicability::MaybeIncorrect,
1361                        )
1362                    }
1363                    TyKind::ImplTrait(_, bounds)
1364                        if let [GenericBound::Trait(tr, ..), ..] = bounds.as_slice() =>
1365                    {
1366                        (
1367                            ty.span.until(tr.span),
1368                            "use the trait bounds directly",
1369                            "",
1370                            &tr.trait_ref.path,
1371                            Applicability::MachineApplicable,
1372                        )
1373                    }
1374                    _ => return Err(err),
1375                };
1376
1377                err.span_suggestion_verbose(span, message, sugg, applicability);
1378
1379                path.clone()
1380            } else {
1381                return Err(err);
1382            };
1383
1384            err.emit();
1385
1386            path
1387        } else {
1388            self.parse_path(PathStyle::Type)?
1389        };
1390
1391        if self.may_recover() && self.token == TokenKind::OpenParen {
1392            self.recover_fn_trait_with_lifetime_params(&mut path, &mut bound_vars)?;
1393        }
1394
1395        if let ast::Parens::Yes = parens {
1396            // Someone has written something like `&dyn (Trait + Other)`. The correct code
1397            // would be `&(dyn Trait + Other)`
1398            if self.token.is_like_plus() && leading_token.is_keyword(kw::Dyn) {
1399                self.eat_plus();
1400                self.parse_generic_bounds()?;
1401                self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?;
1402
1403                self.dcx().emit_err(diagnostics::IncorrectParensTraitBounds {
1404                    span: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [lo, self.prev_token.span]))vec![lo, self.prev_token.span],
1405                    sugg: diagnostics::IncorrectParensTraitBoundsSugg {
1406                        wrong_span: leading_token.span.shrink_to_hi().to(lo),
1407                        new_span: leading_token.span.shrink_to_lo(),
1408                    },
1409                });
1410            } else {
1411                self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?;
1412            }
1413        }
1414
1415        let poly_trait =
1416            PolyTraitRef::new(bound_vars, path, modifiers, lo.to(self.prev_token.span), parens);
1417        Ok(GenericBound::Trait(poly_trait))
1418    }
1419
1420    // recovers a `Fn(..)` parenthesized-style path from `fn(..)`
1421    fn recover_path_from_fn(&mut self) -> Option<ast::Path> {
1422        let fn_token_span = self.token.span;
1423        self.bump();
1424        let args_lo = self.token.span;
1425        let snapshot = self.create_snapshot_for_diagnostic();
1426        let mode =
1427            FnParseMode { req_name: |_, _| false, context: FnContext::Free, req_body: false };
1428        match self.parse_fn_decl(&mode, AllowPlus::No, RecoverReturnSign::OnlyFatArrow) {
1429            Ok(decl) => {
1430                self.dcx().emit_err(ExpectedFnPathFoundFnKeyword { fn_token_span });
1431                Some(ast::Path {
1432                    span: fn_token_span.to(self.prev_token.span),
1433                    segments: {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(ast::PathSegment {
            ident: Ident::new(sym::Fn, fn_token_span),
            id: DUMMY_NODE_ID,
            args: Some(Box::new(ast::GenericArgs::Parenthesized(ast::ParenthesizedArgs {
                            span: args_lo.to(self.prev_token.span),
                            inputs: decl.inputs.iter().map(|a| a.clone()).collect(),
                            inputs_span: args_lo.until(decl.output.span()),
                            output: decl.output.clone(),
                        }))),
        });
    vec
}thin_vec![ast::PathSegment {
1434                        ident: Ident::new(sym::Fn, fn_token_span),
1435                        id: DUMMY_NODE_ID,
1436                        args: Some(Box::new(ast::GenericArgs::Parenthesized(
1437                            ast::ParenthesizedArgs {
1438                                span: args_lo.to(self.prev_token.span),
1439                                inputs: decl.inputs.iter().map(|a| a.clone()).collect(),
1440                                inputs_span: args_lo.until(decl.output.span()),
1441                                output: decl.output.clone(),
1442                            }
1443                        ))),
1444                    }],
1445                })
1446            }
1447            Err(diag) => {
1448                diag.cancel();
1449                self.restore_snapshot(snapshot);
1450                None
1451            }
1452        }
1453    }
1454
1455    /// Parse an optional higher-ranked binder.
1456    ///
1457    /// ```ebnf
1458    /// HigherRankedBinder = ("for" "<" GenericParams ">")?
1459    /// ```
1460    pub(super) fn parse_higher_ranked_binder(
1461        &mut self,
1462    ) -> PResult<'a, (ThinVec<GenericParam>, Option<Span>)> {
1463        if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::For,
    token_type: crate::parser::token_type::TokenType::KwFor,
}exp!(For)) {
1464            let lo = self.token.span;
1465            self.expect_lt()?;
1466            let params = self.parse_generic_params()?;
1467            self.expect_gt()?;
1468            // We rely on AST validation to rule out invalid cases: There must not be
1469            // type or const parameters, and parameters must not have bounds.
1470            Ok((params, Some(lo.to(self.prev_token.span))))
1471        } else {
1472            Ok((ThinVec::new(), None))
1473        }
1474    }
1475
1476    /// Recover from `Fn`-family traits (Fn, FnMut, FnOnce) with lifetime arguments
1477    /// (e.g. `FnOnce<'a>(&'a str) -> bool`). Up to generic arguments have already
1478    /// been eaten.
1479    fn recover_fn_trait_with_lifetime_params(
1480        &mut self,
1481        fn_path: &mut ast::Path,
1482        lifetime_defs: &mut ThinVec<GenericParam>,
1483    ) -> PResult<'a, ()> {
1484        let fn_path_segment = fn_path.segments.last_mut().unwrap();
1485        let generic_args = if let Some(p_args) = &fn_path_segment.args {
1486            *p_args.clone()
1487        } else {
1488            // Normally it wouldn't come here because the upstream should have parsed
1489            // generic parameters (otherwise it's impossible to call this function).
1490            return Ok(());
1491        };
1492        let lifetimes =
1493            if let ast::GenericArgs::AngleBracketed(ast::AngleBracketedArgs { span: _, args }) =
1494                &generic_args
1495            {
1496                args.into_iter()
1497                    .filter_map(|arg| {
1498                        if let ast::AngleBracketedArg::Arg(generic_arg) = arg
1499                            && let ast::GenericArg::Lifetime(lifetime) = generic_arg
1500                        {
1501                            Some(lifetime)
1502                        } else {
1503                            None
1504                        }
1505                    })
1506                    .collect()
1507            } else {
1508                Vec::new()
1509            };
1510        // Only try to recover if the trait has lifetime params.
1511        if lifetimes.is_empty() {
1512            return Ok(());
1513        }
1514
1515        let snapshot = if self.parsing_generics {
1516            // The snapshot is only relevant if we're parsing the generics of an `fn` to avoid
1517            // incorrect recovery.
1518            Some(self.create_snapshot_for_diagnostic())
1519        } else {
1520            None
1521        };
1522        // Parse `(T, U) -> R`.
1523        let inputs_lo = self.token.span;
1524        let mode =
1525            FnParseMode { req_name: |_, _| false, context: FnContext::Free, req_body: false };
1526        let inputs = match self.parse_fn_params(&mode) {
1527            Ok(params) => params,
1528            Err(err) => {
1529                if let Some(snapshot) = snapshot {
1530                    self.restore_snapshot(snapshot);
1531                    err.cancel();
1532                    return Ok(());
1533                } else {
1534                    return Err(err);
1535                }
1536            }
1537        };
1538        let inputs_span = inputs_lo.to(self.prev_token.span);
1539        let output = match self.parse_ret_ty(AllowPlus::No, RecoverQPath::No, RecoverReturnSign::No)
1540        {
1541            Ok(output) => output,
1542            Err(err) => {
1543                if let Some(snapshot) = snapshot {
1544                    self.restore_snapshot(snapshot);
1545                    err.cancel();
1546                    return Ok(());
1547                } else {
1548                    return Err(err);
1549                }
1550            }
1551        };
1552        let args = ast::ParenthesizedArgs {
1553            span: fn_path_segment.span().to(self.prev_token.span),
1554            inputs,
1555            inputs_span,
1556            output,
1557        }
1558        .into();
1559
1560        if let Some(snapshot) = snapshot
1561            && ![token::Comma, token::Gt, token::Plus].contains(&self.token.kind)
1562        {
1563            // We would expect another bound or the end of type params by now. Most likely we've
1564            // encountered a `(` *not* representing `Trait()`, but rather the start of the `fn`'s
1565            // argument list where the generic param list wasn't properly closed.
1566            self.restore_snapshot(snapshot);
1567            return Ok(());
1568        }
1569
1570        *fn_path_segment = ast::PathSegment {
1571            ident: fn_path_segment.ident,
1572            args: Some(args),
1573            id: ast::DUMMY_NODE_ID,
1574        };
1575
1576        // Convert parsed `<'a>` in `Fn<'a>` into `for<'a>`.
1577        let mut generic_params = lifetimes
1578            .iter()
1579            .map(|lt| GenericParam {
1580                id: lt.id,
1581                ident: lt.ident,
1582                attrs: ast::AttrVec::new(),
1583                bounds: ThinVec::new(),
1584                is_placeholder: false,
1585                kind: ast::GenericParamKind::Lifetime,
1586                colon_span: None,
1587            })
1588            .collect::<ThinVec<GenericParam>>();
1589        lifetime_defs.append(&mut generic_params);
1590
1591        let generic_args_span = generic_args.span();
1592        let snippet = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("for<{0}> ",
                lifetimes.iter().map(|lt|
                                lt.ident.as_str()).intersperse(", ").collect::<String>()))
    })format!(
1593            "for<{}> ",
1594            lifetimes.iter().map(|lt| lt.ident.as_str()).intersperse(", ").collect::<String>(),
1595        );
1596        let before_fn_path = fn_path.span.shrink_to_lo();
1597        self.dcx()
1598            .struct_span_err(generic_args_span, "`Fn` traits cannot take lifetime parameters")
1599            .with_multipart_suggestion(
1600                "consider using a higher-ranked trait bound instead",
1601                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(generic_args_span, "".to_owned()), (before_fn_path, snippet)]))vec![(generic_args_span, "".to_owned()), (before_fn_path, snippet)],
1602                Applicability::MaybeIncorrect,
1603            )
1604            .emit();
1605        Ok(())
1606    }
1607
1608    pub(super) fn check_lifetime(&mut self) -> bool {
1609        self.expected_token_types.insert(TokenType::Lifetime);
1610        self.token.is_lifetime()
1611    }
1612
1613    /// Parses a single lifetime `'a` or panics.
1614    pub(super) fn expect_lifetime(&mut self) -> Lifetime {
1615        if let Some((ident, is_raw)) = self.token.lifetime() {
1616            if is_raw == IdentIsRaw::No && ident.without_first_quote().is_reserved_lifetime() {
1617                self.dcx().emit_err(diagnostics::KeywordLifetime { span: ident.span });
1618            }
1619
1620            self.bump();
1621            Lifetime { ident, id: ast::DUMMY_NODE_ID }
1622        } else {
1623            self.dcx().span_bug(self.token.span, "not a lifetime")
1624        }
1625    }
1626
1627    pub(super) fn mk_ty(&self, span: Span, kind: TyKind) -> Box<Ty> {
1628        Box::new(Ty { kind, span, id: ast::DUMMY_NODE_ID })
1629    }
1630}