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