]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_parse/src/parser/ty.rs
Do not eagerly recover for bad impl-trait in macros
[rust.git] / compiler / rustc_parse / src / parser / ty.rs
1 use super::{Parser, PathStyle, TokenType};
2
3 use crate::errors::{
4     DynAfterMut, ExpectedFnPathFoundFnKeyword, ExpectedMutOrConstInRawPointerType,
5     FnPointerCannotBeAsync, FnPointerCannotBeConst, FnPtrWithGenerics, FnPtrWithGenericsSugg,
6     InvalidDynKeyword, LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime,
7     NegativeBoundsNotSupported, NegativeBoundsNotSupportedSugg, NestedCVariadicType,
8     ReturnTypesUseThinArrow,
9 };
10 use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
11
12 use ast::DUMMY_NODE_ID;
13 use rustc_ast::ptr::P;
14 use rustc_ast::token::{self, Delimiter, Token, TokenKind};
15 use rustc_ast::util::case::Case;
16 use rustc_ast::{
17     self as ast, BareFnTy, FnRetTy, GenericBound, GenericBounds, GenericParam, Generics, Lifetime,
18     MacCall, MutTy, Mutability, PolyTraitRef, TraitBoundModifier, TraitObjectSyntax, Ty, TyKind,
19 };
20 use rustc_errors::{Applicability, PResult};
21 use rustc_span::source_map::Span;
22 use rustc_span::symbol::{kw, sym, Ident};
23 use rustc_span::Symbol;
24 use thin_vec::thin_vec;
25
26 /// Any `?` or `~const` modifiers that appear at the start of a bound.
27 struct BoundModifiers {
28     /// `?Trait`.
29     maybe: Option<Span>,
30
31     /// `~const Trait`.
32     maybe_const: Option<Span>,
33 }
34
35 impl BoundModifiers {
36     fn to_trait_bound_modifier(&self) -> TraitBoundModifier {
37         match (self.maybe, self.maybe_const) {
38             (None, None) => TraitBoundModifier::None,
39             (Some(_), None) => TraitBoundModifier::Maybe,
40             (None, Some(_)) => TraitBoundModifier::MaybeConst,
41             (Some(_), Some(_)) => TraitBoundModifier::MaybeConstMaybe,
42         }
43     }
44 }
45
46 #[derive(Copy, Clone, PartialEq)]
47 pub(super) enum AllowPlus {
48     Yes,
49     No,
50 }
51
52 #[derive(PartialEq)]
53 pub(super) enum RecoverQPath {
54     Yes,
55     No,
56 }
57
58 pub(super) enum RecoverQuestionMark {
59     Yes,
60     No,
61 }
62
63 /// Signals whether parsing a type should recover `->`.
64 ///
65 /// More specifically, when parsing a function like:
66 /// ```compile_fail
67 /// fn foo() => u8 { 0 }
68 /// fn bar(): u8 { 0 }
69 /// ```
70 /// The compiler will try to recover interpreting `foo() => u8` as `foo() -> u8` when calling
71 /// `parse_ty` with anything except `RecoverReturnSign::No`, and it will try to recover `bar(): u8`
72 /// as `bar() -> u8` when passing `RecoverReturnSign::Yes` to `parse_ty`
73 #[derive(Copy, Clone, PartialEq)]
74 pub(super) enum RecoverReturnSign {
75     Yes,
76     OnlyFatArrow,
77     No,
78 }
79
80 impl RecoverReturnSign {
81     /// [RecoverReturnSign::Yes] allows for recovering `fn foo() => u8` and `fn foo(): u8`,
82     /// [RecoverReturnSign::OnlyFatArrow] allows for recovering only `fn foo() => u8` (recovering
83     /// colons can cause problems when parsing where clauses), and
84     /// [RecoverReturnSign::No] doesn't allow for any recovery of the return type arrow
85     fn can_recover(self, token: &TokenKind) -> bool {
86         match self {
87             Self::Yes => matches!(token, token::FatArrow | token::Colon),
88             Self::OnlyFatArrow => matches!(token, token::FatArrow),
89             Self::No => false,
90         }
91     }
92 }
93
94 // Is `...` (`CVarArgs`) legal at this level of type parsing?
95 #[derive(PartialEq)]
96 enum AllowCVariadic {
97     Yes,
98     No,
99 }
100
101 /// Returns `true` if `IDENT t` can start a type -- `IDENT::a::b`, `IDENT<u8, u8>`,
102 /// `IDENT<<u8 as Trait>::AssocTy>`.
103 ///
104 /// Types can also be of the form `IDENT(u8, u8) -> u8`, however this assumes
105 /// that `IDENT` is not the ident of a fn trait.
106 fn can_continue_type_after_non_fn_ident(t: &Token) -> bool {
107     t == &token::ModSep || t == &token::Lt || t == &token::BinOp(token::Shl)
108 }
109
110 impl<'a> Parser<'a> {
111     /// Parses a type.
112     pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> {
113         self.parse_ty_common(
114             AllowPlus::Yes,
115             AllowCVariadic::No,
116             RecoverQPath::Yes,
117             RecoverReturnSign::Yes,
118             None,
119             RecoverQuestionMark::Yes,
120         )
121     }
122
123     pub(super) fn parse_ty_with_generics_recovery(
124         &mut self,
125         ty_params: &Generics,
126     ) -> PResult<'a, P<Ty>> {
127         self.parse_ty_common(
128             AllowPlus::Yes,
129             AllowCVariadic::No,
130             RecoverQPath::Yes,
131             RecoverReturnSign::Yes,
132             Some(ty_params),
133             RecoverQuestionMark::Yes,
134         )
135     }
136
137     /// Parse a type suitable for a function or function pointer parameter.
138     /// The difference from `parse_ty` is that this version allows `...`
139     /// (`CVarArgs`) at the top level of the type.
140     pub(super) fn parse_ty_for_param(&mut self) -> PResult<'a, P<Ty>> {
141         self.parse_ty_common(
142             AllowPlus::Yes,
143             AllowCVariadic::Yes,
144             RecoverQPath::Yes,
145             RecoverReturnSign::Yes,
146             None,
147             RecoverQuestionMark::Yes,
148         )
149     }
150
151     /// Parses a type in restricted contexts where `+` is not permitted.
152     ///
153     /// Example 1: `&'a TYPE`
154     ///     `+` is prohibited to maintain operator priority (P(+) < P(&)).
155     /// Example 2: `value1 as TYPE + value2`
156     ///     `+` is prohibited to avoid interactions with expression grammar.
157     pub(super) fn parse_ty_no_plus(&mut self) -> PResult<'a, P<Ty>> {
158         self.parse_ty_common(
159             AllowPlus::No,
160             AllowCVariadic::No,
161             RecoverQPath::Yes,
162             RecoverReturnSign::Yes,
163             None,
164             RecoverQuestionMark::Yes,
165         )
166     }
167
168     /// Parses a type following an `as` cast. Similar to `parse_ty_no_plus`, but signaling origin
169     /// for better diagnostics involving `?`.
170     pub(super) fn parse_as_cast_ty(&mut self) -> PResult<'a, P<Ty>> {
171         self.parse_ty_common(
172             AllowPlus::No,
173             AllowCVariadic::No,
174             RecoverQPath::Yes,
175             RecoverReturnSign::Yes,
176             None,
177             RecoverQuestionMark::No,
178         )
179     }
180
181     pub(super) fn parse_no_question_mark_recover(&mut self) -> PResult<'a, P<Ty>> {
182         self.parse_ty_common(
183             AllowPlus::Yes,
184             AllowCVariadic::No,
185             RecoverQPath::Yes,
186             RecoverReturnSign::Yes,
187             None,
188             RecoverQuestionMark::No,
189         )
190     }
191
192     /// Parse a type without recovering `:` as `->` to avoid breaking code such as `where fn() : for<'a>`
193     pub(super) fn parse_ty_for_where_clause(&mut self) -> PResult<'a, P<Ty>> {
194         self.parse_ty_common(
195             AllowPlus::Yes,
196             AllowCVariadic::Yes,
197             RecoverQPath::Yes,
198             RecoverReturnSign::OnlyFatArrow,
199             None,
200             RecoverQuestionMark::Yes,
201         )
202     }
203
204     /// Parses an optional return type `[ -> TY ]` in a function declaration.
205     pub(super) fn parse_ret_ty(
206         &mut self,
207         allow_plus: AllowPlus,
208         recover_qpath: RecoverQPath,
209         recover_return_sign: RecoverReturnSign,
210     ) -> PResult<'a, FnRetTy> {
211         Ok(if self.eat(&token::RArrow) {
212             // FIXME(Centril): Can we unconditionally `allow_plus`?
213             let ty = self.parse_ty_common(
214                 allow_plus,
215                 AllowCVariadic::No,
216                 recover_qpath,
217                 recover_return_sign,
218                 None,
219                 RecoverQuestionMark::Yes,
220             )?;
221             FnRetTy::Ty(ty)
222         } else if recover_return_sign.can_recover(&self.token.kind) {
223             // Don't `eat` to prevent `=>` from being added as an expected token which isn't
224             // actually expected and could only confuse users
225             self.bump();
226             self.sess.emit_err(ReturnTypesUseThinArrow { span: self.prev_token.span });
227             let ty = self.parse_ty_common(
228                 allow_plus,
229                 AllowCVariadic::No,
230                 recover_qpath,
231                 recover_return_sign,
232                 None,
233                 RecoverQuestionMark::Yes,
234             )?;
235             FnRetTy::Ty(ty)
236         } else {
237             FnRetTy::Default(self.token.span.shrink_to_lo())
238         })
239     }
240
241     fn parse_ty_common(
242         &mut self,
243         allow_plus: AllowPlus,
244         allow_c_variadic: AllowCVariadic,
245         recover_qpath: RecoverQPath,
246         recover_return_sign: RecoverReturnSign,
247         ty_generics: Option<&Generics>,
248         recover_question_mark: RecoverQuestionMark,
249     ) -> PResult<'a, P<Ty>> {
250         let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes;
251         maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);
252         maybe_whole!(self, NtTy, |x| x);
253
254         let lo = self.token.span;
255         let mut impl_dyn_multi = false;
256         let kind = if self.check(&token::OpenDelim(Delimiter::Parenthesis)) {
257             self.parse_ty_tuple_or_parens(lo, allow_plus)?
258         } else if self.eat(&token::Not) {
259             // Never type `!`
260             TyKind::Never
261         } else if self.eat(&token::BinOp(token::Star)) {
262             self.parse_ty_ptr()?
263         } else if self.eat(&token::OpenDelim(Delimiter::Bracket)) {
264             self.parse_array_or_slice_ty()?
265         } else if self.check(&token::BinOp(token::And)) || self.check(&token::AndAnd) {
266             // Reference
267             self.expect_and()?;
268             self.parse_borrowed_pointee()?
269         } else if self.eat_keyword_noexpect(kw::Typeof) {
270             self.parse_typeof_ty()?
271         } else if self.eat_keyword(kw::Underscore) {
272             // A type to be inferred `_`
273             TyKind::Infer
274         } else if self.check_fn_front_matter(false, Case::Sensitive) {
275             // Function pointer type
276             self.parse_ty_bare_fn(lo, Vec::new(), None, recover_return_sign)?
277         } else if self.check_keyword(kw::For) {
278             // Function pointer type or bound list (trait object type) starting with a poly-trait.
279             //   `for<'lt> [unsafe] [extern "ABI"] fn (&'lt S) -> T`
280             //   `for<'lt> Trait1<'lt> + Trait2 + 'a`
281             let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
282             if self.check_fn_front_matter(false, Case::Sensitive) {
283                 self.parse_ty_bare_fn(
284                     lo,
285                     lifetime_defs,
286                     Some(self.prev_token.span.shrink_to_lo()),
287                     recover_return_sign,
288                 )?
289             } else {
290                 let path = self.parse_path(PathStyle::Type)?;
291                 let parse_plus = allow_plus == AllowPlus::Yes && self.check_plus();
292                 self.parse_remaining_bounds_path(lifetime_defs, path, lo, parse_plus)?
293             }
294         } else if self.eat_keyword(kw::Impl) {
295             self.parse_impl_ty(&mut impl_dyn_multi)?
296         } else if self.is_explicit_dyn_type() {
297             self.parse_dyn_ty(&mut impl_dyn_multi)?
298         } else if self.eat_lt() {
299             // Qualified path
300             let (qself, path) = self.parse_qpath(PathStyle::Type)?;
301             TyKind::Path(Some(qself), path)
302         } else if self.check_path() {
303             self.parse_path_start_ty(lo, allow_plus, ty_generics)?
304         } else if self.can_begin_bound() {
305             self.parse_bare_trait_object(lo, allow_plus)?
306         } else if self.eat(&token::DotDotDot) {
307             match allow_c_variadic {
308                 AllowCVariadic::Yes => TyKind::CVarArgs,
309                 AllowCVariadic::No => {
310                     // FIXME(Centril): Should we just allow `...` syntactically
311                     // anywhere in a type and use semantic restrictions instead?
312                     self.sess.emit_err(NestedCVariadicType { span: lo.to(self.prev_token.span) });
313                     TyKind::Err
314                 }
315             }
316         } else {
317             let msg = format!("expected type, found {}", super::token_descr(&self.token));
318             let mut err = self.struct_span_err(self.token.span, &msg);
319             err.span_label(self.token.span, "expected type");
320             self.maybe_annotate_with_ascription(&mut err, true);
321             return Err(err);
322         };
323
324         let span = lo.to(self.prev_token.span);
325         let mut ty = self.mk_ty(span, kind);
326
327         // Try to recover from use of `+` with incorrect priority.
328         match allow_plus {
329             AllowPlus::Yes => self.maybe_recover_from_bad_type_plus(&ty)?,
330             AllowPlus::No => self.maybe_report_ambiguous_plus(impl_dyn_multi, &ty),
331         }
332         if let RecoverQuestionMark::Yes = recover_question_mark {
333             ty = self.maybe_recover_from_question_mark(ty);
334         }
335         if allow_qpath_recovery { self.maybe_recover_from_bad_qpath(ty) } else { Ok(ty) }
336     }
337
338     /// Parses either:
339     /// - `(TYPE)`, a parenthesized type.
340     /// - `(TYPE,)`, a tuple with a single field of type TYPE.
341     fn parse_ty_tuple_or_parens(&mut self, lo: Span, allow_plus: AllowPlus) -> PResult<'a, TyKind> {
342         let mut trailing_plus = false;
343         let (ts, trailing) = self.parse_paren_comma_seq(|p| {
344             let ty = p.parse_ty()?;
345             trailing_plus = p.prev_token.kind == TokenKind::BinOp(token::Plus);
346             Ok(ty)
347         })?;
348
349         if ts.len() == 1 && !trailing {
350             let ty = ts.into_iter().next().unwrap().into_inner();
351             let maybe_bounds = allow_plus == AllowPlus::Yes && self.token.is_like_plus();
352             match ty.kind {
353                 // `(TY_BOUND_NOPAREN) + BOUND + ...`.
354                 TyKind::Path(None, path) if maybe_bounds => {
355                     self.parse_remaining_bounds_path(Vec::new(), path, lo, true)
356                 }
357                 TyKind::TraitObject(bounds, TraitObjectSyntax::None)
358                     if maybe_bounds && bounds.len() == 1 && !trailing_plus =>
359                 {
360                     self.parse_remaining_bounds(bounds, true)
361                 }
362                 // `(TYPE)`
363                 _ => Ok(TyKind::Paren(P(ty))),
364             }
365         } else {
366             Ok(TyKind::Tup(ts))
367         }
368     }
369
370     fn parse_bare_trait_object(&mut self, lo: Span, allow_plus: AllowPlus) -> PResult<'a, TyKind> {
371         let lt_no_plus = self.check_lifetime() && !self.look_ahead(1, |t| t.is_like_plus());
372         let bounds = self.parse_generic_bounds_common(allow_plus, None)?;
373         if lt_no_plus {
374             self.sess.emit_err(NeedPlusAfterTraitObjectLifetime { span: lo });
375         }
376         Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None))
377     }
378
379     fn parse_remaining_bounds_path(
380         &mut self,
381         generic_params: Vec<GenericParam>,
382         path: ast::Path,
383         lo: Span,
384         parse_plus: bool,
385     ) -> PResult<'a, TyKind> {
386         let poly_trait_ref = PolyTraitRef::new(generic_params, path, lo.to(self.prev_token.span));
387         let bounds = vec![GenericBound::Trait(poly_trait_ref, TraitBoundModifier::None)];
388         self.parse_remaining_bounds(bounds, parse_plus)
389     }
390
391     /// Parse the remainder of a bare trait object type given an already parsed list.
392     fn parse_remaining_bounds(
393         &mut self,
394         mut bounds: GenericBounds,
395         plus: bool,
396     ) -> PResult<'a, TyKind> {
397         if plus {
398             self.eat_plus(); // `+`, or `+=` gets split and `+` is discarded
399             bounds.append(&mut self.parse_generic_bounds(Some(self.prev_token.span))?);
400         }
401         Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None))
402     }
403
404     /// Parses a raw pointer type: `*[const | mut] $type`.
405     fn parse_ty_ptr(&mut self) -> PResult<'a, TyKind> {
406         let mutbl = self.parse_const_or_mut().unwrap_or_else(|| {
407             let span = self.prev_token.span;
408             self.sess.emit_err(ExpectedMutOrConstInRawPointerType {
409                 span,
410                 after_asterisk: span.shrink_to_hi(),
411             });
412             Mutability::Not
413         });
414         let ty = self.parse_ty_no_plus()?;
415         Ok(TyKind::Ptr(MutTy { ty, mutbl }))
416     }
417
418     /// Parses an array (`[TYPE; EXPR]`) or slice (`[TYPE]`) type.
419     /// The opening `[` bracket is already eaten.
420     fn parse_array_or_slice_ty(&mut self) -> PResult<'a, TyKind> {
421         let elt_ty = match self.parse_ty() {
422             Ok(ty) => ty,
423             Err(mut err)
424                 if self.look_ahead(1, |t| t.kind == token::CloseDelim(Delimiter::Bracket))
425                     | self.look_ahead(1, |t| t.kind == token::Semi) =>
426             {
427                 // Recover from `[LIT; EXPR]` and `[LIT]`
428                 self.bump();
429                 err.emit();
430                 self.mk_ty(self.prev_token.span, TyKind::Err)
431             }
432             Err(err) => return Err(err),
433         };
434
435         let ty = if self.eat(&token::Semi) {
436             let mut length = self.parse_anon_const_expr()?;
437             if let Err(e) = self.expect(&token::CloseDelim(Delimiter::Bracket)) {
438                 // Try to recover from `X<Y, ...>` when `X::<Y, ...>` works
439                 self.check_mistyped_turbofish_with_multiple_type_params(e, &mut length.value)?;
440                 self.expect(&token::CloseDelim(Delimiter::Bracket))?;
441             }
442             TyKind::Array(elt_ty, length)
443         } else {
444             self.expect(&token::CloseDelim(Delimiter::Bracket))?;
445             TyKind::Slice(elt_ty)
446         };
447
448         Ok(ty)
449     }
450
451     fn parse_borrowed_pointee(&mut self) -> PResult<'a, TyKind> {
452         let and_span = self.prev_token.span;
453         let mut opt_lifetime =
454             if self.check_lifetime() { Some(self.expect_lifetime()) } else { None };
455         let mut mutbl = self.parse_mutability();
456         if self.token.is_lifetime() && mutbl == Mutability::Mut && opt_lifetime.is_none() {
457             // A lifetime is invalid here: it would be part of a bare trait bound, which requires
458             // it to be followed by a plus, but we disallow plus in the pointee type.
459             // So we can handle this case as an error here, and suggest `'a mut`.
460             // If there *is* a plus next though, handling the error later provides better suggestions
461             // (like adding parentheses)
462             if !self.look_ahead(1, |t| t.is_like_plus()) {
463                 let lifetime_span = self.token.span;
464                 let span = and_span.to(lifetime_span);
465
466                 let (suggest_lifetime, snippet) =
467                     if let Ok(lifetime_src) = self.span_to_snippet(lifetime_span) {
468                         (Some(span), lifetime_src)
469                     } else {
470                         (None, String::new())
471                     };
472                 self.sess.emit_err(LifetimeAfterMut { span, suggest_lifetime, snippet });
473
474                 opt_lifetime = Some(self.expect_lifetime());
475             }
476         } else if self.token.is_keyword(kw::Dyn)
477             && mutbl == Mutability::Not
478             && self.look_ahead(1, |t| t.is_keyword(kw::Mut))
479         {
480             // We have `&dyn mut ...`, which is invalid and should be `&mut dyn ...`.
481             let span = and_span.to(self.look_ahead(1, |t| t.span));
482             self.sess.emit_err(DynAfterMut { span });
483
484             // Recovery
485             mutbl = Mutability::Mut;
486             let (dyn_tok, dyn_tok_sp) = (self.token.clone(), self.token_spacing);
487             self.bump();
488             self.bump_with((dyn_tok, dyn_tok_sp));
489         }
490         let ty = self.parse_ty_no_plus()?;
491         Ok(TyKind::Ref(opt_lifetime, MutTy { ty, mutbl }))
492     }
493
494     // Parses the `typeof(EXPR)`.
495     // To avoid ambiguity, the type is surrounded by parentheses.
496     fn parse_typeof_ty(&mut self) -> PResult<'a, TyKind> {
497         self.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
498         let expr = self.parse_anon_const_expr()?;
499         self.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
500         Ok(TyKind::Typeof(expr))
501     }
502
503     /// Parses a function pointer type (`TyKind::BareFn`).
504     /// ```ignore (illustrative)
505     ///    [unsafe] [extern "ABI"] fn (S) -> T
506     /// //  ^~~~~^          ^~~~^     ^~^    ^
507     /// //    |               |        |     |
508     /// //    |               |        |   Return type
509     /// // Function Style    ABI  Parameter types
510     /// ```
511     /// We actually parse `FnHeader FnDecl`, but we error on `const` and `async` qualifiers.
512     fn parse_ty_bare_fn(
513         &mut self,
514         lo: Span,
515         mut params: Vec<GenericParam>,
516         param_insertion_point: Option<Span>,
517         recover_return_sign: RecoverReturnSign,
518     ) -> PResult<'a, TyKind> {
519         let inherited_vis = rustc_ast::Visibility {
520             span: rustc_span::DUMMY_SP,
521             kind: rustc_ast::VisibilityKind::Inherited,
522             tokens: None,
523         };
524         let span_start = self.token.span;
525         let ast::FnHeader { ext, unsafety, constness, asyncness } =
526             self.parse_fn_front_matter(&inherited_vis, Case::Sensitive)?;
527         if self.may_recover() && self.token.kind == TokenKind::Lt {
528             self.recover_fn_ptr_with_generics(lo, &mut params, param_insertion_point)?;
529         }
530         let decl = self.parse_fn_decl(|_| false, AllowPlus::No, recover_return_sign)?;
531         let whole_span = lo.to(self.prev_token.span);
532         if let ast::Const::Yes(span) = constness {
533             // If we ever start to allow `const fn()`, then update
534             // feature gating for `#![feature(const_extern_fn)]` to
535             // cover it.
536             self.sess.emit_err(FnPointerCannotBeConst { span: whole_span, qualifier: span });
537         }
538         if let ast::Async::Yes { span, .. } = asyncness {
539             self.sess.emit_err(FnPointerCannotBeAsync { span: whole_span, qualifier: span });
540         }
541         let decl_span = span_start.to(self.token.span);
542         Ok(TyKind::BareFn(P(BareFnTy { ext, unsafety, generic_params: params, decl, decl_span })))
543     }
544
545     /// Recover from function pointer types with a generic parameter list (e.g. `fn<'a>(&'a str)`).
546     fn recover_fn_ptr_with_generics(
547         &mut self,
548         lo: Span,
549         params: &mut Vec<GenericParam>,
550         param_insertion_point: Option<Span>,
551     ) -> PResult<'a, ()> {
552         let generics = self.parse_generics()?;
553         let arity = generics.params.len();
554
555         let mut lifetimes: Vec<_> = generics
556             .params
557             .into_iter()
558             .filter(|param| matches!(param.kind, ast::GenericParamKind::Lifetime))
559             .collect();
560
561         let sugg = if !lifetimes.is_empty() {
562             let snippet =
563                 lifetimes.iter().map(|param| param.ident.as_str()).intersperse(", ").collect();
564
565             let (left, snippet) = if let Some(span) = param_insertion_point {
566                 (span, if params.is_empty() { snippet } else { format!(", {snippet}") })
567             } else {
568                 (lo.shrink_to_lo(), format!("for<{snippet}> "))
569             };
570
571             Some(FnPtrWithGenericsSugg {
572                 left,
573                 snippet,
574                 right: generics.span,
575                 arity,
576                 for_param_list_exists: param_insertion_point.is_some(),
577             })
578         } else {
579             None
580         };
581
582         self.sess.emit_err(FnPtrWithGenerics { span: generics.span, sugg });
583         params.append(&mut lifetimes);
584         Ok(())
585     }
586
587     /// Parses an `impl B0 + ... + Bn` type.
588     fn parse_impl_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> {
589         // Always parse bounds greedily for better error recovery.
590         if self.token.is_lifetime() {
591             self.look_ahead(1, |t| {
592                 if let token::Ident(symname, _) = t.kind {
593                     // parse pattern with "'a Sized" we're supposed to give suggestion like
594                     // "'a + Sized"
595                     self.struct_span_err(
596                         self.token.span,
597                         &format!("expected `+` between lifetime and {}", symname),
598                     )
599                     .span_suggestion_verbose(
600                         self.token.span.shrink_to_hi(),
601                         "add `+`",
602                         " +",
603                         Applicability::MaybeIncorrect,
604                     )
605                     .emit();
606                 }
607             })
608         }
609         let bounds = self.parse_generic_bounds(None)?;
610         *impl_dyn_multi = bounds.len() > 1 || self.prev_token.kind == TokenKind::BinOp(token::Plus);
611         Ok(TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds))
612     }
613
614     /// Is a `dyn B0 + ... + Bn` type allowed here?
615     fn is_explicit_dyn_type(&mut self) -> bool {
616         self.check_keyword(kw::Dyn)
617             && (self.token.uninterpolated_span().rust_2018()
618                 || self.look_ahead(1, |t| {
619                     (t.can_begin_bound() || t.kind == TokenKind::BinOp(token::Star))
620                         && !can_continue_type_after_non_fn_ident(t)
621                 }))
622     }
623
624     /// Parses a `dyn B0 + ... + Bn` type.
625     ///
626     /// Note that this does *not* parse bare trait objects.
627     fn parse_dyn_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> {
628         self.bump(); // `dyn`
629
630         // parse dyn* types
631         let syntax = if self.eat(&TokenKind::BinOp(token::Star)) {
632             TraitObjectSyntax::DynStar
633         } else {
634             TraitObjectSyntax::Dyn
635         };
636
637         // Always parse bounds greedily for better error recovery.
638         let bounds = self.parse_generic_bounds(None)?;
639         *impl_dyn_multi = bounds.len() > 1 || self.prev_token.kind == TokenKind::BinOp(token::Plus);
640         Ok(TyKind::TraitObject(bounds, syntax))
641     }
642
643     /// Parses a type starting with a path.
644     ///
645     /// This can be:
646     /// 1. a type macro, `mac!(...)`,
647     /// 2. a bare trait object, `B0 + ... + Bn`,
648     /// 3. or a path, `path::to::MyType`.
649     fn parse_path_start_ty(
650         &mut self,
651         lo: Span,
652         allow_plus: AllowPlus,
653         ty_generics: Option<&Generics>,
654     ) -> PResult<'a, TyKind> {
655         // Simple path
656         let path = self.parse_path_inner(PathStyle::Type, ty_generics)?;
657         if self.eat(&token::Not) {
658             // Macro invocation in type position
659             Ok(TyKind::MacCall(P(MacCall {
660                 path,
661                 args: self.parse_delim_args()?,
662                 prior_type_ascription: self.last_type_ascription,
663             })))
664         } else if allow_plus == AllowPlus::Yes && self.check_plus() {
665             // `Trait1 + Trait2 + 'a`
666             self.parse_remaining_bounds_path(Vec::new(), path, lo, true)
667         } else {
668             // Just a type path.
669             Ok(TyKind::Path(None, path))
670         }
671     }
672
673     pub(super) fn parse_generic_bounds(
674         &mut self,
675         colon_span: Option<Span>,
676     ) -> PResult<'a, GenericBounds> {
677         self.parse_generic_bounds_common(AllowPlus::Yes, colon_span)
678     }
679
680     /// Parses bounds of a type parameter `BOUND + BOUND + ...`, possibly with trailing `+`.
681     ///
682     /// See `parse_generic_bound` for the `BOUND` grammar.
683     fn parse_generic_bounds_common(
684         &mut self,
685         allow_plus: AllowPlus,
686         colon_span: Option<Span>,
687     ) -> PResult<'a, GenericBounds> {
688         let mut bounds = Vec::new();
689         let mut negative_bounds = Vec::new();
690
691         // In addition to looping while we find generic bounds:
692         // We continue even if we find a keyword. This is necessary for error recovery on,
693         // for example, `impl fn()`. The only keyword that can go after generic bounds is
694         // `where`, so stop if it's it.
695         // We also continue if we find types (not traits), again for error recovery.
696         while self.can_begin_bound()
697             || (self.may_recover()
698                 && (self.token.can_begin_type()
699                     || (self.token.is_reserved_ident() && !self.token.is_keyword(kw::Where))))
700         {
701             if self.token.is_keyword(kw::Dyn) {
702                 // Account for `&dyn Trait + dyn Other`.
703                 self.sess.emit_err(InvalidDynKeyword { span: self.token.span });
704                 self.bump();
705             }
706             match self.parse_generic_bound()? {
707                 Ok(bound) => bounds.push(bound),
708                 Err(neg_sp) => negative_bounds.push(neg_sp),
709             }
710             if allow_plus == AllowPlus::No || !self.eat_plus() {
711                 break;
712             }
713         }
714
715         if !negative_bounds.is_empty() {
716             self.error_negative_bounds(colon_span, &bounds, negative_bounds);
717         }
718
719         Ok(bounds)
720     }
721
722     /// Can the current token begin a bound?
723     fn can_begin_bound(&mut self) -> bool {
724         // This needs to be synchronized with `TokenKind::can_begin_bound`.
725         self.check_path()
726         || self.check_lifetime()
727         || self.check(&token::Not) // Used for error reporting only.
728         || self.check(&token::Question)
729         || self.check(&token::Tilde)
730         || self.check_keyword(kw::For)
731         || self.check(&token::OpenDelim(Delimiter::Parenthesis))
732     }
733
734     fn error_negative_bounds(
735         &self,
736         colon_span: Option<Span>,
737         bounds: &[GenericBound],
738         negative_bounds: Vec<Span>,
739     ) {
740         let sub = if let Some(bound_list) = colon_span {
741             let bound_list = bound_list.to(self.prev_token.span);
742             let mut new_bound_list = String::new();
743             if !bounds.is_empty() {
744                 let mut snippets = bounds.iter().map(|bound| self.span_to_snippet(bound.span()));
745                 while let Some(Ok(snippet)) = snippets.next() {
746                     new_bound_list.push_str(" + ");
747                     new_bound_list.push_str(&snippet);
748                 }
749                 new_bound_list = new_bound_list.replacen(" +", ":", 1);
750             }
751
752             Some(NegativeBoundsNotSupportedSugg {
753                 bound_list,
754                 num_bounds: negative_bounds.len(),
755                 fixed: new_bound_list,
756             })
757         } else {
758             None
759         };
760
761         let last_span = *negative_bounds.last().expect("no negative bounds, but still error?");
762         self.sess.emit_err(NegativeBoundsNotSupported { negative_bounds, last_span, sub });
763     }
764
765     /// Parses a bound according to the grammar:
766     /// ```ebnf
767     /// BOUND = TY_BOUND | LT_BOUND
768     /// ```
769     fn parse_generic_bound(&mut self) -> PResult<'a, Result<GenericBound, Span>> {
770         let anchor_lo = self.prev_token.span;
771         let lo = self.token.span;
772         let has_parens = self.eat(&token::OpenDelim(Delimiter::Parenthesis));
773         let inner_lo = self.token.span;
774         let is_negative = self.eat(&token::Not);
775
776         let modifiers = self.parse_ty_bound_modifiers()?;
777         let bound = if self.token.is_lifetime() {
778             self.error_lt_bound_with_modifiers(modifiers);
779             self.parse_generic_lt_bound(lo, inner_lo, has_parens)?
780         } else {
781             self.parse_generic_ty_bound(lo, has_parens, modifiers)?
782         };
783
784         Ok(if is_negative { Err(anchor_lo.to(self.prev_token.span)) } else { Ok(bound) })
785     }
786
787     /// Parses a lifetime ("outlives") bound, e.g. `'a`, according to:
788     /// ```ebnf
789     /// LT_BOUND = LIFETIME
790     /// ```
791     fn parse_generic_lt_bound(
792         &mut self,
793         lo: Span,
794         inner_lo: Span,
795         has_parens: bool,
796     ) -> PResult<'a, GenericBound> {
797         let bound = GenericBound::Outlives(self.expect_lifetime());
798         if has_parens {
799             // FIXME(Centril): Consider not erroring here and accepting `('lt)` instead,
800             // possibly introducing `GenericBound::Paren(P<GenericBound>)`?
801             self.recover_paren_lifetime(lo, inner_lo)?;
802         }
803         Ok(bound)
804     }
805
806     /// Emits an error if any trait bound modifiers were present.
807     fn error_lt_bound_with_modifiers(&self, modifiers: BoundModifiers) {
808         if let Some(span) = modifiers.maybe_const {
809             self.struct_span_err(
810                 span,
811                 "`~const` may only modify trait bounds, not lifetime bounds",
812             )
813             .emit();
814         }
815
816         if let Some(span) = modifiers.maybe {
817             self.struct_span_err(span, "`?` may only modify trait bounds, not lifetime bounds")
818                 .emit();
819         }
820     }
821
822     /// Recover on `('lifetime)` with `(` already eaten.
823     fn recover_paren_lifetime(&mut self, lo: Span, inner_lo: Span) -> PResult<'a, ()> {
824         let inner_span = inner_lo.to(self.prev_token.span);
825         self.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
826         let mut err = self.struct_span_err(
827             lo.to(self.prev_token.span),
828             "parenthesized lifetime bounds are not supported",
829         );
830         if let Ok(snippet) = self.span_to_snippet(inner_span) {
831             err.span_suggestion_short(
832                 lo.to(self.prev_token.span),
833                 "remove the parentheses",
834                 snippet,
835                 Applicability::MachineApplicable,
836             );
837         }
838         err.emit();
839         Ok(())
840     }
841
842     /// Parses the modifiers that may precede a trait in a bound, e.g. `?Trait` or `~const Trait`.
843     ///
844     /// If no modifiers are present, this does not consume any tokens.
845     ///
846     /// ```ebnf
847     /// TY_BOUND_MODIFIERS = ["~const"] ["?"]
848     /// ```
849     fn parse_ty_bound_modifiers(&mut self) -> PResult<'a, BoundModifiers> {
850         let maybe_const = if self.eat(&token::Tilde) {
851             let tilde = self.prev_token.span;
852             self.expect_keyword(kw::Const)?;
853             let span = tilde.to(self.prev_token.span);
854             self.sess.gated_spans.gate(sym::const_trait_impl, span);
855             Some(span)
856         } else if self.eat_keyword(kw::Const) {
857             let span = self.prev_token.span;
858             self.sess.gated_spans.gate(sym::const_trait_impl, span);
859
860             self.struct_span_err(span, "const bounds must start with `~`")
861                 .span_suggestion(
862                     span.shrink_to_lo(),
863                     "add `~`",
864                     "~",
865                     Applicability::MachineApplicable,
866                 )
867                 .emit();
868
869             Some(span)
870         } else {
871             None
872         };
873
874         let maybe = if self.eat(&token::Question) { Some(self.prev_token.span) } else { None };
875
876         Ok(BoundModifiers { maybe, maybe_const })
877     }
878
879     /// Parses a type bound according to:
880     /// ```ebnf
881     /// TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN)
882     /// TY_BOUND_NOPAREN = [TY_BOUND_MODIFIERS] [for<LT_PARAM_DEFS>] SIMPLE_PATH
883     /// ```
884     ///
885     /// For example, this grammar accepts `~const ?for<'a: 'b> m::Trait<'a>`.
886     fn parse_generic_ty_bound(
887         &mut self,
888         lo: Span,
889         has_parens: bool,
890         modifiers: BoundModifiers,
891     ) -> PResult<'a, GenericBound> {
892         let mut lifetime_defs = self.parse_late_bound_lifetime_defs()?;
893         let mut path = if self.token.is_keyword(kw::Fn)
894             && self.look_ahead(1, |tok| tok.kind == TokenKind::OpenDelim(Delimiter::Parenthesis))
895             && let Some(path) = self.recover_path_from_fn()
896         {
897             path
898         } else if !self.token.is_path_start() && self.token.can_begin_type() {
899             let ty = self.parse_ty_no_plus()?;
900             // Instead of finding a path (a trait), we found a type.
901             let mut err = self.struct_span_err(ty.span, "expected a trait, found type");
902
903             // If we can recover, try to extract a path from the type. Note
904             // that we do not use the try operator when parsing the type because
905             // if it fails then we get a parser error which we don't want (we're trying
906             // to recover from errors, not make more).
907             let path = if self.may_recover()
908                 && matches!(ty.kind, TyKind::Ptr(..) | TyKind::Ref(..))
909                 && let TyKind::Path(_, path) = &ty.peel_refs().kind {
910                 // Just get the indirection part of the type.
911                 let span = ty.span.until(path.span);
912
913                 err.span_suggestion_verbose(
914                     span,
915                     "consider removing the indirection",
916                     "",
917                     Applicability::MaybeIncorrect,
918                 );
919
920                 path.clone()
921             } else {
922                 return Err(err);
923             };
924
925             err.emit();
926
927             path
928         } else {
929             self.parse_path(PathStyle::Type)?
930         };
931
932         if self.may_recover() && self.token == TokenKind::OpenDelim(Delimiter::Parenthesis) {
933             self.recover_fn_trait_with_lifetime_params(&mut path, &mut lifetime_defs)?;
934         }
935
936         if has_parens {
937             if self.token.is_like_plus() {
938                 // Someone has written something like `&dyn (Trait + Other)`. The correct code
939                 // would be `&(dyn Trait + Other)`, but we don't have access to the appropriate
940                 // span to suggest that. When written as `&dyn Trait + Other`, an appropriate
941                 // suggestion is given.
942                 let bounds = vec![];
943                 self.parse_remaining_bounds(bounds, true)?;
944                 self.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
945                 let sp = vec![lo, self.prev_token.span];
946                 let sugg = vec![(lo, String::from(" ")), (self.prev_token.span, String::new())];
947                 self.struct_span_err(sp, "incorrect braces around trait bounds")
948                     .multipart_suggestion(
949                         "remove the parentheses",
950                         sugg,
951                         Applicability::MachineApplicable,
952                     )
953                     .emit();
954             } else {
955                 self.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
956             }
957         }
958
959         let modifier = modifiers.to_trait_bound_modifier();
960         let poly_trait = PolyTraitRef::new(lifetime_defs, path, lo.to(self.prev_token.span));
961         Ok(GenericBound::Trait(poly_trait, modifier))
962     }
963
964     // recovers a `Fn(..)` parenthesized-style path from `fn(..)`
965     fn recover_path_from_fn(&mut self) -> Option<ast::Path> {
966         let fn_token_span = self.token.span;
967         self.bump();
968         let args_lo = self.token.span;
969         let snapshot = self.create_snapshot_for_diagnostic();
970         match self.parse_fn_decl(|_| false, AllowPlus::No, RecoverReturnSign::OnlyFatArrow) {
971             Ok(decl) => {
972                 self.sess.emit_err(ExpectedFnPathFoundFnKeyword { fn_token_span });
973                 Some(ast::Path {
974                     span: fn_token_span.to(self.prev_token.span),
975                     segments: thin_vec![ast::PathSegment {
976                         ident: Ident::new(Symbol::intern("Fn"), fn_token_span),
977                         id: DUMMY_NODE_ID,
978                         args: Some(P(ast::GenericArgs::Parenthesized(ast::ParenthesizedArgs {
979                             span: args_lo.to(self.prev_token.span),
980                             inputs: decl.inputs.iter().map(|a| a.ty.clone()).collect(),
981                             inputs_span: args_lo.until(decl.output.span()),
982                             output: decl.output.clone(),
983                         }))),
984                     }],
985                     tokens: None,
986                 })
987             }
988             Err(diag) => {
989                 diag.cancel();
990                 self.restore_snapshot(snapshot);
991                 None
992             }
993         }
994     }
995
996     /// Optionally parses `for<$generic_params>`.
997     pub(super) fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec<GenericParam>> {
998         if self.eat_keyword(kw::For) {
999             self.expect_lt()?;
1000             let params = self.parse_generic_params()?;
1001             self.expect_gt()?;
1002             // We rely on AST validation to rule out invalid cases: There must not be type
1003             // parameters, and the lifetime parameters must not have bounds.
1004             Ok(params)
1005         } else {
1006             Ok(Vec::new())
1007         }
1008     }
1009
1010     /// Recover from `Fn`-family traits (Fn, FnMut, FnOnce) with lifetime arguments
1011     /// (e.g. `FnOnce<'a>(&'a str) -> bool`). Up to generic arguments have already
1012     /// been eaten.
1013     fn recover_fn_trait_with_lifetime_params(
1014         &mut self,
1015         fn_path: &mut ast::Path,
1016         lifetime_defs: &mut Vec<GenericParam>,
1017     ) -> PResult<'a, ()> {
1018         let fn_path_segment = fn_path.segments.last_mut().unwrap();
1019         let generic_args = if let Some(p_args) = &fn_path_segment.args {
1020             p_args.clone().into_inner()
1021         } else {
1022             // Normally it wouldn't come here because the upstream should have parsed
1023             // generic parameters (otherwise it's impossible to call this function).
1024             return Ok(());
1025         };
1026         let lifetimes =
1027             if let ast::GenericArgs::AngleBracketed(ast::AngleBracketedArgs { span: _, args }) =
1028                 &generic_args
1029             {
1030                 args.into_iter()
1031                     .filter_map(|arg| {
1032                         if let ast::AngleBracketedArg::Arg(generic_arg) = arg
1033                             && let ast::GenericArg::Lifetime(lifetime) = generic_arg {
1034                             Some(lifetime)
1035                         } else {
1036                             None
1037                         }
1038                     })
1039                     .collect()
1040             } else {
1041                 Vec::new()
1042             };
1043         // Only try to recover if the trait has lifetime params.
1044         if lifetimes.is_empty() {
1045             return Ok(());
1046         }
1047
1048         // Parse `(T, U) -> R`.
1049         let inputs_lo = self.token.span;
1050         let inputs: Vec<_> =
1051             self.parse_fn_params(|_| false)?.into_iter().map(|input| input.ty).collect();
1052         let inputs_span = inputs_lo.to(self.prev_token.span);
1053         let output = self.parse_ret_ty(AllowPlus::No, RecoverQPath::No, RecoverReturnSign::No)?;
1054         let args = ast::ParenthesizedArgs {
1055             span: fn_path_segment.span().to(self.prev_token.span),
1056             inputs,
1057             inputs_span,
1058             output,
1059         }
1060         .into();
1061         *fn_path_segment =
1062             ast::PathSegment { ident: fn_path_segment.ident, args, id: ast::DUMMY_NODE_ID };
1063
1064         // Convert parsed `<'a>` in `Fn<'a>` into `for<'a>`.
1065         let mut generic_params = lifetimes
1066             .iter()
1067             .map(|lt| GenericParam {
1068                 id: lt.id,
1069                 ident: lt.ident,
1070                 attrs: ast::AttrVec::new(),
1071                 bounds: Vec::new(),
1072                 is_placeholder: false,
1073                 kind: ast::GenericParamKind::Lifetime,
1074                 colon_span: None,
1075             })
1076             .collect::<Vec<GenericParam>>();
1077         lifetime_defs.append(&mut generic_params);
1078
1079         let generic_args_span = generic_args.span();
1080         let mut err =
1081             self.struct_span_err(generic_args_span, "`Fn` traits cannot take lifetime parameters");
1082         let snippet = format!(
1083             "for<{}> ",
1084             lifetimes.iter().map(|lt| lt.ident.as_str()).intersperse(", ").collect::<String>(),
1085         );
1086         let before_fn_path = fn_path.span.shrink_to_lo();
1087         err.multipart_suggestion(
1088             "consider using a higher-ranked trait bound instead",
1089             vec![(generic_args_span, "".to_owned()), (before_fn_path, snippet)],
1090             Applicability::MaybeIncorrect,
1091         )
1092         .emit();
1093         Ok(())
1094     }
1095
1096     pub(super) fn check_lifetime(&mut self) -> bool {
1097         self.expected_tokens.push(TokenType::Lifetime);
1098         self.token.is_lifetime()
1099     }
1100
1101     /// Parses a single lifetime `'a` or panics.
1102     pub(super) fn expect_lifetime(&mut self) -> Lifetime {
1103         if let Some(ident) = self.token.lifetime() {
1104             self.bump();
1105             Lifetime { ident, id: ast::DUMMY_NODE_ID }
1106         } else {
1107             self.span_bug(self.token.span, "not a lifetime")
1108         }
1109     }
1110
1111     pub(super) fn mk_ty(&self, span: Span, kind: TyKind) -> P<Ty> {
1112         P(Ty { kind, span, id: ast::DUMMY_NODE_ID, tokens: None })
1113     }
1114 }