1 use super::{Parser, TokenType};
2 use crate::maybe_whole;
3 use rustc_errors::{pluralize, Applicability, PResult};
4 use rustc_span::source_map::{BytePos, Span};
5 use rustc_span::symbol::{kw, sym};
6 use syntax::ast::{self, AngleBracketedArgs, Ident, ParenthesizedArgs, Path, PathSegment, QSelf};
8 AnonConst, AssocTyConstraint, AssocTyConstraintKind, BlockCheckMode, GenericArg,
10 use syntax::token::{self, Token};
15 /// Specifies how to parse a path.
16 #[derive(Copy, Clone, PartialEq)]
18 /// In some contexts, notably in expressions, paths with generic arguments are ambiguous
19 /// with something else. For example, in expressions `segment < ....` can be interpreted
20 /// as a comparison and `segment ( ....` can be interpreted as a function call.
21 /// In all such contexts the non-path interpretation is preferred by default for practical
22 /// reasons, but the path interpretation can be forced by the disambiguator `::`, e.g.
23 /// `x<y>` - comparisons, `x::<y>` - unambiguously a path.
25 /// In other contexts, notably in types, no ambiguity exists and paths can be written
26 /// without the disambiguator, e.g., `x<y>` - unambiguously a path.
27 /// Paths with disambiguators are still accepted, `x::<Y>` - unambiguously a path too.
29 /// A path with generic arguments disallowed, e.g., `foo::bar::Baz`, used in imports,
30 /// visibilities or attributes.
31 /// Technically, this variant is unnecessary and e.g., `Expr` can be used instead
32 /// (paths in "mod" contexts have to be checked later for absence of generic arguments
33 /// anyway, due to macros), but it is used to avoid weird suggestions about expected
34 /// tokens when something goes wrong.
39 /// Parses a qualified path.
40 /// Assumes that the leading `<` has been parsed already.
42 /// `qualified_path = <type [as trait_ref]>::path`
47 /// `<T as U>::F::a<S>` (without disambiguator)
48 /// `<T as U>::F::a::<S>` (with disambiguator)
49 pub(super) fn parse_qpath(&mut self, style: PathStyle) -> PResult<'a, (QSelf, Path)> {
50 let lo = self.prev_span;
51 let ty = self.parse_ty()?;
53 // `path` will contain the prefix of the path up to the `>`,
54 // if any (e.g., `U` in the `<T as U>::*` examples
55 // above). `path_span` has the span of that path, or an empty
56 // span in the case of something like `<T>::Bar`.
57 let (mut path, path_span);
58 if self.eat_keyword(kw::As) {
59 let path_lo = self.token.span;
60 path = self.parse_path(PathStyle::Type)?;
61 path_span = path_lo.to(self.prev_span);
63 path_span = self.token.span.to(self.token.span);
64 path = ast::Path { segments: Vec::new(), span: path_span };
67 // See doc comment for `unmatched_angle_bracket_count`.
68 self.expect(&token::Gt)?;
69 if self.unmatched_angle_bracket_count > 0 {
70 self.unmatched_angle_bracket_count -= 1;
71 debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count);
74 self.expect(&token::ModSep)?;
76 let qself = QSelf { ty, path_span, position: path.segments.len() };
77 self.parse_path_segments(&mut path.segments, style)?;
79 Ok((qself, Path { segments: path.segments, span: lo.to(self.prev_span) }))
82 /// Parses simple paths.
84 /// `path = [::] segment+`
85 /// `segment = ident | ident[::]<args> | ident[::](args) [-> type]`
88 /// `a::b::C<D>` (without disambiguator)
89 /// `a::b::C::<D>` (with disambiguator)
90 /// `Fn(Args)` (without disambiguator)
91 /// `Fn::(Args)` (with disambiguator)
92 pub fn parse_path(&mut self, style: PathStyle) -> PResult<'a, Path> {
93 maybe_whole!(self, NtPath, |path| {
94 if style == PathStyle::Mod && path.segments.iter().any(|segment| segment.args.is_some())
96 self.struct_span_err(path.span, "unexpected generic arguments in path").emit();
101 let lo = self.meta_var_span.unwrap_or(self.token.span);
102 let mut segments = Vec::new();
103 let mod_sep_ctxt = self.token.span.ctxt();
104 if self.eat(&token::ModSep) {
105 segments.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
107 self.parse_path_segments(&mut segments, style)?;
109 Ok(Path { segments, span: lo.to(self.prev_span) })
112 pub(super) fn parse_path_segments(
114 segments: &mut Vec<PathSegment>,
116 ) -> PResult<'a, ()> {
118 let segment = self.parse_path_segment(style)?;
119 if style == PathStyle::Expr {
120 // In order to check for trailing angle brackets, we must have finished
121 // recursing (`parse_path_segment` can indirectly call this function),
122 // that is, the next token must be the highlighted part of the below example:
124 // `Foo::<Bar as Baz<T>>::Qux`
127 // As opposed to the below highlight (if we had only finished the first
130 // `Foo::<Bar as Baz<T>>::Qux`
133 // `PathStyle::Expr` is only provided at the root invocation and never in
134 // `parse_path_segment` to recurse and therefore can be checked to maintain
136 self.check_trailing_angle_brackets(&segment, token::ModSep);
138 segments.push(segment);
140 if self.is_import_coupler() || !self.eat(&token::ModSep) {
146 pub(super) fn parse_path_segment(&mut self, style: PathStyle) -> PResult<'a, PathSegment> {
147 let ident = self.parse_path_segment_ident()?;
149 let is_args_start = |token: &Token| match token.kind {
151 | token::BinOp(token::Shl)
152 | token::OpenDelim(token::Paren)
153 | token::LArrow => true,
156 let check_args_start = |this: &mut Self| {
157 this.expected_tokens.extend_from_slice(&[
158 TokenType::Token(token::Lt),
159 TokenType::Token(token::OpenDelim(token::Paren)),
161 is_args_start(&this.token)
165 if style == PathStyle::Type && check_args_start(self)
166 || style != PathStyle::Mod
167 && self.check(&token::ModSep)
168 && self.look_ahead(1, |t| is_args_start(t))
170 // We use `style == PathStyle::Expr` to check if this is in a recursion or not. If
171 // it isn't, then we reset the unmatched angle bracket count as we're about to start
172 // parsing a new path.
173 if style == PathStyle::Expr {
174 self.unmatched_angle_bracket_count = 0;
175 self.max_angle_bracket_count = 0;
178 // Generic arguments are found - `<`, `(`, `::<` or `::(`.
179 self.eat(&token::ModSep);
180 let lo = self.token.span;
181 let args = if self.eat_lt() {
183 let (args, constraints) =
184 self.parse_generic_args_with_leading_angle_bracket_recovery(style, lo)?;
186 let span = lo.to(self.prev_span);
187 AngleBracketedArgs { args, constraints, span }.into()
190 let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?;
191 let span = ident.span.to(self.prev_span);
192 let output = self.parse_ret_ty(false, false)?;
193 ParenthesizedArgs { inputs, output, span }.into()
196 PathSegment { ident, args, id: ast::DUMMY_NODE_ID }
198 // Generic arguments are not found.
199 PathSegment::from_ident(ident)
204 pub(super) fn parse_path_segment_ident(&mut self) -> PResult<'a, Ident> {
205 match self.token.kind {
206 token::Ident(name, _) if name.is_path_segment_keyword() => {
207 let span = self.token.span;
209 Ok(Ident::new(name, span))
211 _ => self.parse_ident(),
215 /// Parses generic args (within a path segment) with recovery for extra leading angle brackets.
216 /// For the purposes of understanding the parsing logic of generic arguments, this function
217 /// can be thought of being the same as just calling `self.parse_generic_args()` if the source
218 /// had the correct amount of leading angle brackets.
220 /// ```ignore (diagnostics)
221 /// bar::<<<<T as Foo>::Output>();
222 /// ^^ help: remove extra angle brackets
224 fn parse_generic_args_with_leading_angle_bracket_recovery(
228 ) -> PResult<'a, (Vec<GenericArg>, Vec<AssocTyConstraint>)> {
229 // We need to detect whether there are extra leading left angle brackets and produce an
230 // appropriate error and suggestion. This cannot be implemented by looking ahead at
231 // upcoming tokens for a matching `>` character - if there are unmatched `<` tokens
232 // then there won't be matching `>` tokens to find.
234 // To explain how this detection works, consider the following example:
236 // ```ignore (diagnostics)
237 // bar::<<<<T as Foo>::Output>();
238 // ^^ help: remove extra angle brackets
241 // Parsing of the left angle brackets starts in this function. We start by parsing the
242 // `<` token (incrementing the counter of unmatched angle brackets on `Parser` via
245 // *Upcoming tokens:* `<<<<T as Foo>::Output>;`
246 // *Unmatched count:* 1
247 // *`parse_path_segment` calls deep:* 0
249 // This has the effect of recursing as this function is called if a `<` character
250 // is found within the expected generic arguments:
252 // *Upcoming tokens:* `<<<T as Foo>::Output>;`
253 // *Unmatched count:* 2
254 // *`parse_path_segment` calls deep:* 1
256 // Eventually we will have recursed until having consumed all of the `<` tokens and
257 // this will be reflected in the count:
259 // *Upcoming tokens:* `T as Foo>::Output>;`
260 // *Unmatched count:* 4
261 // `parse_path_segment` calls deep:* 3
263 // The parser will continue until reaching the first `>` - this will decrement the
264 // unmatched angle bracket count and return to the parent invocation of this function
265 // having succeeded in parsing:
267 // *Upcoming tokens:* `::Output>;`
268 // *Unmatched count:* 3
269 // *`parse_path_segment` calls deep:* 2
271 // This will continue until the next `>` character which will also return successfully
272 // to the parent invocation of this function and decrement the count:
274 // *Upcoming tokens:* `;`
275 // *Unmatched count:* 2
276 // *`parse_path_segment` calls deep:* 1
278 // At this point, this function will expect to find another matching `>` character but
279 // won't be able to and will return an error. This will continue all the way up the
280 // call stack until the first invocation:
282 // *Upcoming tokens:* `;`
283 // *Unmatched count:* 2
284 // *`parse_path_segment` calls deep:* 0
286 // In doing this, we have managed to work out how many unmatched leading left angle
287 // brackets there are, but we cannot recover as the unmatched angle brackets have
288 // already been consumed. To remedy this, we keep a snapshot of the parser state
289 // before we do the above. We can then inspect whether we ended up with a parsing error
290 // and unmatched left angle brackets and if so, restore the parser state before we
291 // consumed any `<` characters to emit an error and consume the erroneous tokens to
292 // recover by attempting to parse again.
294 // In practice, the recursion of this function is indirect and there will be other
295 // locations that consume some `<` characters - as long as we update the count when
296 // this happens, it isn't an issue.
298 let is_first_invocation = style == PathStyle::Expr;
299 // Take a snapshot before attempting to parse - we can restore this later.
300 let snapshot = if is_first_invocation { Some(self.clone()) } else { None };
302 debug!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)");
303 match self.parse_generic_args() {
304 Ok(value) => Ok(value),
305 Err(ref mut e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => {
306 // Cancel error from being unable to find `>`. We know the error
307 // must have been this due to a non-zero unmatched angle bracket
311 // Swap `self` with our backup of the parser state before attempting to parse
312 // generic arguments.
313 let snapshot = mem::replace(self, snapshot.unwrap());
316 "parse_generic_args_with_leading_angle_bracket_recovery: (snapshot failure) \
317 snapshot.count={:?}",
318 snapshot.unmatched_angle_bracket_count,
321 // Eat the unmatched angle brackets.
322 for _ in 0..snapshot.unmatched_angle_bracket_count {
326 // Make a span over ${unmatched angle bracket count} characters.
327 let span = lo.with_hi(lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count));
328 self.struct_span_err(
331 "unmatched angle bracket{}",
332 pluralize!(snapshot.unmatched_angle_bracket_count)
338 "remove extra angle bracket{}",
339 pluralize!(snapshot.unmatched_angle_bracket_count)
342 Applicability::MachineApplicable,
346 // Try again without unmatched angle bracket characters.
347 self.parse_generic_args()
353 /// Parses (possibly empty) list of lifetime and type arguments and associated type bindings,
354 /// possibly including trailing comma.
355 fn parse_generic_args(&mut self) -> PResult<'a, (Vec<GenericArg>, Vec<AssocTyConstraint>)> {
356 let mut args = Vec::new();
357 let mut constraints = Vec::new();
358 let mut misplaced_assoc_ty_constraints: Vec<Span> = Vec::new();
359 let mut assoc_ty_constraints: Vec<Span> = Vec::new();
361 let args_lo = self.token.span;
364 if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) {
365 // Parse lifetime argument.
366 args.push(GenericArg::Lifetime(self.expect_lifetime()));
367 misplaced_assoc_ty_constraints.append(&mut assoc_ty_constraints);
368 } else if self.check_ident()
369 && self.look_ahead(1, |t| t == &token::Eq || t == &token::Colon)
371 // Parse associated type constraint.
372 let lo = self.token.span;
373 let ident = self.parse_ident()?;
374 let kind = if self.eat(&token::Eq) {
375 AssocTyConstraintKind::Equality { ty: self.parse_ty()? }
376 } else if self.eat(&token::Colon) {
377 AssocTyConstraintKind::Bound {
378 bounds: self.parse_generic_bounds(Some(self.prev_span))?,
384 let span = lo.to(self.prev_span);
386 // Gate associated type bounds, e.g., `Iterator<Item: Ord>`.
387 if let AssocTyConstraintKind::Bound { .. } = kind {
388 self.sess.gated_spans.gate(sym::associated_type_bounds, span);
391 constraints.push(AssocTyConstraint { id: ast::DUMMY_NODE_ID, ident, kind, span });
392 assoc_ty_constraints.push(span);
393 } else if self.check_const_arg() {
394 // Parse const argument.
395 let expr = if let token::OpenDelim(token::Brace) = self.token.kind {
396 self.parse_block_expr(
399 BlockCheckMode::Default,
402 } else if self.token.is_ident() {
403 // FIXME(const_generics): to distinguish between idents for types and consts,
404 // we should introduce a GenericArg::Ident in the AST and distinguish when
405 // lowering to the HIR. For now, idents for const args are not permitted.
406 if self.token.is_bool_lit() {
407 self.parse_literal_maybe_minus()?
409 let span = self.token.span;
410 let msg = "identifiers may currently not be used for const generics";
411 self.struct_span_err(span, msg).emit();
412 let block = self.mk_block_err(span);
413 self.mk_expr(span, ast::ExprKind::Block(block, None), ast::AttrVec::new())
416 self.parse_literal_maybe_minus()?
418 let value = AnonConst { id: ast::DUMMY_NODE_ID, value: expr };
419 args.push(GenericArg::Const(value));
420 misplaced_assoc_ty_constraints.append(&mut assoc_ty_constraints);
421 } else if self.check_type() {
422 // Parse type argument.
423 args.push(GenericArg::Type(self.parse_ty()?));
424 misplaced_assoc_ty_constraints.append(&mut assoc_ty_constraints);
429 if !self.eat(&token::Comma) {
434 // FIXME: we would like to report this in ast_validation instead, but we currently do not
435 // preserve ordering of generic parameters with respect to associated type binding, so we
436 // lose that information after parsing.
437 if misplaced_assoc_ty_constraints.len() > 0 {
438 let mut err = self.struct_span_err(
439 args_lo.to(self.prev_span),
440 "associated type bindings must be declared after generic parameters",
442 for span in misplaced_assoc_ty_constraints {
445 "this associated type binding should be moved after the generic parameters",
451 Ok((args, constraints))