Skip to main content

rustc_parse/parser/
ty.rs

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