]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_parse/src/parser/path.rs
Auto merge of #77616 - jyn514:no-normalize, r=lcnr
[rust.git] / compiler / rustc_parse / src / parser / path.rs
1 use super::ty::{AllowPlus, RecoverQPath};
2 use super::{Parser, TokenType};
3 use crate::maybe_whole;
4 use rustc_ast::ptr::P;
5 use rustc_ast::token::{self, Token};
6 use rustc_ast::{self as ast, AngleBracketedArg, AngleBracketedArgs, ParenthesizedArgs};
7 use rustc_ast::{AnonConst, AssocTyConstraint, AssocTyConstraintKind, BlockCheckMode};
8 use rustc_ast::{GenericArg, GenericArgs};
9 use rustc_ast::{Path, PathSegment, QSelf};
10 use rustc_errors::{pluralize, Applicability, PResult};
11 use rustc_span::source_map::{BytePos, Span};
12 use rustc_span::symbol::{kw, sym, Ident};
13
14 use std::mem;
15 use tracing::debug;
16
17 /// Specifies how to parse a path.
18 #[derive(Copy, Clone, PartialEq)]
19 pub enum PathStyle {
20     /// In some contexts, notably in expressions, paths with generic arguments are ambiguous
21     /// with something else. For example, in expressions `segment < ....` can be interpreted
22     /// as a comparison and `segment ( ....` can be interpreted as a function call.
23     /// In all such contexts the non-path interpretation is preferred by default for practical
24     /// reasons, but the path interpretation can be forced by the disambiguator `::`, e.g.
25     /// `x<y>` - comparisons, `x::<y>` - unambiguously a path.
26     Expr,
27     /// In other contexts, notably in types, no ambiguity exists and paths can be written
28     /// without the disambiguator, e.g., `x<y>` - unambiguously a path.
29     /// Paths with disambiguators are still accepted, `x::<Y>` - unambiguously a path too.
30     Type,
31     /// A path with generic arguments disallowed, e.g., `foo::bar::Baz`, used in imports,
32     /// visibilities or attributes.
33     /// Technically, this variant is unnecessary and e.g., `Expr` can be used instead
34     /// (paths in "mod" contexts have to be checked later for absence of generic arguments
35     /// anyway, due to macros), but it is used to avoid weird suggestions about expected
36     /// tokens when something goes wrong.
37     Mod,
38 }
39
40 impl<'a> Parser<'a> {
41     /// Parses a qualified path.
42     /// Assumes that the leading `<` has been parsed already.
43     ///
44     /// `qualified_path = <type [as trait_ref]>::path`
45     ///
46     /// # Examples
47     /// `<T>::default`
48     /// `<T as U>::a`
49     /// `<T as U>::F::a<S>` (without disambiguator)
50     /// `<T as U>::F::a::<S>` (with disambiguator)
51     pub(super) fn parse_qpath(&mut self, style: PathStyle) -> PResult<'a, (QSelf, Path)> {
52         let lo = self.prev_token.span;
53         let ty = self.parse_ty()?;
54
55         // `path` will contain the prefix of the path up to the `>`,
56         // if any (e.g., `U` in the `<T as U>::*` examples
57         // above). `path_span` has the span of that path, or an empty
58         // span in the case of something like `<T>::Bar`.
59         let (mut path, path_span);
60         if self.eat_keyword(kw::As) {
61             let path_lo = self.token.span;
62             path = self.parse_path(PathStyle::Type)?;
63             path_span = path_lo.to(self.prev_token.span);
64         } else {
65             path_span = self.token.span.to(self.token.span);
66             path = ast::Path { segments: Vec::new(), span: path_span, tokens: None };
67         }
68
69         // See doc comment for `unmatched_angle_bracket_count`.
70         self.expect(&token::Gt)?;
71         if self.unmatched_angle_bracket_count > 0 {
72             self.unmatched_angle_bracket_count -= 1;
73             debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count);
74         }
75
76         if !self.recover_colon_before_qpath_proj() {
77             self.expect(&token::ModSep)?;
78         }
79
80         let qself = QSelf { ty, path_span, position: path.segments.len() };
81         self.parse_path_segments(&mut path.segments, style)?;
82
83         Ok((
84             qself,
85             Path { segments: path.segments, span: lo.to(self.prev_token.span), tokens: None },
86         ))
87     }
88
89     /// Recover from an invalid single colon, when the user likely meant a qualified path.
90     /// We avoid emitting this if not followed by an identifier, as our assumption that the user
91     /// intended this to be a qualified path may not be correct.
92     ///
93     /// ```ignore (diagnostics)
94     /// <Bar as Baz<T>>:Qux
95     ///                ^ help: use double colon
96     /// ```
97     fn recover_colon_before_qpath_proj(&mut self) -> bool {
98         if self.token.kind != token::Colon
99             || self.look_ahead(1, |t| !t.is_ident() || t.is_reserved_ident())
100         {
101             return false;
102         }
103
104         self.bump(); // colon
105
106         self.diagnostic()
107             .struct_span_err(
108                 self.prev_token.span,
109                 "found single colon before projection in qualified path",
110             )
111             .span_suggestion(
112                 self.prev_token.span,
113                 "use double colon",
114                 "::".to_string(),
115                 Applicability::MachineApplicable,
116             )
117             .emit();
118
119         true
120     }
121
122     /// Parses simple paths.
123     ///
124     /// `path = [::] segment+`
125     /// `segment = ident | ident[::]<args> | ident[::](args) [-> type]`
126     ///
127     /// # Examples
128     /// `a::b::C<D>` (without disambiguator)
129     /// `a::b::C::<D>` (with disambiguator)
130     /// `Fn(Args)` (without disambiguator)
131     /// `Fn::(Args)` (with disambiguator)
132     pub(super) fn parse_path(&mut self, style: PathStyle) -> PResult<'a, Path> {
133         maybe_whole!(self, NtPath, |path| {
134             if style == PathStyle::Mod && path.segments.iter().any(|segment| segment.args.is_some())
135             {
136                 self.struct_span_err(path.span, "unexpected generic arguments in path").emit();
137             }
138             path
139         });
140
141         let lo = self.token.span;
142         let mut segments = Vec::new();
143         let mod_sep_ctxt = self.token.span.ctxt();
144         if self.eat(&token::ModSep) {
145             segments.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
146         }
147         self.parse_path_segments(&mut segments, style)?;
148
149         Ok(Path { segments, span: lo.to(self.prev_token.span), tokens: None })
150     }
151
152     pub(super) fn parse_path_segments(
153         &mut self,
154         segments: &mut Vec<PathSegment>,
155         style: PathStyle,
156     ) -> PResult<'a, ()> {
157         loop {
158             let segment = self.parse_path_segment(style)?;
159             if style == PathStyle::Expr {
160                 // In order to check for trailing angle brackets, we must have finished
161                 // recursing (`parse_path_segment` can indirectly call this function),
162                 // that is, the next token must be the highlighted part of the below example:
163                 //
164                 // `Foo::<Bar as Baz<T>>::Qux`
165                 //                      ^ here
166                 //
167                 // As opposed to the below highlight (if we had only finished the first
168                 // recursion):
169                 //
170                 // `Foo::<Bar as Baz<T>>::Qux`
171                 //                     ^ here
172                 //
173                 // `PathStyle::Expr` is only provided at the root invocation and never in
174                 // `parse_path_segment` to recurse and therefore can be checked to maintain
175                 // this invariant.
176                 self.check_trailing_angle_brackets(&segment, &[&token::ModSep]);
177             }
178             segments.push(segment);
179
180             if self.is_import_coupler() || !self.eat(&token::ModSep) {
181                 return Ok(());
182             }
183         }
184     }
185
186     pub(super) fn parse_path_segment(&mut self, style: PathStyle) -> PResult<'a, PathSegment> {
187         let ident = self.parse_path_segment_ident()?;
188
189         let is_args_start = |token: &Token| {
190             matches!(
191                 token.kind,
192                 token::Lt
193                     | token::BinOp(token::Shl)
194                     | token::OpenDelim(token::Paren)
195                     | token::LArrow
196             )
197         };
198         let check_args_start = |this: &mut Self| {
199             this.expected_tokens.extend_from_slice(&[
200                 TokenType::Token(token::Lt),
201                 TokenType::Token(token::OpenDelim(token::Paren)),
202             ]);
203             is_args_start(&this.token)
204         };
205
206         Ok(
207             if style == PathStyle::Type && check_args_start(self)
208                 || style != PathStyle::Mod
209                     && self.check(&token::ModSep)
210                     && self.look_ahead(1, |t| is_args_start(t))
211             {
212                 // We use `style == PathStyle::Expr` to check if this is in a recursion or not. If
213                 // it isn't, then we reset the unmatched angle bracket count as we're about to start
214                 // parsing a new path.
215                 if style == PathStyle::Expr {
216                     self.unmatched_angle_bracket_count = 0;
217                     self.max_angle_bracket_count = 0;
218                 }
219
220                 // Generic arguments are found - `<`, `(`, `::<` or `::(`.
221                 self.eat(&token::ModSep);
222                 let lo = self.token.span;
223                 let args = if self.eat_lt() {
224                     // `<'a, T, A = U>`
225                     let args =
226                         self.parse_angle_args_with_leading_angle_bracket_recovery(style, lo)?;
227                     self.expect_gt()?;
228                     let span = lo.to(self.prev_token.span);
229                     AngleBracketedArgs { args, span }.into()
230                 } else {
231                     // `(T, U) -> R`
232                     let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?;
233                     let span = ident.span.to(self.prev_token.span);
234                     let output = self.parse_ret_ty(AllowPlus::No, RecoverQPath::No)?;
235                     ParenthesizedArgs { inputs, output, span }.into()
236                 };
237
238                 PathSegment { ident, args, id: ast::DUMMY_NODE_ID }
239             } else {
240                 // Generic arguments are not found.
241                 PathSegment::from_ident(ident)
242             },
243         )
244     }
245
246     pub(super) fn parse_path_segment_ident(&mut self) -> PResult<'a, Ident> {
247         match self.token.ident() {
248             Some((ident, false)) if ident.is_path_segment_keyword() => {
249                 self.bump();
250                 Ok(ident)
251             }
252             _ => self.parse_ident(),
253         }
254     }
255
256     /// Parses generic args (within a path segment) with recovery for extra leading angle brackets.
257     /// For the purposes of understanding the parsing logic of generic arguments, this function
258     /// can be thought of being the same as just calling `self.parse_angle_args()` if the source
259     /// had the correct amount of leading angle brackets.
260     ///
261     /// ```ignore (diagnostics)
262     /// bar::<<<<T as Foo>::Output>();
263     ///      ^^ help: remove extra angle brackets
264     /// ```
265     fn parse_angle_args_with_leading_angle_bracket_recovery(
266         &mut self,
267         style: PathStyle,
268         lo: Span,
269     ) -> PResult<'a, Vec<AngleBracketedArg>> {
270         // We need to detect whether there are extra leading left angle brackets and produce an
271         // appropriate error and suggestion. This cannot be implemented by looking ahead at
272         // upcoming tokens for a matching `>` character - if there are unmatched `<` tokens
273         // then there won't be matching `>` tokens to find.
274         //
275         // To explain how this detection works, consider the following example:
276         //
277         // ```ignore (diagnostics)
278         // bar::<<<<T as Foo>::Output>();
279         //      ^^ help: remove extra angle brackets
280         // ```
281         //
282         // Parsing of the left angle brackets starts in this function. We start by parsing the
283         // `<` token (incrementing the counter of unmatched angle brackets on `Parser` via
284         // `eat_lt`):
285         //
286         // *Upcoming tokens:* `<<<<T as Foo>::Output>;`
287         // *Unmatched count:* 1
288         // *`parse_path_segment` calls deep:* 0
289         //
290         // This has the effect of recursing as this function is called if a `<` character
291         // is found within the expected generic arguments:
292         //
293         // *Upcoming tokens:* `<<<T as Foo>::Output>;`
294         // *Unmatched count:* 2
295         // *`parse_path_segment` calls deep:* 1
296         //
297         // Eventually we will have recursed until having consumed all of the `<` tokens and
298         // this will be reflected in the count:
299         //
300         // *Upcoming tokens:* `T as Foo>::Output>;`
301         // *Unmatched count:* 4
302         // `parse_path_segment` calls deep:* 3
303         //
304         // The parser will continue until reaching the first `>` - this will decrement the
305         // unmatched angle bracket count and return to the parent invocation of this function
306         // having succeeded in parsing:
307         //
308         // *Upcoming tokens:* `::Output>;`
309         // *Unmatched count:* 3
310         // *`parse_path_segment` calls deep:* 2
311         //
312         // This will continue until the next `>` character which will also return successfully
313         // to the parent invocation of this function and decrement the count:
314         //
315         // *Upcoming tokens:* `;`
316         // *Unmatched count:* 2
317         // *`parse_path_segment` calls deep:* 1
318         //
319         // At this point, this function will expect to find another matching `>` character but
320         // won't be able to and will return an error. This will continue all the way up the
321         // call stack until the first invocation:
322         //
323         // *Upcoming tokens:* `;`
324         // *Unmatched count:* 2
325         // *`parse_path_segment` calls deep:* 0
326         //
327         // In doing this, we have managed to work out how many unmatched leading left angle
328         // brackets there are, but we cannot recover as the unmatched angle brackets have
329         // already been consumed. To remedy this, we keep a snapshot of the parser state
330         // before we do the above. We can then inspect whether we ended up with a parsing error
331         // and unmatched left angle brackets and if so, restore the parser state before we
332         // consumed any `<` characters to emit an error and consume the erroneous tokens to
333         // recover by attempting to parse again.
334         //
335         // In practice, the recursion of this function is indirect and there will be other
336         // locations that consume some `<` characters - as long as we update the count when
337         // this happens, it isn't an issue.
338
339         let is_first_invocation = style == PathStyle::Expr;
340         // Take a snapshot before attempting to parse - we can restore this later.
341         let snapshot = if is_first_invocation { Some(self.clone()) } else { None };
342
343         debug!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)");
344         match self.parse_angle_args() {
345             Ok(args) => Ok(args),
346             Err(ref mut e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => {
347                 // Cancel error from being unable to find `>`. We know the error
348                 // must have been this due to a non-zero unmatched angle bracket
349                 // count.
350                 e.cancel();
351
352                 // Swap `self` with our backup of the parser state before attempting to parse
353                 // generic arguments.
354                 let snapshot = mem::replace(self, snapshot.unwrap());
355
356                 debug!(
357                     "parse_generic_args_with_leading_angle_bracket_recovery: (snapshot failure) \
358                      snapshot.count={:?}",
359                     snapshot.unmatched_angle_bracket_count,
360                 );
361
362                 // Eat the unmatched angle brackets.
363                 for _ in 0..snapshot.unmatched_angle_bracket_count {
364                     self.eat_lt();
365                 }
366
367                 // Make a span over ${unmatched angle bracket count} characters.
368                 let span = lo.with_hi(lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count));
369                 self.struct_span_err(
370                     span,
371                     &format!(
372                         "unmatched angle bracket{}",
373                         pluralize!(snapshot.unmatched_angle_bracket_count)
374                     ),
375                 )
376                 .span_suggestion(
377                     span,
378                     &format!(
379                         "remove extra angle bracket{}",
380                         pluralize!(snapshot.unmatched_angle_bracket_count)
381                     ),
382                     String::new(),
383                     Applicability::MachineApplicable,
384                 )
385                 .emit();
386
387                 // Try again without unmatched angle bracket characters.
388                 self.parse_angle_args()
389             }
390             Err(e) => Err(e),
391         }
392     }
393
394     /// Parses (possibly empty) list of generic arguments / associated item constraints,
395     /// possibly including trailing comma.
396     pub(super) fn parse_angle_args(&mut self) -> PResult<'a, Vec<AngleBracketedArg>> {
397         let mut args = Vec::new();
398         while let Some(arg) = self.parse_angle_arg()? {
399             args.push(arg);
400             if !self.eat(&token::Comma) {
401                 if !self.token.kind.should_end_const_arg() {
402                     if self.handle_ambiguous_unbraced_const_arg(&mut args)? {
403                         // We've managed to (partially) recover, so continue trying to parse
404                         // arguments.
405                         continue;
406                     }
407                 }
408                 break;
409             }
410         }
411         Ok(args)
412     }
413
414     /// Parses a single argument in the angle arguments `<...>` of a path segment.
415     fn parse_angle_arg(&mut self) -> PResult<'a, Option<AngleBracketedArg>> {
416         let lo = self.token.span;
417         let arg = self.parse_generic_arg()?;
418         match arg {
419             Some(arg) => {
420                 if self.check(&token::Colon) | self.check(&token::Eq) {
421                     let (ident, gen_args) = self.get_ident_from_generic_arg(arg, lo)?;
422                     let kind = if self.eat(&token::Colon) {
423                         // Parse associated type constraint bound.
424
425                         let bounds = self.parse_generic_bounds(Some(self.prev_token.span))?;
426                         AssocTyConstraintKind::Bound { bounds }
427                     } else if self.eat(&token::Eq) {
428                         // Parse associated type equality constraint
429
430                         let ty = self.parse_assoc_equality_term(ident, self.prev_token.span)?;
431                         AssocTyConstraintKind::Equality { ty }
432                     } else {
433                         unreachable!();
434                     };
435
436                     let span = lo.to(self.prev_token.span);
437
438                     // Gate associated type bounds, e.g., `Iterator<Item: Ord>`.
439                     if let AssocTyConstraintKind::Bound { .. } = kind {
440                         self.sess.gated_spans.gate(sym::associated_type_bounds, span);
441                     }
442                     let constraint =
443                         AssocTyConstraint { id: ast::DUMMY_NODE_ID, ident, gen_args, kind, span };
444                     Ok(Some(AngleBracketedArg::Constraint(constraint)))
445                 } else {
446                     Ok(Some(AngleBracketedArg::Arg(arg)))
447                 }
448             }
449             _ => Ok(None),
450         }
451     }
452
453     /// Parse the term to the right of an associated item equality constraint.
454     /// That is, parse `<term>` in `Item = <term>`.
455     /// Right now, this only admits types in `<term>`.
456     fn parse_assoc_equality_term(&mut self, ident: Ident, eq: Span) -> PResult<'a, P<ast::Ty>> {
457         let arg = self.parse_generic_arg()?;
458         let span = ident.span.to(self.prev_token.span);
459         match arg {
460             Some(GenericArg::Type(ty)) => return Ok(ty),
461             Some(GenericArg::Const(expr)) => {
462                 self.struct_span_err(span, "cannot constrain an associated constant to a value")
463                     .span_label(ident.span, "this associated constant...")
464                     .span_label(expr.value.span, "...cannot be constrained to this value")
465                     .emit();
466             }
467             Some(GenericArg::Lifetime(lt)) => {
468                 self.struct_span_err(span, "associated lifetimes are not supported")
469                     .span_label(lt.ident.span, "the lifetime is given here")
470                     .help("if you meant to specify a trait object, write `dyn Trait + 'lifetime`")
471                     .emit();
472             }
473             None => {
474                 let after_eq = eq.shrink_to_hi();
475                 let before_next = self.token.span.shrink_to_lo();
476                 self.struct_span_err(after_eq.to(before_next), "missing type to the right of `=`")
477                     .span_suggestion(
478                         self.sess.source_map().next_point(eq).to(before_next),
479                         "to constrain the associated type, add a type after `=`",
480                         " TheType".to_string(),
481                         Applicability::HasPlaceholders,
482                     )
483                     .span_suggestion(
484                         eq.to(before_next),
485                         &format!("remove the `=` if `{}` is a type", ident),
486                         String::new(),
487                         Applicability::MaybeIncorrect,
488                     )
489                     .emit();
490             }
491         }
492         Ok(self.mk_ty(span, ast::TyKind::Err))
493     }
494
495     /// We do not permit arbitrary expressions as const arguments. They must be one of:
496     /// - An expression surrounded in `{}`.
497     /// - A literal.
498     /// - A numeric literal prefixed by `-`.
499     /// - A single-segment path.
500     pub(super) fn expr_is_valid_const_arg(&self, expr: &P<rustc_ast::Expr>) -> bool {
501         match &expr.kind {
502             ast::ExprKind::Block(_, _) | ast::ExprKind::Lit(_) => true,
503             ast::ExprKind::Unary(ast::UnOp::Neg, expr) => match &expr.kind {
504                 ast::ExprKind::Lit(_) => true,
505                 _ => false,
506             },
507             // We can only resolve single-segment paths at the moment, because multi-segment paths
508             // require type-checking: see `visit_generic_arg` in `src/librustc_resolve/late.rs`.
509             ast::ExprKind::Path(None, path)
510                 if path.segments.len() == 1 && path.segments[0].args.is_none() =>
511             {
512                 true
513             }
514             _ => false,
515         }
516     }
517
518     /// Parse a generic argument in a path segment.
519     /// This does not include constraints, e.g., `Item = u8`, which is handled in `parse_angle_arg`.
520     fn parse_generic_arg(&mut self) -> PResult<'a, Option<GenericArg>> {
521         let start = self.token.span;
522         let arg = if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) {
523             // Parse lifetime argument.
524             GenericArg::Lifetime(self.expect_lifetime())
525         } else if self.check_const_arg() {
526             // Parse const argument.
527             let value = if let token::OpenDelim(token::Brace) = self.token.kind {
528                 self.parse_block_expr(
529                     None,
530                     self.token.span,
531                     BlockCheckMode::Default,
532                     ast::AttrVec::new(),
533                 )?
534             } else {
535                 self.handle_unambiguous_unbraced_const_arg()?
536             };
537             GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value })
538         } else if self.check_type() {
539             // Parse type argument.
540             match self.parse_ty() {
541                 Ok(ty) => GenericArg::Type(ty),
542                 Err(err) => {
543                     // Try to recover from possible `const` arg without braces.
544                     return self.recover_const_arg(start, err).map(Some);
545                 }
546             }
547         } else {
548             return Ok(None);
549         };
550         Ok(Some(arg))
551     }
552
553     fn get_ident_from_generic_arg(
554         &self,
555         gen_arg: GenericArg,
556         lo: Span,
557     ) -> PResult<'a, (Ident, Option<GenericArgs>)> {
558         let gen_arg_span = gen_arg.span();
559         match gen_arg {
560             GenericArg::Type(t) => match t.into_inner().kind {
561                 ast::TyKind::Path(qself, mut path) => {
562                     if let Some(qself) = qself {
563                         let mut err = self.struct_span_err(
564                             gen_arg_span,
565                             "qualified paths cannot be used in associated type constraints",
566                         );
567                         err.span_label(
568                             qself.path_span,
569                             "not allowed in associated type constraints",
570                         );
571                         return Err(err);
572                     }
573                     if path.segments.len() == 1 {
574                         let path_seg = path.segments.remove(0);
575                         let ident = path_seg.ident;
576                         let gen_args = path_seg.args.map(|args| args.into_inner());
577                         return Ok((ident, gen_args));
578                     }
579                     let err = self.struct_span_err(
580                         path.span,
581                         "paths with multiple segments cannot be used in associated type constraints",
582                     );
583                     return Err(err);
584                 }
585                 _ => {
586                     let span = lo.to(self.prev_token.span);
587                     let err = self.struct_span_err(
588                         span,
589                         "only path types can be used in associated type constraints",
590                     );
591                     return Err(err);
592                 }
593             },
594             _ => {
595                 let span = lo.to(self.prev_token.span);
596                 let err = self
597                     .struct_span_err(span, "only types can be used in associated type constraints");
598                 return Err(err);
599             }
600         }
601     }
602 }