1 use super::ty
::{AllowPlus, RecoverQPath}
;
2 use super::{Parser, TokenType}
;
3 use crate::maybe_whole
;
4 use rustc_ast
::ast
::{self, AngleBracketedArg, AngleBracketedArgs, GenericArg, ParenthesizedArgs}
;
5 use rustc_ast
::ast
::{AnonConst, AssocTyConstraint, AssocTyConstraintKind, BlockCheckMode}
;
6 use rustc_ast
::ast
::{Ident, Path, PathSegment, QSelf}
;
8 use rustc_ast
::token
::{self, Token}
;
9 use rustc_errors
::{pluralize, Applicability, PResult}
;
10 use rustc_span
::source_map
::{BytePos, Span}
;
11 use rustc_span
::symbol
::{kw, sym}
;
16 /// Specifies how to parse a path.
17 #[derive(Copy, Clone, PartialEq)]
19 /// In some contexts, notably in expressions, paths with generic arguments are ambiguous
20 /// with something else. For example, in expressions `segment < ....` can be interpreted
21 /// as a comparison and `segment ( ....` can be interpreted as a function call.
22 /// In all such contexts the non-path interpretation is preferred by default for practical
23 /// reasons, but the path interpretation can be forced by the disambiguator `::`, e.g.
24 /// `x<y>` - comparisons, `x::<y>` - unambiguously a path.
26 /// In other contexts, notably in types, no ambiguity exists and paths can be written
27 /// without the disambiguator, e.g., `x<y>` - unambiguously a path.
28 /// Paths with disambiguators are still accepted, `x::<Y>` - unambiguously a path too.
30 /// A path with generic arguments disallowed, e.g., `foo::bar::Baz`, used in imports,
31 /// visibilities or attributes.
32 /// Technically, this variant is unnecessary and e.g., `Expr` can be used instead
33 /// (paths in "mod" contexts have to be checked later for absence of generic arguments
34 /// anyway, due to macros), but it is used to avoid weird suggestions about expected
35 /// tokens when something goes wrong.
40 /// Parses a qualified path.
41 /// Assumes that the leading `<` has been parsed already.
43 /// `qualified_path = <type [as trait_ref]>::path`
48 /// `<T as U>::F::a<S>` (without disambiguator)
49 /// `<T as U>::F::a::<S>` (with disambiguator)
50 pub(super) fn parse_qpath(&mut self, style
: PathStyle
) -> PResult
<'a
, (QSelf
, Path
)> {
51 let lo
= self.prev_token
.span
;
52 let ty
= self.parse_ty()?
;
54 // `path` will contain the prefix of the path up to the `>`,
55 // if any (e.g., `U` in the `<T as U>::*` examples
56 // above). `path_span` has the span of that path, or an empty
57 // span in the case of something like `<T>::Bar`.
58 let (mut path
, path_span
);
59 if self.eat_keyword(kw
::As
) {
60 let path_lo
= self.token
.span
;
61 path
= self.parse_path(PathStyle
::Type
)?
;
62 path_span
= path_lo
.to(self.prev_token
.span
);
64 path_span
= self.token
.span
.to(self.token
.span
);
65 path
= ast
::Path { segments: Vec::new(), span: path_span }
;
68 // See doc comment for `unmatched_angle_bracket_count`.
69 self.expect(&token
::Gt
)?
;
70 if self.unmatched_angle_bracket_count
> 0 {
71 self.unmatched_angle_bracket_count
-= 1;
72 debug
!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count
);
75 if !self.recover_colon_before_qpath_proj() {
76 self.expect(&token
::ModSep
)?
;
79 let qself
= QSelf { ty, path_span, position: path.segments.len() }
;
80 self.parse_path_segments(&mut path
.segments
, style
)?
;
82 Ok((qself
, Path { segments: path.segments, span: lo.to(self.prev_token.span) }
))
85 /// Recover from an invalid single colon, when the user likely meant a qualified path.
86 /// We avoid emitting this if not followed by an identifier, as our assumption that the user
87 /// intended this to be a qualified path may not be correct.
89 /// ```ignore (diagnostics)
90 /// <Bar as Baz<T>>:Qux
91 /// ^ help: use double colon
93 fn recover_colon_before_qpath_proj(&mut self) -> bool
{
94 if self.token
.kind
!= token
::Colon
95 || self.look_ahead(1, |t
| !t
.is_ident() || t
.is_reserved_ident())
100 self.bump(); // colon
104 self.prev_token
.span
,
105 "found single colon before projection in qualified path",
108 self.prev_token
.span
,
111 Applicability
::MachineApplicable
,
118 /// Parses simple paths.
120 /// `path = [::] segment+`
121 /// `segment = ident | ident[::]<args> | ident[::](args) [-> type]`
124 /// `a::b::C<D>` (without disambiguator)
125 /// `a::b::C::<D>` (with disambiguator)
126 /// `Fn(Args)` (without disambiguator)
127 /// `Fn::(Args)` (with disambiguator)
128 pub fn parse_path(&mut self, style
: PathStyle
) -> PResult
<'a
, Path
> {
129 maybe_whole
!(self, NtPath
, |path
| {
130 if style
== PathStyle
::Mod
&& path
.segments
.iter().any(|segment
| segment
.args
.is_some())
132 self.struct_span_err(path
.span
, "unexpected generic arguments in path").emit();
137 let lo
= self.token
.span
;
138 let mut segments
= Vec
::new();
139 let mod_sep_ctxt
= self.token
.span
.ctxt();
140 if self.eat(&token
::ModSep
) {
141 segments
.push(PathSegment
::path_root(lo
.shrink_to_lo().with_ctxt(mod_sep_ctxt
)));
143 self.parse_path_segments(&mut segments
, style
)?
;
145 Ok(Path { segments, span: lo.to(self.prev_token.span) }
)
148 pub(super) fn parse_path_segments(
150 segments
: &mut Vec
<PathSegment
>,
152 ) -> PResult
<'a
, ()> {
154 let segment
= self.parse_path_segment(style
)?
;
155 if style
== PathStyle
::Expr
{
156 // In order to check for trailing angle brackets, we must have finished
157 // recursing (`parse_path_segment` can indirectly call this function),
158 // that is, the next token must be the highlighted part of the below example:
160 // `Foo::<Bar as Baz<T>>::Qux`
163 // As opposed to the below highlight (if we had only finished the first
166 // `Foo::<Bar as Baz<T>>::Qux`
169 // `PathStyle::Expr` is only provided at the root invocation and never in
170 // `parse_path_segment` to recurse and therefore can be checked to maintain
172 self.check_trailing_angle_brackets(&segment
, token
::ModSep
);
174 segments
.push(segment
);
176 if self.is_import_coupler() || !self.eat(&token
::ModSep
) {
182 pub(super) fn parse_path_segment(&mut self, style
: PathStyle
) -> PResult
<'a
, PathSegment
> {
183 let ident
= self.parse_path_segment_ident()?
;
185 let is_args_start
= |token
: &Token
| match token
.kind
{
187 | token
::BinOp(token
::Shl
)
188 | token
::OpenDelim(token
::Paren
)
189 | token
::LArrow
=> true,
192 let check_args_start
= |this
: &mut Self| {
193 this
.expected_tokens
.extend_from_slice(&[
194 TokenType
::Token(token
::Lt
),
195 TokenType
::Token(token
::OpenDelim(token
::Paren
)),
197 is_args_start(&this
.token
)
201 if style
== PathStyle
::Type
&& check_args_start(self)
202 || style
!= PathStyle
::Mod
203 && self.check(&token
::ModSep
)
204 && self.look_ahead(1, |t
| is_args_start(t
))
206 // We use `style == PathStyle::Expr` to check if this is in a recursion or not. If
207 // it isn't, then we reset the unmatched angle bracket count as we're about to start
208 // parsing a new path.
209 if style
== PathStyle
::Expr
{
210 self.unmatched_angle_bracket_count
= 0;
211 self.max_angle_bracket_count
= 0;
214 // Generic arguments are found - `<`, `(`, `::<` or `::(`.
215 self.eat(&token
::ModSep
);
216 let lo
= self.token
.span
;
217 let args
= if self.eat_lt() {
220 self.parse_angle_args_with_leading_angle_bracket_recovery(style
, lo
)?
;
222 let span
= lo
.to(self.prev_token
.span
);
223 AngleBracketedArgs { args, span }
.into()
226 let (inputs
, _
) = self.parse_paren_comma_seq(|p
| p
.parse_ty())?
;
227 let span
= ident
.span
.to(self.prev_token
.span
);
228 let output
= self.parse_ret_ty(AllowPlus
::No
, RecoverQPath
::No
)?
;
229 ParenthesizedArgs { inputs, output, span }
.into()
232 PathSegment { ident, args, id: ast::DUMMY_NODE_ID }
234 // Generic arguments are not found.
235 PathSegment
::from_ident(ident
)
240 pub(super) fn parse_path_segment_ident(&mut self) -> PResult
<'a
, Ident
> {
241 match self.token
.ident() {
242 Some((ident
, false)) if ident
.is_path_segment_keyword() => {
246 _
=> self.parse_ident(),
250 /// Parses generic args (within a path segment) with recovery for extra leading angle brackets.
251 /// For the purposes of understanding the parsing logic of generic arguments, this function
252 /// can be thought of being the same as just calling `self.parse_angle_args()` if the source
253 /// had the correct amount of leading angle brackets.
255 /// ```ignore (diagnostics)
256 /// bar::<<<<T as Foo>::Output>();
257 /// ^^ help: remove extra angle brackets
259 fn parse_angle_args_with_leading_angle_bracket_recovery(
263 ) -> PResult
<'a
, Vec
<AngleBracketedArg
>> {
264 // We need to detect whether there are extra leading left angle brackets and produce an
265 // appropriate error and suggestion. This cannot be implemented by looking ahead at
266 // upcoming tokens for a matching `>` character - if there are unmatched `<` tokens
267 // then there won't be matching `>` tokens to find.
269 // To explain how this detection works, consider the following example:
271 // ```ignore (diagnostics)
272 // bar::<<<<T as Foo>::Output>();
273 // ^^ help: remove extra angle brackets
276 // Parsing of the left angle brackets starts in this function. We start by parsing the
277 // `<` token (incrementing the counter of unmatched angle brackets on `Parser` via
280 // *Upcoming tokens:* `<<<<T as Foo>::Output>;`
281 // *Unmatched count:* 1
282 // *`parse_path_segment` calls deep:* 0
284 // This has the effect of recursing as this function is called if a `<` character
285 // is found within the expected generic arguments:
287 // *Upcoming tokens:* `<<<T as Foo>::Output>;`
288 // *Unmatched count:* 2
289 // *`parse_path_segment` calls deep:* 1
291 // Eventually we will have recursed until having consumed all of the `<` tokens and
292 // this will be reflected in the count:
294 // *Upcoming tokens:* `T as Foo>::Output>;`
295 // *Unmatched count:* 4
296 // `parse_path_segment` calls deep:* 3
298 // The parser will continue until reaching the first `>` - this will decrement the
299 // unmatched angle bracket count and return to the parent invocation of this function
300 // having succeeded in parsing:
302 // *Upcoming tokens:* `::Output>;`
303 // *Unmatched count:* 3
304 // *`parse_path_segment` calls deep:* 2
306 // This will continue until the next `>` character which will also return successfully
307 // to the parent invocation of this function and decrement the count:
309 // *Upcoming tokens:* `;`
310 // *Unmatched count:* 2
311 // *`parse_path_segment` calls deep:* 1
313 // At this point, this function will expect to find another matching `>` character but
314 // won't be able to and will return an error. This will continue all the way up the
315 // call stack until the first invocation:
317 // *Upcoming tokens:* `;`
318 // *Unmatched count:* 2
319 // *`parse_path_segment` calls deep:* 0
321 // In doing this, we have managed to work out how many unmatched leading left angle
322 // brackets there are, but we cannot recover as the unmatched angle brackets have
323 // already been consumed. To remedy this, we keep a snapshot of the parser state
324 // before we do the above. We can then inspect whether we ended up with a parsing error
325 // and unmatched left angle brackets and if so, restore the parser state before we
326 // consumed any `<` characters to emit an error and consume the erroneous tokens to
327 // recover by attempting to parse again.
329 // In practice, the recursion of this function is indirect and there will be other
330 // locations that consume some `<` characters - as long as we update the count when
331 // this happens, it isn't an issue.
333 let is_first_invocation
= style
== PathStyle
::Expr
;
334 // Take a snapshot before attempting to parse - we can restore this later.
335 let snapshot
= if is_first_invocation { Some(self.clone()) }
else { None }
;
337 debug
!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)");
338 match self.parse_angle_args() {
339 Ok(args
) => Ok(args
),
340 Err(ref mut e
) if is_first_invocation
&& self.unmatched_angle_bracket_count
> 0 => {
341 // Cancel error from being unable to find `>`. We know the error
342 // must have been this due to a non-zero unmatched angle bracket
346 // Swap `self` with our backup of the parser state before attempting to parse
347 // generic arguments.
348 let snapshot
= mem
::replace(self, snapshot
.unwrap());
351 "parse_generic_args_with_leading_angle_bracket_recovery: (snapshot failure) \
352 snapshot.count={:?}",
353 snapshot
.unmatched_angle_bracket_count
,
356 // Eat the unmatched angle brackets.
357 for _
in 0..snapshot
.unmatched_angle_bracket_count
{
361 // Make a span over ${unmatched angle bracket count} characters.
362 let span
= lo
.with_hi(lo
.lo() + BytePos(snapshot
.unmatched_angle_bracket_count
));
363 self.struct_span_err(
366 "unmatched angle bracket{}",
367 pluralize
!(snapshot
.unmatched_angle_bracket_count
)
373 "remove extra angle bracket{}",
374 pluralize
!(snapshot
.unmatched_angle_bracket_count
)
377 Applicability
::MachineApplicable
,
381 // Try again without unmatched angle bracket characters.
382 self.parse_angle_args()
388 /// Parses (possibly empty) list of generic arguments / associated item constraints,
389 /// possibly including trailing comma.
390 fn parse_angle_args(&mut self) -> PResult
<'a
, Vec
<AngleBracketedArg
>> {
391 let mut args
= Vec
::new();
392 while let Some(arg
) = self.parse_angle_arg()?
{
394 if !self.eat(&token
::Comma
) {
401 /// Parses a single argument in the angle arguments `<...>` of a path segment.
402 fn parse_angle_arg(&mut self) -> PResult
<'a
, Option
<AngleBracketedArg
>> {
403 if self.check_ident() && self.look_ahead(1, |t
| matches
!(t
.kind
, token
::Eq
| token
::Colon
))
405 // Parse associated type constraint.
406 let lo
= self.token
.span
;
407 let ident
= self.parse_ident()?
;
408 let kind
= if self.eat(&token
::Eq
) {
409 let ty
= self.parse_assoc_equality_term(ident
, self.prev_token
.span
)?
;
410 AssocTyConstraintKind
::Equality { ty }
411 } else if self.eat(&token
::Colon
) {
412 let bounds
= self.parse_generic_bounds(Some(self.prev_token
.span
))?
;
413 AssocTyConstraintKind
::Bound { bounds }
418 let span
= lo
.to(self.prev_token
.span
);
420 // Gate associated type bounds, e.g., `Iterator<Item: Ord>`.
421 if let AssocTyConstraintKind
::Bound { .. }
= kind
{
422 self.sess
.gated_spans
.gate(sym
::associated_type_bounds
, span
);
425 let constraint
= AssocTyConstraint { id: ast::DUMMY_NODE_ID, ident, kind, span }
;
426 Ok(Some(AngleBracketedArg
::Constraint(constraint
)))
428 Ok(self.parse_generic_arg()?
.map(AngleBracketedArg
::Arg
))
432 /// Parse the term to the right of an associated item equality constraint.
433 /// That is, parse `<term>` in `Item = <term>`.
434 /// Right now, this only admits types in `<term>`.
435 fn parse_assoc_equality_term(&mut self, ident
: Ident
, eq
: Span
) -> PResult
<'a
, P
<ast
::Ty
>> {
436 let arg
= self.parse_generic_arg()?
;
437 let span
= ident
.span
.to(self.prev_token
.span
);
439 Some(GenericArg
::Type(ty
)) => return Ok(ty
),
440 Some(GenericArg
::Const(expr
)) => {
441 self.struct_span_err(span
, "cannot constrain an associated constant to a value")
442 .span_label(ident
.span
, "this associated constant...")
443 .span_label(expr
.value
.span
, "...cannot be constrained to this value")
446 Some(GenericArg
::Lifetime(lt
)) => {
447 self.struct_span_err(span
, "associated lifetimes are not supported")
448 .span_label(lt
.ident
.span
, "the lifetime is given here")
449 .help("if you meant to specify a trait object, write `dyn Trait + 'lifetime`")
453 let after_eq
= eq
.shrink_to_hi();
454 let before_next
= self.token
.span
.shrink_to_lo();
455 self.struct_span_err(after_eq
.to(before_next
), "missing type to the right of `=`")
457 self.sess
.source_map().next_point(eq
).to(before_next
),
458 "to constrain the associated type, add a type after `=`",
459 " TheType".to_string(),
460 Applicability
::HasPlaceholders
,
464 &format
!("remove the `=` if `{}` is a type", ident
),
466 Applicability
::MaybeIncorrect
,
471 Ok(self.mk_ty(span
, ast
::TyKind
::Err
))
474 /// Parse a generic argument in a path segment.
475 /// This does not include constraints, e.g., `Item = u8`, which is handled in `parse_angle_arg`.
476 fn parse_generic_arg(&mut self) -> PResult
<'a
, Option
<GenericArg
>> {
477 let arg
= if self.check_lifetime() && self.look_ahead(1, |t
| !t
.is_like_plus()) {
478 // Parse lifetime argument.
479 GenericArg
::Lifetime(self.expect_lifetime())
480 } else if self.check_const_arg() {
481 // Parse const argument.
482 let expr
= if let token
::OpenDelim(token
::Brace
) = self.token
.kind
{
483 self.parse_block_expr(
486 BlockCheckMode
::Default
,
489 } else if self.token
.is_ident() {
490 // FIXME(const_generics): to distinguish between idents for types and consts,
491 // we should introduce a GenericArg::Ident in the AST and distinguish when
492 // lowering to the HIR. For now, idents for const args are not permitted.
493 if self.token
.is_bool_lit() {
494 self.parse_literal_maybe_minus()?
496 let span
= self.token
.span
;
497 let msg
= "identifiers may currently not be used for const generics";
498 self.struct_span_err(span
, msg
).emit();
499 let block
= self.mk_block_err(span
);
500 self.mk_expr(span
, ast
::ExprKind
::Block(block
, None
), ast
::AttrVec
::new())
503 self.parse_literal_maybe_minus()?
505 GenericArg
::Const(AnonConst { id: ast::DUMMY_NODE_ID, value: expr }
)
506 } else if self.check_type() {
507 // Parse type argument.
508 GenericArg
::Type(self.parse_ty()?
)