rustc_parse/parser/
path.rs

1use std::mem;
2
3use ast::token::IdentIsRaw;
4use rustc_ast::token::{self, MetaVarKind, Token, TokenKind};
5use rustc_ast::{
6    self as ast, AngleBracketedArg, AngleBracketedArgs, AnonConst, AssocItemConstraint,
7    AssocItemConstraintKind, BlockCheckMode, GenericArg, GenericArgs, Generics, MgcaDisambiguation,
8    ParenthesizedArgs, Path, PathSegment, QSelf,
9};
10use rustc_errors::{Applicability, Diag, PResult};
11use rustc_span::{BytePos, Ident, Span, kw, sym};
12use thin_vec::ThinVec;
13use tracing::debug;
14
15use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
16use super::{Parser, Restrictions, TokenType};
17use crate::ast::{PatKind, TyKind};
18use crate::errors::{
19    self, AttributeOnEmptyType, AttributeOnGenericArg, ConstGenericWithoutBraces,
20    ConstGenericWithoutBracesSugg, FnPathFoundNamedParams, PathFoundAttributeInParams,
21    PathFoundCVariadicParams, PathSingleColon, PathTripleColon,
22};
23use crate::exp;
24use crate::parser::{
25    CommaRecoveryMode, Expr, ExprKind, FnContext, FnParseMode, RecoverColon, RecoverComma,
26};
27
28/// Specifies how to parse a path.
29#[derive(Copy, Clone, PartialEq)]
30pub enum PathStyle {
31    /// In some contexts, notably in expressions, paths with generic arguments are ambiguous
32    /// with something else. For example, in expressions `segment < ....` can be interpreted
33    /// as a comparison and `segment ( ....` can be interpreted as a function call.
34    /// In all such contexts the non-path interpretation is preferred by default for practical
35    /// reasons, but the path interpretation can be forced by the disambiguator `::`, e.g.
36    /// `x<y>` - comparisons, `x::<y>` - unambiguously a path.
37    ///
38    /// Also, a path may never be followed by a `:`. This means that we can eagerly recover if
39    /// we encounter it.
40    Expr,
41    /// The same as `Expr`, but may be followed by a `:`.
42    /// For example, this code:
43    /// ```rust
44    /// struct S;
45    ///
46    /// let S: S;
47    /// //  ^ Followed by a `:`
48    /// ```
49    Pat,
50    /// In other contexts, notably in types, no ambiguity exists and paths can be written
51    /// without the disambiguator, e.g., `x<y>` - unambiguously a path.
52    /// Paths with disambiguators are still accepted, `x::<Y>` - unambiguously a path too.
53    Type,
54    /// A path with generic arguments disallowed, e.g., `foo::bar::Baz`, used in imports,
55    /// visibilities or attributes.
56    /// Technically, this variant is unnecessary and e.g., `Expr` can be used instead
57    /// (paths in "mod" contexts have to be checked later for absence of generic arguments
58    /// anyway, due to macros), but it is used to avoid weird suggestions about expected
59    /// tokens when something goes wrong.
60    Mod,
61}
62
63impl PathStyle {
64    fn has_generic_ambiguity(&self) -> bool {
65        matches!(self, Self::Expr | Self::Pat)
66    }
67}
68
69impl<'a> Parser<'a> {
70    /// Parses a qualified path.
71    /// Assumes that the leading `<` has been parsed already.
72    ///
73    /// `qualified_path = <type [as trait_ref]>::path`
74    ///
75    /// # Examples
76    /// `<T>::default`
77    /// `<T as U>::a`
78    /// `<T as U>::F::a<S>` (without disambiguator)
79    /// `<T as U>::F::a::<S>` (with disambiguator)
80    pub(super) fn parse_qpath(&mut self, style: PathStyle) -> PResult<'a, (Box<QSelf>, Path)> {
81        let lo = self.prev_token.span;
82        let ty = self.parse_ty()?;
83
84        // `path` will contain the prefix of the path up to the `>`,
85        // if any (e.g., `U` in the `<T as U>::*` examples
86        // above). `path_span` has the span of that path, or an empty
87        // span in the case of something like `<T>::Bar`.
88        let (mut path, path_span);
89        if self.eat_keyword(exp!(As)) {
90            let path_lo = self.token.span;
91            path = self.parse_path(PathStyle::Type)?;
92            path_span = path_lo.to(self.prev_token.span);
93        } else {
94            path_span = self.token.span.to(self.token.span);
95            path = ast::Path { segments: ThinVec::new(), span: path_span, tokens: None };
96        }
97
98        // See doc comment for `unmatched_angle_bracket_count`.
99        self.expect(exp!(Gt))?;
100        if self.unmatched_angle_bracket_count > 0 {
101            self.unmatched_angle_bracket_count -= 1;
102            debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count);
103        }
104
105        let is_import_coupler = self.is_import_coupler();
106        if !is_import_coupler && !self.recover_colon_before_qpath_proj() {
107            self.expect(exp!(PathSep))?;
108        }
109
110        let qself = Box::new(QSelf { ty, path_span, position: path.segments.len() });
111        if !is_import_coupler {
112            self.parse_path_segments(&mut path.segments, style, None)?;
113        }
114
115        Ok((
116            qself,
117            Path { segments: path.segments, span: lo.to(self.prev_token.span), tokens: None },
118        ))
119    }
120
121    /// Recover from an invalid single colon, when the user likely meant a qualified path.
122    /// We avoid emitting this if not followed by an identifier, as our assumption that the user
123    /// intended this to be a qualified path may not be correct.
124    ///
125    /// ```ignore (diagnostics)
126    /// <Bar as Baz<T>>:Qux
127    ///                ^ help: use double colon
128    /// ```
129    fn recover_colon_before_qpath_proj(&mut self) -> bool {
130        if !self.check_noexpect(&TokenKind::Colon)
131            || self.look_ahead(1, |t| !t.is_non_reserved_ident())
132        {
133            return false;
134        }
135
136        self.bump(); // colon
137
138        self.dcx()
139            .struct_span_err(
140                self.prev_token.span,
141                "found single colon before projection in qualified path",
142            )
143            .with_span_suggestion(
144                self.prev_token.span,
145                "use double colon",
146                "::",
147                Applicability::MachineApplicable,
148            )
149            .emit();
150
151        true
152    }
153
154    pub fn parse_path(&mut self, style: PathStyle) -> PResult<'a, Path> {
155        self.parse_path_inner(style, None)
156    }
157
158    /// Parses simple paths.
159    ///
160    /// `path = [::] segment+`
161    /// `segment = ident | ident[::]<args> | ident[::](args) [-> type]`
162    ///
163    /// # Examples
164    /// `a::b::C<D>` (without disambiguator)
165    /// `a::b::C::<D>` (with disambiguator)
166    /// `Fn(Args)` (without disambiguator)
167    /// `Fn::(Args)` (with disambiguator)
168    pub(super) fn parse_path_inner(
169        &mut self,
170        style: PathStyle,
171        ty_generics: Option<&Generics>,
172    ) -> PResult<'a, Path> {
173        let reject_generics_if_mod_style = |parser: &Parser<'_>, path: Path| {
174            // Ensure generic arguments don't end up in attribute paths, such as:
175            //
176            //     macro_rules! m {
177            //         ($p:path) => { #[$p] struct S; }
178            //     }
179            //
180            //     m!(inline<u8>); //~ ERROR: unexpected generic arguments in path
181            //
182            if style == PathStyle::Mod && path.segments.iter().any(|segment| segment.args.is_some())
183            {
184                let span = path
185                    .segments
186                    .iter()
187                    .filter_map(|segment| segment.args.as_ref())
188                    .map(|arg| arg.span())
189                    .collect::<Vec<_>>();
190                parser.dcx().emit_err(errors::GenericsInPath { span });
191                // Ignore these arguments to prevent unexpected behaviors.
192                let segments = path
193                    .segments
194                    .iter()
195                    .map(|segment| PathSegment { ident: segment.ident, id: segment.id, args: None })
196                    .collect();
197                Path { segments, ..path }
198            } else {
199                path
200            }
201        };
202
203        if let Some(path) =
204            self.eat_metavar_seq(MetaVarKind::Path, |this| this.parse_path(PathStyle::Type))
205        {
206            return Ok(reject_generics_if_mod_style(self, path));
207        }
208
209        // If we have a `ty` metavar in the form of a path, reparse it directly as a path, instead
210        // of reparsing it as a `ty` and then extracting the path.
211        if let Some(path) = self.eat_metavar_seq(MetaVarKind::Ty { is_path: true }, |this| {
212            this.parse_path(PathStyle::Type)
213        }) {
214            return Ok(reject_generics_if_mod_style(self, path));
215        }
216
217        let lo = self.token.span;
218        let mut segments = ThinVec::new();
219        let mod_sep_ctxt = self.token.span.ctxt();
220        if self.eat_path_sep() {
221            segments.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
222        }
223        self.parse_path_segments(&mut segments, style, ty_generics)?;
224        Ok(Path { segments, span: lo.to(self.prev_token.span), tokens: None })
225    }
226
227    pub(super) fn parse_path_segments(
228        &mut self,
229        segments: &mut ThinVec<PathSegment>,
230        style: PathStyle,
231        ty_generics: Option<&Generics>,
232    ) -> PResult<'a, ()> {
233        loop {
234            let segment = self.parse_path_segment(style, ty_generics)?;
235            if style.has_generic_ambiguity() {
236                // In order to check for trailing angle brackets, we must have finished
237                // recursing (`parse_path_segment` can indirectly call this function),
238                // that is, the next token must be the highlighted part of the below example:
239                //
240                // `Foo::<Bar as Baz<T>>::Qux`
241                //                      ^ here
242                //
243                // As opposed to the below highlight (if we had only finished the first
244                // recursion):
245                //
246                // `Foo::<Bar as Baz<T>>::Qux`
247                //                     ^ here
248                //
249                // `PathStyle::Expr` is only provided at the root invocation and never in
250                // `parse_path_segment` to recurse and therefore can be checked to maintain
251                // this invariant.
252                self.check_trailing_angle_brackets(&segment, &[exp!(PathSep)]);
253            }
254            segments.push(segment);
255
256            if self.is_import_coupler() || !self.eat_path_sep() {
257                // IMPORTANT: We can *only ever* treat single colons as typo'ed double colons in
258                // expression contexts (!) since only there paths cannot possibly be followed by
259                // a colon and still form a syntactically valid construct. In pattern contexts,
260                // a path may be followed by a type annotation. E.g., `let pat:ty`. In type
261                // contexts, a path may be followed by a list of bounds. E.g., `where ty:bound`.
262                if self.may_recover()
263                    && style == PathStyle::Expr // (!)
264                    && self.token == token::Colon
265                    && self.look_ahead(1, |token| token.is_non_reserved_ident())
266                {
267                    // Emit a special error message for `a::b:c` to help users
268                    // otherwise, `a: c` might have meant to introduce a new binding
269                    if self.token.span.lo() == self.prev_token.span.hi()
270                        && self.look_ahead(1, |token| self.token.span.hi() == token.span.lo())
271                    {
272                        self.bump(); // bump past the colon
273                        self.dcx().emit_err(PathSingleColon {
274                            span: self.prev_token.span,
275                            suggestion: self.prev_token.span.shrink_to_hi(),
276                        });
277                    }
278                    continue;
279                }
280
281                return Ok(());
282            }
283        }
284    }
285
286    /// Eat `::` or, potentially, `:::`.
287    #[must_use]
288    pub(super) fn eat_path_sep(&mut self) -> bool {
289        let result = self.eat(exp!(PathSep));
290        if result && self.may_recover() {
291            if self.eat_noexpect(&token::Colon) {
292                self.dcx().emit_err(PathTripleColon { span: self.prev_token.span });
293            }
294        }
295        result
296    }
297
298    pub(super) fn parse_path_segment(
299        &mut self,
300        style: PathStyle,
301        ty_generics: Option<&Generics>,
302    ) -> PResult<'a, PathSegment> {
303        let ident = self.parse_path_segment_ident()?;
304        let is_args_start = |token: &Token| {
305            matches!(token.kind, token::Lt | token::Shl | token::OpenParen | token::LArrow)
306        };
307        let check_args_start = |this: &mut Self| {
308            this.expected_token_types.insert(TokenType::Lt);
309            this.expected_token_types.insert(TokenType::OpenParen);
310            is_args_start(&this.token)
311        };
312
313        Ok(
314            if style == PathStyle::Type && check_args_start(self)
315                || style != PathStyle::Mod && self.check_path_sep_and_look_ahead(is_args_start)
316            {
317                // We use `style == PathStyle::Expr` to check if this is in a recursion or not. If
318                // it isn't, then we reset the unmatched angle bracket count as we're about to start
319                // parsing a new path.
320                if style == PathStyle::Expr {
321                    self.unmatched_angle_bracket_count = 0;
322                }
323
324                // Generic arguments are found - `<`, `(`, `::<` or `::(`.
325                // First, eat `::` if it exists.
326                let _ = self.eat_path_sep();
327
328                let lo = self.token.span;
329                let args = if self.eat_lt() {
330                    // `<'a, T, A = U>`
331                    let args = self.parse_angle_args_with_leading_angle_bracket_recovery(
332                        style,
333                        lo,
334                        ty_generics,
335                    )?;
336                    self.expect_gt().map_err(|mut err| {
337                        // Try to recover a `:` into a `::`
338                        if self.token == token::Colon
339                            && self.look_ahead(1, |token| token.is_non_reserved_ident())
340                        {
341                            err.cancel();
342                            err = self.dcx().create_err(PathSingleColon {
343                                span: self.token.span,
344                                suggestion: self.prev_token.span.shrink_to_hi(),
345                            });
346                        }
347                        // Attempt to find places where a missing `>` might belong.
348                        else if let Some(arg) = args
349                            .iter()
350                            .rev()
351                            .find(|arg| !matches!(arg, AngleBracketedArg::Constraint(_)))
352                        {
353                            err.span_suggestion_verbose(
354                                arg.span().shrink_to_hi(),
355                                "you might have meant to end the type parameters here",
356                                ">",
357                                Applicability::MaybeIncorrect,
358                            );
359                        }
360                        err
361                    })?;
362                    let span = lo.to(self.prev_token.span);
363                    AngleBracketedArgs { args, span }.into()
364                } else if self.token == token::OpenParen
365                    // FIXME(return_type_notation): Could also recover `...` here.
366                    && self.look_ahead(1, |t| *t == token::DotDot)
367                {
368                    self.bump(); // (
369                    self.bump(); // ..
370                    self.expect(exp!(CloseParen))?;
371                    let span = lo.to(self.prev_token.span);
372
373                    self.psess.gated_spans.gate(sym::return_type_notation, span);
374
375                    let prev_lo = self.prev_token.span.shrink_to_hi();
376                    if self.eat_noexpect(&token::RArrow) {
377                        let lo = self.prev_token.span;
378                        let ty = self.parse_ty()?;
379                        let span = lo.to(ty.span);
380                        let suggestion = prev_lo.to(ty.span);
381                        self.dcx()
382                            .emit_err(errors::BadReturnTypeNotationOutput { span, suggestion });
383                    }
384
385                    Box::new(ast::GenericArgs::ParenthesizedElided(span))
386                } else {
387                    // `(T, U) -> R`
388
389                    let prev_token_before_parsing = self.prev_token;
390                    let token_before_parsing = self.token;
391                    let mut snapshot = None;
392                    if self.may_recover()
393                        && prev_token_before_parsing == token::PathSep
394                        && (style == PathStyle::Expr && self.token.can_begin_expr()
395                            || style == PathStyle::Pat
396                                && self.token.can_begin_pattern(token::NtPatKind::PatParam {
397                                    inferred: false,
398                                }))
399                    {
400                        snapshot = Some(self.create_snapshot_for_diagnostic());
401                    }
402
403                    let dcx = self.dcx();
404                    let parse_params_result = self.parse_paren_comma_seq(|p| {
405                        // Inside parenthesized type arguments, we want types only, not names.
406                        let mode = FnParseMode {
407                            context: FnContext::Free,
408                            req_name: |_, _| false,
409                            req_body: false,
410                        };
411                        let param = p.parse_param_general(&mode, false, false);
412                        param.map(move |param| {
413                            if !matches!(param.pat.kind, PatKind::Missing) {
414                                dcx.emit_err(FnPathFoundNamedParams {
415                                    named_param_span: param.pat.span,
416                                });
417                            }
418                            if matches!(param.ty.kind, TyKind::CVarArgs) {
419                                dcx.emit_err(PathFoundCVariadicParams { span: param.pat.span });
420                            }
421                            if !param.attrs.is_empty() {
422                                dcx.emit_err(PathFoundAttributeInParams {
423                                    span: param.attrs[0].span,
424                                });
425                            }
426                            param.ty
427                        })
428                    });
429
430                    let (inputs, _) = match parse_params_result {
431                        Ok(output) => output,
432                        Err(mut error) if prev_token_before_parsing == token::PathSep => {
433                            error.span_label(
434                                prev_token_before_parsing.span.to(token_before_parsing.span),
435                                "while parsing this parenthesized list of type arguments starting here",
436                            );
437
438                            if let Some(mut snapshot) = snapshot {
439                                snapshot.recover_fn_call_leading_path_sep(
440                                    style,
441                                    prev_token_before_parsing,
442                                    &mut error,
443                                )
444                            }
445
446                            return Err(error);
447                        }
448                        Err(error) => return Err(error),
449                    };
450                    let inputs_span = lo.to(self.prev_token.span);
451                    let output =
452                        self.parse_ret_ty(AllowPlus::No, RecoverQPath::No, RecoverReturnSign::No)?;
453                    let span = ident.span.to(self.prev_token.span);
454                    ParenthesizedArgs { span, inputs, inputs_span, output }.into()
455                };
456
457                PathSegment { ident, args: Some(args), id: ast::DUMMY_NODE_ID }
458            } else {
459                // Generic arguments are not found.
460                PathSegment::from_ident(ident)
461            },
462        )
463    }
464
465    pub(super) fn parse_path_segment_ident(&mut self) -> PResult<'a, Ident> {
466        match self.token.ident() {
467            Some((ident, IdentIsRaw::No)) if ident.is_path_segment_keyword() => {
468                self.bump();
469                Ok(ident)
470            }
471            _ => self.parse_ident(),
472        }
473    }
474
475    /// Recover `$path::(...)` as `$path(...)`.
476    ///
477    /// ```ignore (diagnostics)
478    /// foo::(420, "bar")
479    ///    ^^ remove extra separator to make the function call
480    /// // or
481    /// match x {
482    ///    Foo::(420, "bar") => { ... },
483    ///       ^^ remove extra separator to turn this into tuple struct pattern
484    ///    _ => { ... },
485    /// }
486    /// ```
487    fn recover_fn_call_leading_path_sep(
488        &mut self,
489        style: PathStyle,
490        prev_token_before_parsing: Token,
491        error: &mut Diag<'_>,
492    ) {
493        match style {
494            PathStyle::Expr
495                if let Ok(_) = self
496                    .parse_paren_comma_seq(|p| p.parse_expr())
497                    .map_err(|error| error.cancel()) => {}
498            PathStyle::Pat
499                if let Ok(_) = self
500                    .parse_paren_comma_seq(|p| {
501                        p.parse_pat_allow_top_guard(
502                            None,
503                            RecoverComma::No,
504                            RecoverColon::No,
505                            CommaRecoveryMode::LikelyTuple,
506                        )
507                    })
508                    .map_err(|error| error.cancel()) => {}
509            _ => {
510                return;
511            }
512        }
513
514        if let token::PathSep | token::RArrow = self.token.kind {
515            return;
516        }
517
518        error.span_suggestion_verbose(
519            prev_token_before_parsing.span,
520            format!(
521                "consider removing the `::` here to {}",
522                match style {
523                    PathStyle::Expr => "call the expression",
524                    PathStyle::Pat => "turn this into a tuple struct pattern",
525                    _ => {
526                        return;
527                    }
528                }
529            ),
530            "",
531            Applicability::MaybeIncorrect,
532        );
533    }
534
535    /// Parses generic args (within a path segment) with recovery for extra leading angle brackets.
536    /// For the purposes of understanding the parsing logic of generic arguments, this function
537    /// can be thought of being the same as just calling `self.parse_angle_args()` if the source
538    /// had the correct amount of leading angle brackets.
539    ///
540    /// ```ignore (diagnostics)
541    /// bar::<<<<T as Foo>::Output>();
542    ///      ^^ help: remove extra angle brackets
543    /// ```
544    fn parse_angle_args_with_leading_angle_bracket_recovery(
545        &mut self,
546        style: PathStyle,
547        lo: Span,
548        ty_generics: Option<&Generics>,
549    ) -> PResult<'a, ThinVec<AngleBracketedArg>> {
550        // We need to detect whether there are extra leading left angle brackets and produce an
551        // appropriate error and suggestion. This cannot be implemented by looking ahead at
552        // upcoming tokens for a matching `>` character - if there are unmatched `<` tokens
553        // then there won't be matching `>` tokens to find.
554        //
555        // To explain how this detection works, consider the following example:
556        //
557        // ```ignore (diagnostics)
558        // bar::<<<<T as Foo>::Output>();
559        //      ^^ help: remove extra angle brackets
560        // ```
561        //
562        // Parsing of the left angle brackets starts in this function. We start by parsing the
563        // `<` token (incrementing the counter of unmatched angle brackets on `Parser` via
564        // `eat_lt`):
565        //
566        // *Upcoming tokens:* `<<<<T as Foo>::Output>;`
567        // *Unmatched count:* 1
568        // *`parse_path_segment` calls deep:* 0
569        //
570        // This has the effect of recursing as this function is called if a `<` character
571        // is found within the expected generic arguments:
572        //
573        // *Upcoming tokens:* `<<<T as Foo>::Output>;`
574        // *Unmatched count:* 2
575        // *`parse_path_segment` calls deep:* 1
576        //
577        // Eventually we will have recursed until having consumed all of the `<` tokens and
578        // this will be reflected in the count:
579        //
580        // *Upcoming tokens:* `T as Foo>::Output>;`
581        // *Unmatched count:* 4
582        // `parse_path_segment` calls deep:* 3
583        //
584        // The parser will continue until reaching the first `>` - this will decrement the
585        // unmatched angle bracket count and return to the parent invocation of this function
586        // having succeeded in parsing:
587        //
588        // *Upcoming tokens:* `::Output>;`
589        // *Unmatched count:* 3
590        // *`parse_path_segment` calls deep:* 2
591        //
592        // This will continue until the next `>` character which will also return successfully
593        // to the parent invocation of this function and decrement the count:
594        //
595        // *Upcoming tokens:* `;`
596        // *Unmatched count:* 2
597        // *`parse_path_segment` calls deep:* 1
598        //
599        // At this point, this function will expect to find another matching `>` character but
600        // won't be able to and will return an error. This will continue all the way up the
601        // call stack until the first invocation:
602        //
603        // *Upcoming tokens:* `;`
604        // *Unmatched count:* 2
605        // *`parse_path_segment` calls deep:* 0
606        //
607        // In doing this, we have managed to work out how many unmatched leading left angle
608        // brackets there are, but we cannot recover as the unmatched angle brackets have
609        // already been consumed. To remedy this, we keep a snapshot of the parser state
610        // before we do the above. We can then inspect whether we ended up with a parsing error
611        // and unmatched left angle brackets and if so, restore the parser state before we
612        // consumed any `<` characters to emit an error and consume the erroneous tokens to
613        // recover by attempting to parse again.
614        //
615        // In practice, the recursion of this function is indirect and there will be other
616        // locations that consume some `<` characters - as long as we update the count when
617        // this happens, it isn't an issue.
618
619        let is_first_invocation = style == PathStyle::Expr;
620        // Take a snapshot before attempting to parse - we can restore this later.
621        let snapshot = is_first_invocation.then(|| self.clone());
622
623        self.angle_bracket_nesting += 1;
624        debug!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)");
625        match self.parse_angle_args(ty_generics) {
626            Ok(args) => {
627                self.angle_bracket_nesting -= 1;
628                Ok(args)
629            }
630            Err(e) if self.angle_bracket_nesting > 10 => {
631                self.angle_bracket_nesting -= 1;
632                // When encountering severely malformed code where there are several levels of
633                // nested unclosed angle args (`f::<f::<f::<f::<...`), we avoid severe O(n^2)
634                // behavior by bailing out earlier (#117080).
635                e.emit().raise_fatal();
636            }
637            Err(e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => {
638                self.angle_bracket_nesting -= 1;
639
640                // Swap `self` with our backup of the parser state before attempting to parse
641                // generic arguments.
642                let snapshot = mem::replace(self, snapshot.unwrap());
643
644                // Eat the unmatched angle brackets.
645                let all_angle_brackets = (0..snapshot.unmatched_angle_bracket_count)
646                    .fold(true, |a, _| a && self.eat_lt());
647
648                if !all_angle_brackets {
649                    // If there are other tokens in between the extraneous `<`s, we cannot simply
650                    // suggest to remove them. This check also prevents us from accidentally ending
651                    // up in the middle of a multibyte character (issue #84104).
652                    let _ = mem::replace(self, snapshot);
653                    Err(e)
654                } else {
655                    // Cancel error from being unable to find `>`. We know the error
656                    // must have been this due to a non-zero unmatched angle bracket
657                    // count.
658                    e.cancel();
659
660                    debug!(
661                        "parse_generic_args_with_leading_angle_bracket_recovery: (snapshot failure) \
662                         snapshot.count={:?}",
663                        snapshot.unmatched_angle_bracket_count,
664                    );
665
666                    // Make a span over ${unmatched angle bracket count} characters.
667                    // This is safe because `all_angle_brackets` ensures that there are only `<`s,
668                    // i.e. no multibyte characters, in this range.
669                    let span = lo
670                        .with_hi(lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count.into()));
671                    self.dcx().emit_err(errors::UnmatchedAngle {
672                        span,
673                        plural: snapshot.unmatched_angle_bracket_count > 1,
674                    });
675
676                    // Try again without unmatched angle bracket characters.
677                    self.parse_angle_args(ty_generics)
678                }
679            }
680            Err(e) => {
681                self.angle_bracket_nesting -= 1;
682                Err(e)
683            }
684        }
685    }
686
687    /// Parses (possibly empty) list of generic arguments / associated item constraints,
688    /// possibly including trailing comma.
689    pub(super) fn parse_angle_args(
690        &mut self,
691        ty_generics: Option<&Generics>,
692    ) -> PResult<'a, ThinVec<AngleBracketedArg>> {
693        let mut args = ThinVec::new();
694        while let Some(arg) = self.parse_angle_arg(ty_generics)? {
695            args.push(arg);
696            if !self.eat(exp!(Comma)) {
697                if self.check_noexpect(&TokenKind::Semi)
698                    && self.look_ahead(1, |t| t.is_ident() || t.is_lifetime())
699                {
700                    // Add `>` to the list of expected tokens.
701                    self.check(exp!(Gt));
702                    // Handle `,` to `;` substitution
703                    let mut err = self.unexpected().unwrap_err();
704                    self.bump();
705                    err.span_suggestion_verbose(
706                        self.prev_token.span.until(self.token.span),
707                        "use a comma to separate type parameters",
708                        ", ",
709                        Applicability::MachineApplicable,
710                    );
711                    err.emit();
712                    continue;
713                }
714                if !self.token.kind.should_end_const_arg()
715                    && self.handle_ambiguous_unbraced_const_arg(&mut args)?
716                {
717                    // We've managed to (partially) recover, so continue trying to parse
718                    // arguments.
719                    continue;
720                }
721                break;
722            }
723        }
724        Ok(args)
725    }
726
727    /// Parses a single argument in the angle arguments `<...>` of a path segment.
728    fn parse_angle_arg(
729        &mut self,
730        ty_generics: Option<&Generics>,
731    ) -> PResult<'a, Option<AngleBracketedArg>> {
732        let lo = self.token.span;
733        let arg = self.parse_generic_arg(ty_generics)?;
734        match arg {
735            Some(arg) => {
736                // we are using noexpect here because we first want to find out if either `=` or `:`
737                // is present and then use that info to push the other token onto the tokens list
738                let separated =
739                    self.check_noexpect(&token::Colon) || self.check_noexpect(&token::Eq);
740                if separated && (self.check(exp!(Colon)) | self.check(exp!(Eq))) {
741                    let arg_span = arg.span();
742                    let (binder, ident, gen_args) = match self.get_ident_from_generic_arg(&arg) {
743                        Ok(ident_gen_args) => ident_gen_args,
744                        Err(()) => return Ok(Some(AngleBracketedArg::Arg(arg))),
745                    };
746                    if binder {
747                        // FIXME(compiler-errors): this could be improved by suggesting lifting
748                        // this up to the trait, at least before this becomes real syntax.
749                        // e.g. `Trait<for<'a> Assoc = Ty>` -> `for<'a> Trait<Assoc = Ty>`
750                        return Err(self.dcx().struct_span_err(
751                            arg_span,
752                            "`for<...>` is not allowed on associated type bounds",
753                        ));
754                    }
755                    let kind = if self.eat(exp!(Colon)) {
756                        AssocItemConstraintKind::Bound { bounds: self.parse_generic_bounds()? }
757                    } else if self.check(exp!(Eq)) {
758                        self.parse_assoc_equality_term(ident, gen_args.as_ref())?
759                    } else {
760                        unreachable!();
761                    };
762
763                    let span = lo.to(self.prev_token.span);
764                    let constraint =
765                        AssocItemConstraint { id: ast::DUMMY_NODE_ID, ident, gen_args, kind, span };
766                    Ok(Some(AngleBracketedArg::Constraint(constraint)))
767                } else {
768                    // we only want to suggest `:` and `=` in contexts where the previous token
769                    // is an ident and the current token or the next token is an ident
770                    if self.prev_token.is_ident()
771                        && (self.token.is_ident() || self.look_ahead(1, |token| token.is_ident()))
772                    {
773                        self.check(exp!(Colon));
774                        self.check(exp!(Eq));
775                    }
776                    Ok(Some(AngleBracketedArg::Arg(arg)))
777                }
778            }
779            _ => Ok(None),
780        }
781    }
782
783    /// Parse the term to the right of an associated item equality constraint.
784    ///
785    /// That is, parse `$term` in `Item = $term` where `$term` is a type or
786    /// a const expression (wrapped in curly braces if complex).
787    fn parse_assoc_equality_term(
788        &mut self,
789        ident: Ident,
790        gen_args: Option<&GenericArgs>,
791    ) -> PResult<'a, AssocItemConstraintKind> {
792        let prev_token_span = self.prev_token.span;
793        let eq_span = self.token.span;
794        self.expect(exp!(Eq))?;
795        let arg = self.parse_generic_arg(None)?;
796        let span = ident.span.to(self.prev_token.span);
797        let term = match arg {
798            Some(GenericArg::Type(ty)) => ty.into(),
799            Some(GenericArg::Const(c)) => {
800                self.psess.gated_spans.gate(sym::associated_const_equality, span);
801                c.into()
802            }
803            Some(GenericArg::Lifetime(lt)) => {
804                let guar = self.dcx().emit_err(errors::LifetimeInEqConstraint {
805                    span: lt.ident.span,
806                    lifetime: lt.ident,
807                    binding_label: span,
808                    colon_sugg: gen_args
809                        .map_or(ident.span, |args| args.span())
810                        .between(lt.ident.span),
811                });
812                self.mk_ty(lt.ident.span, ast::TyKind::Err(guar)).into()
813            }
814            None => {
815                let after_eq = eq_span.shrink_to_hi();
816                let before_next = self.token.span.shrink_to_lo();
817                let mut err = self
818                    .dcx()
819                    .struct_span_err(after_eq.to(before_next), "missing type to the right of `=`");
820                if matches!(self.token.kind, token::Comma | token::Gt) {
821                    err.span_suggestion(
822                        self.psess.source_map().next_point(eq_span).to(before_next),
823                        "to constrain the associated type, add a type after `=`",
824                        " TheType",
825                        Applicability::HasPlaceholders,
826                    );
827                    err.span_suggestion(
828                        prev_token_span.shrink_to_hi().to(before_next),
829                        format!("remove the `=` if `{ident}` is a type"),
830                        "",
831                        Applicability::MaybeIncorrect,
832                    )
833                } else {
834                    err.span_label(
835                        self.token.span,
836                        format!("expected type, found {}", super::token_descr(&self.token)),
837                    )
838                };
839                return Err(err);
840            }
841        };
842        Ok(AssocItemConstraintKind::Equality { term })
843    }
844
845    /// We do not permit arbitrary expressions as const arguments. They must be one of:
846    /// - An expression surrounded in `{}`.
847    /// - A literal.
848    /// - A numeric literal prefixed by `-`.
849    /// - A single-segment path.
850    pub(super) fn expr_is_valid_const_arg(&self, expr: &Box<rustc_ast::Expr>) -> bool {
851        match &expr.kind {
852            ast::ExprKind::Block(_, _)
853            | ast::ExprKind::Lit(_)
854            | ast::ExprKind::IncludedBytes(..) => true,
855            ast::ExprKind::Unary(ast::UnOp::Neg, expr) => {
856                matches!(expr.kind, ast::ExprKind::Lit(_))
857            }
858            // We can only resolve single-segment paths at the moment, because multi-segment paths
859            // require type-checking: see `visit_generic_arg` in `src/librustc_resolve/late.rs`.
860            ast::ExprKind::Path(None, path)
861                if let [segment] = path.segments.as_slice()
862                    && segment.args.is_none() =>
863            {
864                true
865            }
866            _ => false,
867        }
868    }
869
870    /// Parse a const argument, e.g. `<3>`. It is assumed the angle brackets will be parsed by
871    /// the caller.
872    pub(super) fn parse_const_arg(&mut self) -> PResult<'a, AnonConst> {
873        // Parse const argument.
874        let (value, mgca_disambiguation) = if self.token.kind == token::OpenBrace {
875            let value = self.parse_expr_block(None, self.token.span, BlockCheckMode::Default)?;
876            (value, MgcaDisambiguation::Direct)
877        } else if self.token.is_keyword(kw::Const) {
878            // While we could just disambiguate `Direct` from `AnonConst` by
879            // treating all const block exprs as `AnonConst`, that would
880            // complicate the DefCollector and likely all other visitors.
881            // So we strip the const blockiness and just store it as a block
882            // in the AST with the extra disambiguator on the AnonConst
883            let value = self.parse_mgca_const_block(true)?;
884            (value.value, MgcaDisambiguation::AnonConst)
885        } else {
886            self.parse_unambiguous_unbraced_const_arg()?
887        };
888        Ok(AnonConst { id: ast::DUMMY_NODE_ID, value, mgca_disambiguation })
889    }
890
891    /// Attempt to parse a const argument that has not been enclosed in braces.
892    /// There are a limited number of expressions that are permitted without being
893    /// enclosed in braces:
894    /// - Literals.
895    /// - Single-segment paths (i.e. standalone generic const parameters).
896    /// All other expressions that can be parsed will emit an error suggesting the expression be
897    /// wrapped in braces.
898    pub(super) fn parse_unambiguous_unbraced_const_arg(
899        &mut self,
900    ) -> PResult<'a, (Box<Expr>, MgcaDisambiguation)> {
901        let start = self.token.span;
902        let attrs = self.parse_outer_attributes()?;
903        let (expr, _) =
904            self.parse_expr_res(Restrictions::CONST_EXPR, attrs).map_err(|mut err| {
905                err.span_label(
906                    start.shrink_to_lo(),
907                    "while parsing a const generic argument starting here",
908                );
909                err
910            })?;
911        if !self.expr_is_valid_const_arg(&expr) {
912            self.dcx().emit_err(ConstGenericWithoutBraces {
913                span: expr.span,
914                sugg: ConstGenericWithoutBracesSugg {
915                    left: expr.span.shrink_to_lo(),
916                    right: expr.span.shrink_to_hi(),
917                },
918            });
919        }
920
921        let mgca_disambiguation = self.mgca_direct_lit_hack(&expr);
922        Ok((expr, mgca_disambiguation))
923    }
924
925    /// Under `min_generic_const_args` we still allow *some* anon consts to be written without
926    /// a `const` block as it makes things quite a lot nicer. This function is useful for contexts
927    /// where we would like to use `MgcaDisambiguation::Direct` but need to fudge it to be `AnonConst`
928    /// in the presence of literals.
929    //
930    /// FIXME(min_generic_const_args): In the long term it would be nice to have a way to directly
931    /// represent literals in `hir::ConstArgKind` so that we can remove this special case by not
932    /// needing an anon const.
933    pub fn mgca_direct_lit_hack(&self, expr: &Expr) -> MgcaDisambiguation {
934        match &expr.kind {
935            ast::ExprKind::Lit(_) => MgcaDisambiguation::AnonConst,
936            ast::ExprKind::Unary(ast::UnOp::Neg, expr)
937                if matches!(expr.kind, ast::ExprKind::Lit(_)) =>
938            {
939                MgcaDisambiguation::AnonConst
940            }
941            _ => MgcaDisambiguation::Direct,
942        }
943    }
944
945    /// Parse a generic argument in a path segment.
946    /// This does not include constraints, e.g., `Item = u8`, which is handled in `parse_angle_arg`.
947    pub(super) fn parse_generic_arg(
948        &mut self,
949        ty_generics: Option<&Generics>,
950    ) -> PResult<'a, Option<GenericArg>> {
951        let mut attr_span: Option<Span> = None;
952        if self.token == token::Pound && self.look_ahead(1, |t| *t == token::OpenBracket) {
953            let attrs_wrapper = self.parse_outer_attributes()?;
954            let raw_attrs = attrs_wrapper.take_for_recovery(self.psess);
955            attr_span = Some(raw_attrs[0].span.to(raw_attrs.last().unwrap().span));
956        }
957        let start = self.token.span;
958        let arg = if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) {
959            // Parse lifetime argument.
960            GenericArg::Lifetime(self.expect_lifetime())
961        } else if self.check_const_arg() {
962            // Parse const argument.
963            GenericArg::Const(self.parse_const_arg()?)
964        } else if self.check_type() {
965            // Parse type argument.
966
967            // Proactively create a parser snapshot enabling us to rewind and try to reparse the
968            // input as a const expression in case we fail to parse a type. If we successfully
969            // do so, we will report an error that it needs to be wrapped in braces.
970            let mut snapshot = None;
971            if self.may_recover() && self.token.can_begin_expr() {
972                snapshot = Some(self.create_snapshot_for_diagnostic());
973            }
974
975            match self.parse_ty() {
976                Ok(ty) => {
977                    // Since the type parser recovers from some malformed slice and array types and
978                    // successfully returns a type, we need to look for `TyKind::Err`s in the
979                    // type to determine if error recovery has occurred and if the input is not a
980                    // syntactically valid type after all.
981                    if let ast::TyKind::Slice(inner_ty) | ast::TyKind::Array(inner_ty, _) = &ty.kind
982                        && let ast::TyKind::Err(_) = inner_ty.kind
983                        && let Some(snapshot) = snapshot
984                        && let Some(expr) =
985                            self.recover_unbraced_const_arg_that_can_begin_ty(snapshot)
986                    {
987                        return Ok(Some(
988                            self.dummy_const_arg_needs_braces(
989                                self.dcx()
990                                    .struct_span_err(expr.span, "invalid const generic expression"),
991                                expr.span,
992                            ),
993                        ));
994                    }
995
996                    GenericArg::Type(ty)
997                }
998                Err(err) => {
999                    if let Some(snapshot) = snapshot
1000                        && let Some(expr) =
1001                            self.recover_unbraced_const_arg_that_can_begin_ty(snapshot)
1002                    {
1003                        return Ok(Some(self.dummy_const_arg_needs_braces(err, expr.span)));
1004                    }
1005                    // Try to recover from possible `const` arg without braces.
1006                    return self.recover_const_arg(start, err).map(Some);
1007                }
1008            }
1009        } else if self.token.is_keyword(kw::Const) {
1010            return self.recover_const_param_declaration(ty_generics);
1011        } else if let Some(attr_span) = attr_span {
1012            let diag = self.dcx().create_err(AttributeOnEmptyType { span: attr_span });
1013            return Err(diag);
1014        } else {
1015            // Fall back by trying to parse a const-expr expression. If we successfully do so,
1016            // then we should report an error that it needs to be wrapped in braces.
1017            let snapshot = self.create_snapshot_for_diagnostic();
1018            let attrs = self.parse_outer_attributes()?;
1019            match self.parse_expr_res(Restrictions::CONST_EXPR, attrs) {
1020                Ok((expr, _)) => {
1021                    return Ok(Some(self.dummy_const_arg_needs_braces(
1022                        self.dcx().struct_span_err(expr.span, "invalid const generic expression"),
1023                        expr.span,
1024                    )));
1025                }
1026                Err(err) => {
1027                    self.restore_snapshot(snapshot);
1028                    err.cancel();
1029                    return Ok(None);
1030                }
1031            }
1032        };
1033
1034        if let Some(attr_span) = attr_span {
1035            let guar = self.dcx().emit_err(AttributeOnGenericArg {
1036                span: attr_span,
1037                fix_span: attr_span.until(arg.span()),
1038            });
1039            return Ok(Some(match arg {
1040                GenericArg::Type(_) => GenericArg::Type(self.mk_ty(attr_span, TyKind::Err(guar))),
1041                GenericArg::Const(_) => {
1042                    let error_expr = self.mk_expr(attr_span, ExprKind::Err(guar));
1043                    GenericArg::Const(AnonConst {
1044                        id: ast::DUMMY_NODE_ID,
1045                        value: error_expr,
1046                        mgca_disambiguation: MgcaDisambiguation::Direct,
1047                    })
1048                }
1049                GenericArg::Lifetime(lt) => GenericArg::Lifetime(lt),
1050            }));
1051        }
1052
1053        Ok(Some(arg))
1054    }
1055
1056    /// Given a arg inside of generics, we try to destructure it as if it were the LHS in
1057    /// `LHS = ...`, i.e. an associated item binding.
1058    /// This returns a bool indicating if there are any `for<'a, 'b>` binder args, the
1059    /// identifier, and any GAT arguments.
1060    fn get_ident_from_generic_arg(
1061        &self,
1062        gen_arg: &GenericArg,
1063    ) -> Result<(bool, Ident, Option<GenericArgs>), ()> {
1064        if let GenericArg::Type(ty) = gen_arg {
1065            if let ast::TyKind::Path(qself, path) = &ty.kind
1066                && qself.is_none()
1067                && let [seg] = path.segments.as_slice()
1068            {
1069                return Ok((false, seg.ident, seg.args.as_deref().cloned()));
1070            } else if let ast::TyKind::TraitObject(bounds, ast::TraitObjectSyntax::None) = &ty.kind
1071                && let [ast::GenericBound::Trait(trait_ref)] = bounds.as_slice()
1072                && trait_ref.modifiers == ast::TraitBoundModifiers::NONE
1073                && let [seg] = trait_ref.trait_ref.path.segments.as_slice()
1074            {
1075                return Ok((true, seg.ident, seg.args.as_deref().cloned()));
1076            }
1077        }
1078        Err(())
1079    }
1080}