1 use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
2 use super::{Parser, Restrictions, TokenType};
3 use crate::maybe_whole;
5 use rustc_ast::token::{self, Delimiter, Token};
7 self as ast, AngleBracketedArg, AngleBracketedArgs, AnonConst, AssocConstraint,
8 AssocConstraintKind, BlockCheckMode, GenericArg, GenericArgs, Generics, ParenthesizedArgs,
9 Path, PathSegment, QSelf,
11 use rustc_errors::{pluralize, Applicability, PResult};
12 use rustc_span::source_map::{BytePos, Span};
13 use rustc_span::symbol::{kw, sym, Ident};
18 /// Specifies how to parse a path.
19 #[derive(Copy, Clone, PartialEq)]
21 /// In some contexts, notably in expressions, paths with generic arguments are ambiguous
22 /// with something else. For example, in expressions `segment < ....` can be interpreted
23 /// as a comparison and `segment ( ....` can be interpreted as a function call.
24 /// In all such contexts the non-path interpretation is preferred by default for practical
25 /// reasons, but the path interpretation can be forced by the disambiguator `::`, e.g.
26 /// `x<y>` - comparisons, `x::<y>` - unambiguously a path.
28 /// In other contexts, notably in types, no ambiguity exists and paths can be written
29 /// without the disambiguator, e.g., `x<y>` - unambiguously a path.
30 /// Paths with disambiguators are still accepted, `x::<Y>` - unambiguously a path too.
32 /// A path with generic arguments disallowed, e.g., `foo::bar::Baz`, used in imports,
33 /// visibilities or attributes.
34 /// Technically, this variant is unnecessary and e.g., `Expr` can be used instead
35 /// (paths in "mod" contexts have to be checked later for absence of generic arguments
36 /// anyway, due to macros), but it is used to avoid weird suggestions about expected
37 /// tokens when something goes wrong.
42 /// Parses a qualified path.
43 /// Assumes that the leading `<` has been parsed already.
45 /// `qualified_path = <type [as trait_ref]>::path`
50 /// `<T as U>::F::a<S>` (without disambiguator)
51 /// `<T as U>::F::a::<S>` (with disambiguator)
52 pub(super) fn parse_qpath(&mut self, style: PathStyle) -> PResult<'a, (QSelf, Path)> {
53 let lo = self.prev_token.span;
54 let ty = self.parse_ty()?;
56 // `path` will contain the prefix of the path up to the `>`,
57 // if any (e.g., `U` in the `<T as U>::*` examples
58 // above). `path_span` has the span of that path, or an empty
59 // span in the case of something like `<T>::Bar`.
60 let (mut path, path_span);
61 if self.eat_keyword(kw::As) {
62 let path_lo = self.token.span;
63 path = self.parse_path(PathStyle::Type)?;
64 path_span = path_lo.to(self.prev_token.span);
66 path_span = self.token.span.to(self.token.span);
67 path = ast::Path { segments: Vec::new(), span: path_span, tokens: None };
70 // See doc comment for `unmatched_angle_bracket_count`.
71 self.expect(&token::Gt)?;
72 if self.unmatched_angle_bracket_count > 0 {
73 self.unmatched_angle_bracket_count -= 1;
74 debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count);
77 if !self.recover_colon_before_qpath_proj() {
78 self.expect(&token::ModSep)?;
81 let qself = QSelf { ty, path_span, position: path.segments.len() };
82 self.parse_path_segments(&mut path.segments, style, None)?;
86 Path { segments: path.segments, span: lo.to(self.prev_token.span), tokens: None },
90 /// Recover from an invalid single colon, when the user likely meant a qualified path.
91 /// We avoid emitting this if not followed by an identifier, as our assumption that the user
92 /// intended this to be a qualified path may not be correct.
94 /// ```ignore (diagnostics)
95 /// <Bar as Baz<T>>:Qux
96 /// ^ help: use double colon
98 fn recover_colon_before_qpath_proj(&mut self) -> bool {
99 if self.token.kind != token::Colon
100 || self.look_ahead(1, |t| !t.is_ident() || t.is_reserved_ident())
105 self.bump(); // colon
109 self.prev_token.span,
110 "found single colon before projection in qualified path",
113 self.prev_token.span,
116 Applicability::MachineApplicable,
123 pub(super) fn parse_path(&mut self, style: PathStyle) -> PResult<'a, Path> {
124 self.parse_path_inner(style, None)
127 /// Parses simple paths.
129 /// `path = [::] segment+`
130 /// `segment = ident | ident[::]<args> | ident[::](args) [-> type]`
133 /// `a::b::C<D>` (without disambiguator)
134 /// `a::b::C::<D>` (with disambiguator)
135 /// `Fn(Args)` (without disambiguator)
136 /// `Fn::(Args)` (with disambiguator)
137 pub(super) fn parse_path_inner(
140 ty_generics: Option<&Generics>,
141 ) -> PResult<'a, Path> {
142 let reject_generics_if_mod_style = |parser: &Parser<'_>, path: &Path| {
143 // Ensure generic arguments don't end up in attribute paths, such as:
146 // ($p:path) => { #[$p] struct S; }
149 // m!(inline<u8>); //~ ERROR: unexpected generic arguments in path
151 if style == PathStyle::Mod && path.segments.iter().any(|segment| segment.args.is_some())
157 .filter_map(|segment| segment.args.as_ref())
158 .map(|arg| arg.span())
159 .collect::<Vec<_>>(),
160 "unexpected generic arguments in path",
166 maybe_whole!(self, NtPath, |path| {
167 reject_generics_if_mod_style(self, &path);
171 if let token::Interpolated(nt) = &self.token.kind {
172 if let token::NtTy(ty) = &**nt {
173 if let ast::TyKind::Path(None, path) = &ty.kind {
174 let path = path.clone();
176 reject_generics_if_mod_style(self, &path);
182 let lo = self.token.span;
183 let mut segments = Vec::new();
184 let mod_sep_ctxt = self.token.span.ctxt();
185 if self.eat(&token::ModSep) {
186 segments.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
188 self.parse_path_segments(&mut segments, style, ty_generics)?;
190 Ok(Path { segments, span: lo.to(self.prev_token.span), tokens: None })
193 pub(super) fn parse_path_segments(
195 segments: &mut Vec<PathSegment>,
197 ty_generics: Option<&Generics>,
198 ) -> PResult<'a, ()> {
200 let segment = self.parse_path_segment(style, ty_generics)?;
201 if style == PathStyle::Expr {
202 // In order to check for trailing angle brackets, we must have finished
203 // recursing (`parse_path_segment` can indirectly call this function),
204 // that is, the next token must be the highlighted part of the below example:
206 // `Foo::<Bar as Baz<T>>::Qux`
209 // As opposed to the below highlight (if we had only finished the first
212 // `Foo::<Bar as Baz<T>>::Qux`
215 // `PathStyle::Expr` is only provided at the root invocation and never in
216 // `parse_path_segment` to recurse and therefore can be checked to maintain
218 self.check_trailing_angle_brackets(&segment, &[&token::ModSep]);
220 segments.push(segment);
222 if self.is_import_coupler() || !self.eat(&token::ModSep) {
228 pub(super) fn parse_path_segment(
231 ty_generics: Option<&Generics>,
232 ) -> PResult<'a, PathSegment> {
233 let ident = self.parse_path_segment_ident()?;
234 let is_args_start = |token: &Token| {
238 | token::BinOp(token::Shl)
239 | token::OpenDelim(Delimiter::Parenthesis)
243 let check_args_start = |this: &mut Self| {
244 this.expected_tokens.extend_from_slice(&[
245 TokenType::Token(token::Lt),
246 TokenType::Token(token::OpenDelim(Delimiter::Parenthesis)),
248 is_args_start(&this.token)
252 if style == PathStyle::Type && check_args_start(self)
253 || style != PathStyle::Mod
254 && self.check(&token::ModSep)
255 && self.look_ahead(1, |t| is_args_start(t))
257 // We use `style == PathStyle::Expr` to check if this is in a recursion or not. If
258 // it isn't, then we reset the unmatched angle bracket count as we're about to start
259 // parsing a new path.
260 if style == PathStyle::Expr {
261 self.unmatched_angle_bracket_count = 0;
262 self.max_angle_bracket_count = 0;
265 // Generic arguments are found - `<`, `(`, `::<` or `::(`.
266 self.eat(&token::ModSep);
267 let lo = self.token.span;
268 let args = if self.eat_lt() {
270 let args = self.parse_angle_args_with_leading_angle_bracket_recovery(
275 self.expect_gt().map_err(|mut err| {
276 // Attempt to find places where a missing `>` might belong.
277 if let Some(arg) = args
280 .skip_while(|arg| matches!(arg, AngleBracketedArg::Constraint(_)))
283 err.span_suggestion_verbose(
284 arg.span().shrink_to_hi(),
285 "you might have meant to end the type parameters here",
287 Applicability::MaybeIncorrect,
292 let span = lo.to(self.prev_token.span);
293 AngleBracketedArgs { args, span }.into()
296 let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?;
297 let inputs_span = lo.to(self.prev_token.span);
299 self.parse_ret_ty(AllowPlus::No, RecoverQPath::No, RecoverReturnSign::No)?;
300 let span = ident.span.to(self.prev_token.span);
301 ParenthesizedArgs { span, inputs, inputs_span, output }.into()
304 PathSegment { ident, args, id: ast::DUMMY_NODE_ID }
306 // Generic arguments are not found.
307 PathSegment::from_ident(ident)
312 pub(super) fn parse_path_segment_ident(&mut self) -> PResult<'a, Ident> {
313 match self.token.ident() {
314 Some((ident, false)) if ident.is_path_segment_keyword() => {
318 _ => self.parse_ident(),
322 /// Parses generic args (within a path segment) with recovery for extra leading angle brackets.
323 /// For the purposes of understanding the parsing logic of generic arguments, this function
324 /// can be thought of being the same as just calling `self.parse_angle_args()` if the source
325 /// had the correct amount of leading angle brackets.
327 /// ```ignore (diagnostics)
328 /// bar::<<<<T as Foo>::Output>();
329 /// ^^ help: remove extra angle brackets
331 fn parse_angle_args_with_leading_angle_bracket_recovery(
335 ty_generics: Option<&Generics>,
336 ) -> PResult<'a, Vec<AngleBracketedArg>> {
337 // We need to detect whether there are extra leading left angle brackets and produce an
338 // appropriate error and suggestion. This cannot be implemented by looking ahead at
339 // upcoming tokens for a matching `>` character - if there are unmatched `<` tokens
340 // then there won't be matching `>` tokens to find.
342 // To explain how this detection works, consider the following example:
344 // ```ignore (diagnostics)
345 // bar::<<<<T as Foo>::Output>();
346 // ^^ help: remove extra angle brackets
349 // Parsing of the left angle brackets starts in this function. We start by parsing the
350 // `<` token (incrementing the counter of unmatched angle brackets on `Parser` via
353 // *Upcoming tokens:* `<<<<T as Foo>::Output>;`
354 // *Unmatched count:* 1
355 // *`parse_path_segment` calls deep:* 0
357 // This has the effect of recursing as this function is called if a `<` character
358 // is found within the expected generic arguments:
360 // *Upcoming tokens:* `<<<T as Foo>::Output>;`
361 // *Unmatched count:* 2
362 // *`parse_path_segment` calls deep:* 1
364 // Eventually we will have recursed until having consumed all of the `<` tokens and
365 // this will be reflected in the count:
367 // *Upcoming tokens:* `T as Foo>::Output>;`
368 // *Unmatched count:* 4
369 // `parse_path_segment` calls deep:* 3
371 // The parser will continue until reaching the first `>` - this will decrement the
372 // unmatched angle bracket count and return to the parent invocation of this function
373 // having succeeded in parsing:
375 // *Upcoming tokens:* `::Output>;`
376 // *Unmatched count:* 3
377 // *`parse_path_segment` calls deep:* 2
379 // This will continue until the next `>` character which will also return successfully
380 // to the parent invocation of this function and decrement the count:
382 // *Upcoming tokens:* `;`
383 // *Unmatched count:* 2
384 // *`parse_path_segment` calls deep:* 1
386 // At this point, this function will expect to find another matching `>` character but
387 // won't be able to and will return an error. This will continue all the way up the
388 // call stack until the first invocation:
390 // *Upcoming tokens:* `;`
391 // *Unmatched count:* 2
392 // *`parse_path_segment` calls deep:* 0
394 // In doing this, we have managed to work out how many unmatched leading left angle
395 // brackets there are, but we cannot recover as the unmatched angle brackets have
396 // already been consumed. To remedy this, we keep a snapshot of the parser state
397 // before we do the above. We can then inspect whether we ended up with a parsing error
398 // and unmatched left angle brackets and if so, restore the parser state before we
399 // consumed any `<` characters to emit an error and consume the erroneous tokens to
400 // recover by attempting to parse again.
402 // In practice, the recursion of this function is indirect and there will be other
403 // locations that consume some `<` characters - as long as we update the count when
404 // this happens, it isn't an issue.
406 let is_first_invocation = style == PathStyle::Expr;
407 // Take a snapshot before attempting to parse - we can restore this later.
408 let snapshot = if is_first_invocation { Some(self.clone()) } else { None };
410 debug!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)");
411 match self.parse_angle_args(ty_generics) {
412 Ok(args) => Ok(args),
413 Err(e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => {
414 // Swap `self` with our backup of the parser state before attempting to parse
415 // generic arguments.
416 let snapshot = mem::replace(self, snapshot.unwrap());
418 // Eat the unmatched angle brackets.
419 let all_angle_brackets = (0..snapshot.unmatched_angle_bracket_count)
420 .fold(true, |a, _| a && self.eat_lt());
422 if !all_angle_brackets {
423 // If there are other tokens in between the extraneous `<`s, we cannot simply
424 // suggest to remove them. This check also prevents us from accidentally ending
425 // up in the middle of a multibyte character (issue #84104).
426 let _ = mem::replace(self, snapshot);
429 // Cancel error from being unable to find `>`. We know the error
430 // must have been this due to a non-zero unmatched angle bracket
435 "parse_generic_args_with_leading_angle_bracket_recovery: (snapshot failure) \
436 snapshot.count={:?}",
437 snapshot.unmatched_angle_bracket_count,
440 // Make a span over ${unmatched angle bracket count} characters.
441 // This is safe because `all_angle_brackets` ensures that there are only `<`s,
442 // i.e. no multibyte characters, in this range.
444 lo.with_hi(lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count));
445 self.struct_span_err(
448 "unmatched angle bracket{}",
449 pluralize!(snapshot.unmatched_angle_bracket_count)
455 "remove extra angle bracket{}",
456 pluralize!(snapshot.unmatched_angle_bracket_count)
459 Applicability::MachineApplicable,
463 // Try again without unmatched angle bracket characters.
464 self.parse_angle_args(ty_generics)
471 /// Parses (possibly empty) list of generic arguments / associated item constraints,
472 /// possibly including trailing comma.
473 pub(super) fn parse_angle_args(
475 ty_generics: Option<&Generics>,
476 ) -> PResult<'a, Vec<AngleBracketedArg>> {
477 let mut args = Vec::new();
478 while let Some(arg) = self.parse_angle_arg(ty_generics)? {
480 if !self.eat(&token::Comma) {
481 if self.token.kind == token::Semi
482 && self.look_ahead(1, |t| t.is_ident() || t.is_lifetime())
484 // Add `>` to the list of expected tokens.
485 self.check(&token::Gt);
486 // Handle `,` to `;` substitution
487 let mut err = self.unexpected::<()>().unwrap_err();
489 err.span_suggestion_verbose(
490 self.prev_token.span.until(self.token.span),
491 "use a comma to separate type parameters",
493 Applicability::MachineApplicable,
498 if !self.token.kind.should_end_const_arg() {
499 if self.handle_ambiguous_unbraced_const_arg(&mut args)? {
500 // We've managed to (partially) recover, so continue trying to parse
511 /// Parses a single argument in the angle arguments `<...>` of a path segment.
514 ty_generics: Option<&Generics>,
515 ) -> PResult<'a, Option<AngleBracketedArg>> {
516 let lo = self.token.span;
517 let arg = self.parse_generic_arg(ty_generics)?;
520 if self.check(&token::Colon) | self.check(&token::Eq) {
521 let arg_span = arg.span();
522 let (binder, ident, gen_args) = match self.get_ident_from_generic_arg(&arg) {
523 Ok(ident_gen_args) => ident_gen_args,
524 Err(()) => return Ok(Some(AngleBracketedArg::Arg(arg))),
526 if binder.is_some() {
527 // FIXME(compiler-errors): this could be improved by suggesting lifting
528 // this up to the trait, at least before this becomes real syntax.
529 // e.g. `Trait<for<'a> Assoc = Ty>` -> `for<'a> Trait<Assoc = Ty>`
530 return Err(self.struct_span_err(
532 "`for<...>` is not allowed on associated type bounds",
535 let kind = if self.eat(&token::Colon) {
536 // Parse associated type constraint bound.
538 let bounds = self.parse_generic_bounds(Some(self.prev_token.span))?;
539 AssocConstraintKind::Bound { bounds }
540 } else if self.eat(&token::Eq) {
541 self.parse_assoc_equality_term(ident, self.prev_token.span)?
546 let span = lo.to(self.prev_token.span);
548 // Gate associated type bounds, e.g., `Iterator<Item: Ord>`.
549 if let AssocConstraintKind::Bound { .. } = kind {
550 self.sess.gated_spans.gate(sym::associated_type_bounds, span);
553 AssocConstraint { id: ast::DUMMY_NODE_ID, ident, gen_args, kind, span };
554 Ok(Some(AngleBracketedArg::Constraint(constraint)))
556 Ok(Some(AngleBracketedArg::Arg(arg)))
563 /// Parse the term to the right of an associated item equality constraint.
564 /// That is, parse `<term>` in `Item = <term>`.
565 /// Right now, this only admits types in `<term>`.
566 fn parse_assoc_equality_term(
570 ) -> PResult<'a, AssocConstraintKind> {
571 let arg = self.parse_generic_arg(None)?;
572 let span = ident.span.to(self.prev_token.span);
573 let term = match arg {
574 Some(GenericArg::Type(ty)) => ty.into(),
575 Some(GenericArg::Const(c)) => {
576 self.sess.gated_spans.gate(sym::associated_const_equality, span);
579 Some(GenericArg::Lifetime(lt)) => {
580 self.struct_span_err(span, "associated lifetimes are not supported")
581 .span_label(lt.ident.span, "the lifetime is given here")
582 .help("if you meant to specify a trait object, write `dyn Trait + 'lifetime`")
584 self.mk_ty(span, ast::TyKind::Err).into()
587 let after_eq = eq.shrink_to_hi();
588 let before_next = self.token.span.shrink_to_lo();
590 .struct_span_err(after_eq.to(before_next), "missing type to the right of `=`");
591 if matches!(self.token.kind, token::Comma | token::Gt) {
593 self.sess.source_map().next_point(eq).to(before_next),
594 "to constrain the associated type, add a type after `=`",
595 " TheType".to_string(),
596 Applicability::HasPlaceholders,
600 &format!("remove the `=` if `{}` is a type", ident),
602 Applicability::MaybeIncorrect,
607 &format!("expected type, found {}", super::token_descr(&self.token)),
613 Ok(AssocConstraintKind::Equality { term })
616 /// We do not permit arbitrary expressions as const arguments. They must be one of:
617 /// - An expression surrounded in `{}`.
619 /// - A numeric literal prefixed by `-`.
620 /// - A single-segment path.
621 pub(super) fn expr_is_valid_const_arg(&self, expr: &P<rustc_ast::Expr>) -> bool {
623 ast::ExprKind::Block(_, _) | ast::ExprKind::Lit(_) => true,
624 ast::ExprKind::Unary(ast::UnOp::Neg, expr) => {
625 matches!(expr.kind, ast::ExprKind::Lit(_))
627 // We can only resolve single-segment paths at the moment, because multi-segment paths
628 // require type-checking: see `visit_generic_arg` in `src/librustc_resolve/late.rs`.
629 ast::ExprKind::Path(None, path)
630 if path.segments.len() == 1 && path.segments[0].args.is_none() =>
638 /// Parse a const argument, e.g. `<3>`. It is assumed the angle brackets will be parsed by
640 pub(super) fn parse_const_arg(&mut self) -> PResult<'a, AnonConst> {
641 // Parse const argument.
642 let value = if let token::OpenDelim(Delimiter::Brace) = self.token.kind {
643 self.parse_block_expr(
646 BlockCheckMode::Default,
650 self.handle_unambiguous_unbraced_const_arg()?
652 Ok(AnonConst { id: ast::DUMMY_NODE_ID, value })
655 /// Parse a generic argument in a path segment.
656 /// This does not include constraints, e.g., `Item = u8`, which is handled in `parse_angle_arg`.
657 pub(super) fn parse_generic_arg(
659 ty_generics: Option<&Generics>,
660 ) -> PResult<'a, Option<GenericArg>> {
661 let start = self.token.span;
662 let arg = if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) {
663 // Parse lifetime argument.
664 GenericArg::Lifetime(self.expect_lifetime())
665 } else if self.check_const_arg() {
666 // Parse const argument.
667 GenericArg::Const(self.parse_const_arg()?)
668 } else if self.check_type() {
669 // Parse type argument.
671 self.look_ahead(1, |t| t.kind == token::OpenDelim(Delimiter::Parenthesis));
672 let mut snapshot = self.create_snapshot_for_diagnostic();
673 match self.parse_ty() {
674 Ok(ty) => GenericArg::Type(ty),
677 match (*snapshot).parse_expr_res(Restrictions::CONST_EXPR, None) {
679 self.restore_snapshot(snapshot);
680 return Ok(Some(self.dummy_const_arg_needs_braces(err, expr.span)));
687 // Try to recover from possible `const` arg without braces.
688 return self.recover_const_arg(start, err).map(Some);
691 } else if self.token.is_keyword(kw::Const) {
692 return self.recover_const_param_declaration(ty_generics);
694 // Fall back by trying to parse a const-expr expression. If we successfully do so,
695 // then we should report an error that it needs to be wrapped in braces.
696 let snapshot = self.create_snapshot_for_diagnostic();
697 match self.parse_expr_res(Restrictions::CONST_EXPR, None) {
699 return Ok(Some(self.dummy_const_arg_needs_braces(
700 self.struct_span_err(expr.span, "invalid const generic expression"),
705 self.restore_snapshot(snapshot);
714 /// Given a arg inside of generics, we try to destructure it as if it were the LHS in
715 /// `LHS = ...`, i.e. an associated type binding.
716 /// This returns (optionally, if they are present) any `for<'a, 'b>` binder args, the
717 /// identifier, and any GAT arguments.
718 fn get_ident_from_generic_arg(
720 gen_arg: &GenericArg,
721 ) -> Result<(Option<Vec<ast::GenericParam>>, Ident, Option<GenericArgs>), ()> {
722 if let GenericArg::Type(ty) = gen_arg {
723 if let ast::TyKind::Path(qself, path) = &ty.kind
725 && let [seg] = path.segments.as_slice()
727 return Ok((None, seg.ident, seg.args.as_deref().cloned()));
728 } else if let ast::TyKind::TraitObject(bounds, ast::TraitObjectSyntax::None) = &ty.kind
729 && let [ast::GenericBound::Trait(trait_ref, ast::TraitBoundModifier::None)] =
731 && let [seg] = trait_ref.trait_ref.path.segments.as_slice()
734 Some(trait_ref.bound_generic_params.clone()),
736 seg.args.as_deref().cloned(),