1use std::mem;
23use ast::token::IdentIsRaw;
4use rustc_ast::token::{self, MetaVarKind, Token, TokenKind};
5use rustc_ast::{
6selfas ast, AngleBracketedArg, AngleBracketedArgs, AnonConst, AssocItemConstraint,
7AssocItemConstraintKind, BlockCheckMode, GenericArg, GenericArgs, Generics, MgcaDisambiguation,
8ParenthesizedArgs, 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;
1415use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
16use super::{Parser, Restrictions, TokenType};
17use crate::ast::{PatKind, TyKind};
18use crate::errors::{
19self, AttributeOnEmptyType, AttributeOnGenericArg, ConstGenericWithoutBraces,
20ConstGenericWithoutBracesSugg, FnPathFoundNamedParams, PathFoundAttributeInParams,
21PathFoundCVariadicParams, PathSingleColon, PathTripleColon,
22};
23use crate::exp;
24use crate::parser::{
25CommaRecoveryMode, Expr, ExprKind, FnContext, FnParseMode, RecoverColon, RecoverComma,
26};
2728/// Specifies how to parse a path.
29#[derive(#[automatically_derived]
impl ::core::marker::Copy for PathStyle { }Copy, #[automatically_derived]
impl ::core::clone::Clone for PathStyle {
#[inline]
fn clone(&self) -> PathStyle { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for PathStyle {
#[inline]
fn eq(&self, other: &PathStyle) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}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.
40Expr,
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 /// ```
49Pat,
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.
53Type,
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.
60Mod,
61}
6263impl PathStyle {
64fn has_generic_ambiguity(&self) -> bool {
65#[allow(non_exhaustive_omitted_patterns)] match self {
Self::Expr | Self::Pat => true,
_ => false,
}matches!(self, Self::Expr | Self::Pat)66 }
67}
6869impl<'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)
80pub(super) fn parse_qpath(&mut self, style: PathStyle) -> PResult<'a, (Box<QSelf>, Path)> {
81let lo = self.prev_token.span;
82let ty = self.parse_ty()?;
8384// `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`.
88let (mut path, path_span);
89if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::As,
token_type: crate::parser::token_type::TokenType::KwAs,
}exp!(As)) {
90let path_lo = self.token.span;
91path = self.parse_path(PathStyle::Type)?;
92path_span = path_lo.to(self.prev_token.span);
93 } else {
94path_span = self.token.span.to(self.token.span);
95path = ast::Path { segments: ThinVec::new(), span: path_span, tokens: None };
96 }
9798// See doc comment for `unmatched_angle_bracket_count`.
99self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Gt,
token_type: crate::parser::token_type::TokenType::Gt,
}exp!(Gt))?;
100if self.unmatched_angle_bracket_count > 0 {
101self.unmatched_angle_bracket_count -= 1;
102{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/path.rs:102",
"rustc_parse::parser::path", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/path.rs"),
::tracing_core::__macro_support::Option::Some(102u32),
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("parse_qpath: (decrement) count={0:?}",
self.unmatched_angle_bracket_count) as &dyn Value))])
});
} else { ; }
};debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count);
103 }
104105let is_import_coupler = self.is_import_coupler();
106if !is_import_coupler && !self.recover_colon_before_qpath_proj() {
107self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::PathSep,
token_type: crate::parser::token_type::TokenType::PathSep,
}exp!(PathSep))?;
108 }
109110let qself = Box::new(QSelf { ty, path_span, position: path.segments.len() });
111if !is_import_coupler {
112self.parse_path_segments(&mut path.segments, style, None)?;
113 }
114115Ok((
116qself,
117Path { segments: path.segments, span: lo.to(self.prev_token.span), tokens: None },
118 ))
119 }
120121/// 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 /// ```
129fn recover_colon_before_qpath_proj(&mut self) -> bool {
130if !self.check_noexpect(&TokenKind::Colon)
131 || self.look_ahead(1, |t| !t.is_non_reserved_ident())
132 {
133return false;
134 }
135136self.bump(); // colon
137138self.dcx()
139 .struct_span_err(
140self.prev_token.span,
141"found single colon before projection in qualified path",
142 )
143 .with_span_suggestion(
144self.prev_token.span,
145"use double colon",
146"::",
147 Applicability::MachineApplicable,
148 )
149 .emit();
150151true
152}
153154pub fn parse_path(&mut self, style: PathStyle) -> PResult<'a, Path> {
155self.parse_path_inner(style, None)
156 }
157158/// 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)
168pub(super) fn parse_path_inner(
169&mut self,
170 style: PathStyle,
171 ty_generics: Option<&Generics>,
172 ) -> PResult<'a, Path> {
173let 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 //
182if style == PathStyle::Mod && path.segments.iter().any(|segment| segment.args.is_some())
183 {
184let span = path185 .segments
186 .iter()
187 .filter_map(|segment| segment.args.as_ref())
188 .map(|arg| arg.span())
189 .collect::<Vec<_>>();
190parser.dcx().emit_err(errors::GenericsInPath { span });
191// Ignore these arguments to prevent unexpected behaviors.
192let segments = path193 .segments
194 .iter()
195 .map(|segment| PathSegment { ident: segment.ident, id: segment.id, args: None })
196 .collect();
197Path { segments, ..path }
198 } else {
199path200 }
201 };
202203if let Some(path) =
204self.eat_metavar_seq(MetaVarKind::Path, |this| this.parse_path(PathStyle::Type))
205 {
206return Ok(reject_generics_if_mod_style(self, path));
207 }
208209// 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.
211if let Some(path) = self.eat_metavar_seq(MetaVarKind::Ty { is_path: true }, |this| {
212this.parse_path(PathStyle::Type)
213 }) {
214return Ok(reject_generics_if_mod_style(self, path));
215 }
216217let lo = self.token.span;
218let mut segments = ThinVec::new();
219let mod_sep_ctxt = self.token.span.ctxt();
220if self.eat_path_sep() {
221segments.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
222 }
223self.parse_path_segments(&mut segments, style, ty_generics)?;
224Ok(Path { segments, span: lo.to(self.prev_token.span), tokens: None })
225 }
226227pub(super) fn parse_path_segments(
228&mut self,
229 segments: &mut ThinVec<PathSegment>,
230 style: PathStyle,
231 ty_generics: Option<&Generics>,
232 ) -> PResult<'a, ()> {
233loop {
234let segment = self.parse_path_segment(style, ty_generics)?;
235if 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.
252self.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)]);
253 }
254segments.push(segment);
255256if 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`.
262if self.may_recover()
263 && style == PathStyle::Expr// (!)
264&& self.token == token::Colon265 && 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
269if self.token.span.lo() == self.prev_token.span.hi()
270 && self.look_ahead(1, |token| self.token.span.hi() == token.span.lo())
271 {
272self.bump(); // bump past the colon
273self.dcx().emit_err(PathSingleColon {
274 span: self.prev_token.span,
275 suggestion: self.prev_token.span.shrink_to_hi(),
276 });
277 }
278continue;
279 }
280281return Ok(());
282 }
283 }
284 }
285286/// Eat `::` or, potentially, `:::`.
287#[must_use]
288pub(super) fn eat_path_sep(&mut self) -> bool {
289let result = self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::PathSep,
token_type: crate::parser::token_type::TokenType::PathSep,
}exp!(PathSep));
290if result && self.may_recover() {
291if self.eat_noexpect(&token::Colon) {
292self.dcx().emit_err(PathTripleColon { span: self.prev_token.span });
293 }
294 }
295result296 }
297298pub(super) fn parse_path_segment(
299&mut self,
300 style: PathStyle,
301 ty_generics: Option<&Generics>,
302 ) -> PResult<'a, PathSegment> {
303let ident = self.parse_path_segment_ident()?;
304let is_args_start = |token: &Token| {
305#[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)306 };
307let check_args_start = |this: &mut Self| {
308this.expected_token_types.insert(TokenType::Lt);
309this.expected_token_types.insert(TokenType::OpenParen);
310is_args_start(&this.token)
311 };
312313Ok(
314if 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.
320if style == PathStyle::Expr {
321self.unmatched_angle_bracket_count = 0;
322 }
323324// Generic arguments are found - `<`, `(`, `::<` or `::(`.
325 // First, eat `::` if it exists.
326let _ = self.eat_path_sep();
327328let lo = self.token.span;
329let args = if self.eat_lt() {
330// `<'a, T, A = U>`
331let args = self.parse_angle_args_with_leading_angle_bracket_recovery(
332style,
333lo,
334ty_generics,
335 )?;
336self.expect_gt().map_err(|mut err| {
337// Try to recover a `:` into a `::`
338if self.token == token::Colon339 && self.look_ahead(1, |token| token.is_non_reserved_ident())
340 {
341err.cancel();
342err = 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.
348else if let Some(arg) = args349 .iter()
350 .rev()
351 .find(|arg| !#[allow(non_exhaustive_omitted_patterns)] match arg {
AngleBracketedArg::Constraint(_) => true,
_ => false,
}matches!(arg, AngleBracketedArg::Constraint(_)))
352 {
353err.span_suggestion_verbose(
354arg.span().shrink_to_hi(),
355"you might have meant to end the type parameters here",
356">",
357 Applicability::MaybeIncorrect,
358 );
359 }
360err361 })?;
362let span = lo.to(self.prev_token.span);
363AngleBracketedArgs { args, span }.into()
364 } else if self.token == token::OpenParen365// FIXME(return_type_notation): Could also recover `...` here.
366&& self.look_ahead(1, |t| *t == token::DotDot)
367 {
368self.bump(); // (
369self.bump(); // ..
370self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?;
371let span = lo.to(self.prev_token.span);
372373self.psess.gated_spans.gate(sym::return_type_notation, span);
374375let prev_lo = self.prev_token.span.shrink_to_hi();
376if self.eat_noexpect(&token::RArrow) {
377let lo = self.prev_token.span;
378let ty = self.parse_ty()?;
379let span = lo.to(ty.span);
380let suggestion = prev_lo.to(ty.span);
381self.dcx()
382 .emit_err(errors::BadReturnTypeNotationOutput { span, suggestion });
383 }
384385Box::new(ast::GenericArgs::ParenthesizedElided(span))
386 } else {
387// `(T, U) -> R`
388389let prev_token_before_parsing = self.prev_token;
390let token_before_parsing = self.token;
391let mut snapshot = None;
392if self.may_recover()
393 && prev_token_before_parsing == token::PathSep394 && (style == PathStyle::Expr && self.token.can_begin_expr()
395 || style == PathStyle::Pat396 && self.token.can_begin_pattern(token::NtPatKind::PatParam {
397 inferred: false,
398 }))
399 {
400snapshot = Some(self.create_snapshot_for_diagnostic());
401 }
402403let dcx = self.dcx();
404let parse_params_result = self.parse_paren_comma_seq(|p| {
405// Inside parenthesized type arguments, we want types only, not names.
406let mode = FnParseMode {
407 context: FnContext::Free,
408 req_name: |_, _| false,
409 req_body: false,
410 };
411let param = p.parse_param_general(&mode, false, false);
412param.map(move |param| {
413if !#[allow(non_exhaustive_omitted_patterns)] match param.pat.kind {
PatKind::Missing => true,
_ => false,
}matches!(param.pat.kind, PatKind::Missing) {
414dcx.emit_err(FnPathFoundNamedParams {
415 named_param_span: param.pat.span,
416 });
417 }
418if #[allow(non_exhaustive_omitted_patterns)] match param.ty.kind {
TyKind::CVarArgs => true,
_ => false,
}matches!(param.ty.kind, TyKind::CVarArgs) {
419dcx.emit_err(PathFoundCVariadicParams { span: param.pat.span });
420 }
421if !param.attrs.is_empty() {
422dcx.emit_err(PathFoundAttributeInParams {
423 span: param.attrs[0].span,
424 });
425 }
426param.ty
427 })
428 });
429430let (inputs, _) = match parse_params_result {
431Ok(output) => output,
432Err(mut error) if prev_token_before_parsing == token::PathSep => {
433error.span_label(
434prev_token_before_parsing.span.to(token_before_parsing.span),
435"while parsing this parenthesized list of type arguments starting here",
436 );
437438if let Some(mut snapshot) = snapshot {
439snapshot.recover_fn_call_leading_path_sep(
440style,
441prev_token_before_parsing,
442&mut error,
443 )
444 }
445446return Err(error);
447 }
448Err(error) => return Err(error),
449 };
450let inputs_span = lo.to(self.prev_token.span);
451let output =
452self.parse_ret_ty(AllowPlus::No, RecoverQPath::No, RecoverReturnSign::No)?;
453let span = ident.span.to(self.prev_token.span);
454ParenthesizedArgs { span, inputs, inputs_span, output }.into()
455 };
456457PathSegment { ident, args: Some(args), id: ast::DUMMY_NODE_ID }
458 } else {
459// Generic arguments are not found.
460PathSegment::from_ident(ident)
461 },
462 )
463 }
464465pub(super) fn parse_path_segment_ident(&mut self) -> PResult<'a, Ident> {
466match self.token.ident() {
467Some((ident, IdentIsRaw::No)) if ident.is_path_segment_keyword() => {
468self.bump();
469Ok(ident)
470 }
471_ => self.parse_ident(),
472 }
473 }
474475/// 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 /// ```
487fn recover_fn_call_leading_path_sep(
488&mut self,
489 style: PathStyle,
490 prev_token_before_parsing: Token,
491 error: &mut Diag<'_>,
492 ) {
493match style {
494 PathStyle::Expr495if let Ok(_) = self496 .parse_paren_comma_seq(|p| p.parse_expr())
497 .map_err(|error| error.cancel()) => {}
498 PathStyle::Pat499if let Ok(_) = self500 .parse_paren_comma_seq(|p| {
501p.parse_pat_allow_top_guard(
502None,
503 RecoverComma::No,
504 RecoverColon::No,
505 CommaRecoveryMode::LikelyTuple,
506 )
507 })
508 .map_err(|error| error.cancel()) => {}
509_ => {
510return;
511 }
512 }
513514if let token::PathSep | token::RArrow = self.token.kind {
515return;
516 }
517518error.span_suggestion_verbose(
519prev_token_before_parsing.span,
520::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!(
521"consider removing the `::` here to {}",
522match style {
523 PathStyle::Expr => "call the expression",
524 PathStyle::Pat => "turn this into a tuple struct pattern",
525_ => {
526return;
527 }
528 }
529 ),
530"",
531 Applicability::MaybeIncorrect,
532 );
533 }
534535/// 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 /// ```
544fn 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.
618619let is_first_invocation = style == PathStyle::Expr;
620// Take a snapshot before attempting to parse - we can restore this later.
621let snapshot = is_first_invocation.then(|| self.clone());
622623self.angle_bracket_nesting += 1;
624{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/path.rs:624",
"rustc_parse::parser::path", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/path.rs"),
::tracing_core::__macro_support::Option::Some(624u32),
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)")
as &dyn Value))])
});
} else { ; }
};debug!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)");
625match self.parse_angle_args(ty_generics) {
626Ok(args) => {
627self.angle_bracket_nesting -= 1;
628Ok(args)
629 }
630Err(e) if self.angle_bracket_nesting > 10 => {
631self.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).
635e.emit().raise_fatal();
636 }
637Err(e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => {
638self.angle_bracket_nesting -= 1;
639640// Swap `self` with our backup of the parser state before attempting to parse
641 // generic arguments.
642let snapshot = mem::replace(self, snapshot.unwrap());
643644// Eat the unmatched angle brackets.
645let all_angle_brackets = (0..snapshot.unmatched_angle_bracket_count)
646 .fold(true, |a, _| a && self.eat_lt());
647648if !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).
652let _ = mem::replace(self, snapshot);
653Err(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.
658e.cancel();
659660{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/path.rs:660",
"rustc_parse::parser::path", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/path.rs"),
::tracing_core::__macro_support::Option::Some(660u32),
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::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 Value))])
});
} else { ; }
};debug!(
661"parse_generic_args_with_leading_angle_bracket_recovery: (snapshot failure) \
662 snapshot.count={:?}",
663 snapshot.unmatched_angle_bracket_count,
664 );
665666// 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.
669let span = lo670 .with_hi(lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count.into()));
671self.dcx().emit_err(errors::UnmatchedAngle {
672span,
673 plural: snapshot.unmatched_angle_bracket_count > 1,
674 });
675676// Try again without unmatched angle bracket characters.
677self.parse_angle_args(ty_generics)
678 }
679 }
680Err(e) => {
681self.angle_bracket_nesting -= 1;
682Err(e)
683 }
684 }
685 }
686687/// Parses (possibly empty) list of generic arguments / associated item constraints,
688 /// possibly including trailing comma.
689pub(super) fn parse_angle_args(
690&mut self,
691 ty_generics: Option<&Generics>,
692 ) -> PResult<'a, ThinVec<AngleBracketedArg>> {
693let mut args = ThinVec::new();
694while let Some(arg) = self.parse_angle_arg(ty_generics)? {
695 args.push(arg);
696if !self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma)) {
697if 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.
701self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Gt,
token_type: crate::parser::token_type::TokenType::Gt,
}exp!(Gt));
702// Handle `,` to `;` substitution
703let mut err = self.unexpected().unwrap_err();
704self.bump();
705 err.span_suggestion_verbose(
706self.prev_token.span.until(self.token.span),
707"use a comma to separate type parameters",
708", ",
709 Applicability::MachineApplicable,
710 );
711 err.emit();
712continue;
713 }
714if !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.
719continue;
720 }
721break;
722 }
723 }
724Ok(args)
725 }
726727/// Parses a single argument in the angle arguments `<...>` of a path segment.
728fn parse_angle_arg(
729&mut self,
730 ty_generics: Option<&Generics>,
731 ) -> PResult<'a, Option<AngleBracketedArg>> {
732let lo = self.token.span;
733let arg = self.parse_generic_arg(ty_generics)?;
734match arg {
735Some(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
738let separated =
739self.check_noexpect(&token::Colon) || self.check_noexpect(&token::Eq);
740if 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))) {
741let arg_span = arg.span();
742let (binder, ident, gen_args) = match self.get_ident_from_generic_arg(&arg) {
743Ok(ident_gen_args) => ident_gen_args,
744Err(()) => return Ok(Some(AngleBracketedArg::Arg(arg))),
745 };
746if 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>`
750return Err(self.dcx().struct_span_err(
751arg_span,
752"`for<...>` is not allowed on associated type bounds",
753 ));
754 }
755let kind = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) {
756 AssocItemConstraintKind::Bound { bounds: self.parse_generic_bounds()? }
757 } else if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) {
758self.parse_assoc_equality_term(ident, gen_args.as_ref())?
759} else {
760::core::panicking::panic("internal error: entered unreachable code");unreachable!();
761 };
762763let span = lo.to(self.prev_token.span);
764let constraint =
765AssocItemConstraint { id: ast::DUMMY_NODE_ID, ident, gen_args, kind, span };
766Ok(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
770if self.prev_token.is_ident()
771 && (self.token.is_ident() || self.look_ahead(1, |token| token.is_ident()))
772 {
773self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon));
774self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq));
775 }
776Ok(Some(AngleBracketedArg::Arg(arg)))
777 }
778 }
779_ => Ok(None),
780 }
781 }
782783/// 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).
787fn parse_assoc_equality_term(
788&mut self,
789 ident: Ident,
790 gen_args: Option<&GenericArgs>,
791 ) -> PResult<'a, AssocItemConstraintKind> {
792let prev_token_span = self.prev_token.span;
793let eq_span = self.token.span;
794self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq))?;
795let arg = self.parse_generic_arg(None)?;
796let span = ident.span.to(self.prev_token.span);
797let term = match arg {
798Some(GenericArg::Type(ty)) => ty.into(),
799Some(GenericArg::Const(c)) => {
800self.psess.gated_spans.gate(sym::associated_const_equality, span);
801c.into()
802 }
803Some(GenericArg::Lifetime(lt)) => {
804let guar = self.dcx().emit_err(errors::LifetimeInEqConstraint {
805 span: lt.ident.span,
806 lifetime: lt.ident,
807 binding_label: span,
808 colon_sugg: gen_args809 .map_or(ident.span, |args| args.span())
810 .between(lt.ident.span),
811 });
812self.mk_ty(lt.ident.span, ast::TyKind::Err(guar)).into()
813 }
814None => {
815let after_eq = eq_span.shrink_to_hi();
816let before_next = self.token.span.shrink_to_lo();
817let mut err = self818 .dcx()
819 .struct_span_err(after_eq.to(before_next), "missing type to the right of `=`");
820if #[allow(non_exhaustive_omitted_patterns)] match self.token.kind {
token::Comma | token::Gt => true,
_ => false,
}matches!(self.token.kind, token::Comma | token::Gt) {
821err.span_suggestion(
822self.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 );
827err.span_suggestion(
828prev_token_span.shrink_to_hi().to(before_next),
829::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"),
830"",
831 Applicability::MaybeIncorrect,
832 )
833 } else {
834err.span_label(
835self.token.span,
836::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)),
837 )
838 };
839return Err(err);
840 }
841 };
842Ok(AssocItemConstraintKind::Equality { term })
843 }
844845/// 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.
850pub(super) fn expr_is_valid_const_arg(&self, expr: &Box<rustc_ast::Expr>) -> bool {
851match &expr.kind {
852 ast::ExprKind::Block(_, _)
853 | ast::ExprKind::Lit(_)
854 | ast::ExprKind::IncludedBytes(..) => true,
855 ast::ExprKind::Unary(ast::UnOp::Neg, expr) => {
856#[allow(non_exhaustive_omitted_patterns)] match expr.kind {
ast::ExprKind::Lit(_) => true,
_ => false,
}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`.
860ast::ExprKind::Path(None, path)
861if let [segment] = path.segments.as_slice()
862 && segment.args.is_none() =>
863 {
864true
865}
866_ => false,
867 }
868 }
869870/// Parse a const argument, e.g. `<3>`. It is assumed the angle brackets will be parsed by
871 /// the caller.
872pub(super) fn parse_const_arg(&mut self) -> PResult<'a, AnonConst> {
873// Parse const argument.
874let (value, mgca_disambiguation) = if self.token.kind == token::OpenBrace {
875let value = self.parse_expr_block(None, self.token.span, BlockCheckMode::Default)?;
876 (value, MgcaDisambiguation::Direct)
877 } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(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
883let value = self.parse_mgca_const_block(true)?;
884 (value.value, MgcaDisambiguation::AnonConst)
885 } else {
886self.parse_unambiguous_unbraced_const_arg()?
887};
888Ok(AnonConst { id: ast::DUMMY_NODE_ID, value, mgca_disambiguation })
889 }
890891/// 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.
898pub(super) fn parse_unambiguous_unbraced_const_arg(
899&mut self,
900 ) -> PResult<'a, (Box<Expr>, MgcaDisambiguation)> {
901let start = self.token.span;
902let attrs = self.parse_outer_attributes()?;
903let (expr, _) =
904self.parse_expr_res(Restrictions::CONST_EXPR, attrs).map_err(|mut err| {
905err.span_label(
906start.shrink_to_lo(),
907"while parsing a const generic argument starting here",
908 );
909err910 })?;
911if !self.expr_is_valid_const_arg(&expr) {
912self.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 }
920921let mgca_disambiguation = self.mgca_direct_lit_hack(&expr);
922Ok((expr, mgca_disambiguation))
923 }
924925/// 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.
933pub fn mgca_direct_lit_hack(&self, expr: &Expr) -> MgcaDisambiguation {
934match &expr.kind {
935 ast::ExprKind::Lit(_) => MgcaDisambiguation::AnonConst,
936 ast::ExprKind::Unary(ast::UnOp::Neg, expr)
937if #[allow(non_exhaustive_omitted_patterns)] match expr.kind {
ast::ExprKind::Lit(_) => true,
_ => false,
}matches!(expr.kind, ast::ExprKind::Lit(_)) =>
938 {
939 MgcaDisambiguation::AnonConst940 }
941_ => MgcaDisambiguation::Direct,
942 }
943 }
944945/// 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`.
947pub(super) fn parse_generic_arg(
948&mut self,
949 ty_generics: Option<&Generics>,
950 ) -> PResult<'a, Option<GenericArg>> {
951let mut attr_span: Option<Span> = None;
952if self.token == token::Pound && self.look_ahead(1, |t| *t == token::OpenBracket) {
953let attrs_wrapper = self.parse_outer_attributes()?;
954let raw_attrs = attrs_wrapper.take_for_recovery(self.psess);
955attr_span = Some(raw_attrs[0].span.to(raw_attrs.last().unwrap().span));
956 }
957let start = self.token.span;
958let arg = if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) {
959// Parse lifetime argument.
960GenericArg::Lifetime(self.expect_lifetime())
961 } else if self.check_const_arg() {
962// Parse const argument.
963GenericArg::Const(self.parse_const_arg()?)
964 } else if self.check_type() {
965// Parse type argument.
966967 // 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.
970let mut snapshot = None;
971if self.may_recover() && self.token.can_begin_expr() {
972snapshot = Some(self.create_snapshot_for_diagnostic());
973 }
974975match self.parse_ty() {
976Ok(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.
981if 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) = snapshot984 && let Some(expr) =
985self.recover_unbraced_const_arg_that_can_begin_ty(snapshot)
986 {
987return Ok(Some(
988self.dummy_const_arg_needs_braces(
989self.dcx()
990 .struct_span_err(expr.span, "invalid const generic expression"),
991expr.span,
992 ),
993 ));
994 }
995996 GenericArg::Type(ty)
997 }
998Err(err) => {
999if let Some(snapshot) = snapshot1000 && let Some(expr) =
1001self.recover_unbraced_const_arg_that_can_begin_ty(snapshot)
1002 {
1003return Ok(Some(self.dummy_const_arg_needs_braces(err, expr.span)));
1004 }
1005// Try to recover from possible `const` arg without braces.
1006return self.recover_const_arg(start, err).map(Some);
1007 }
1008 }
1009 } else if self.token.is_keyword(kw::Const) {
1010return self.recover_const_param_declaration(ty_generics);
1011 } else if let Some(attr_span) = attr_span {
1012let diag = self.dcx().create_err(AttributeOnEmptyType { span: attr_span });
1013return 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.
1017let snapshot = self.create_snapshot_for_diagnostic();
1018let attrs = self.parse_outer_attributes()?;
1019match self.parse_expr_res(Restrictions::CONST_EXPR, attrs) {
1020Ok((expr, _)) => {
1021return Ok(Some(self.dummy_const_arg_needs_braces(
1022self.dcx().struct_span_err(expr.span, "invalid const generic expression"),
1023expr.span,
1024 )));
1025 }
1026Err(err) => {
1027self.restore_snapshot(snapshot);
1028err.cancel();
1029return Ok(None);
1030 }
1031 }
1032 };
10331034if let Some(attr_span) = attr_span {
1035let guar = self.dcx().emit_err(AttributeOnGenericArg {
1036 span: attr_span,
1037 fix_span: attr_span.until(arg.span()),
1038 });
1039return Ok(Some(match arg {
1040 GenericArg::Type(_) => GenericArg::Type(self.mk_ty(attr_span, TyKind::Err(guar))),
1041 GenericArg::Const(_) => {
1042let 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 }
10521053Ok(Some(arg))
1054 }
10551056/// 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.
1060fn get_ident_from_generic_arg(
1061&self,
1062 gen_arg: &GenericArg,
1063 ) -> Result<(bool, Ident, Option<GenericArgs>), ()> {
1064if let GenericArg::Type(ty) = gen_arg {
1065if let ast::TyKind::Path(qself, path) = &ty.kind
1066 && qself.is_none()
1067 && let [seg] = path.segments.as_slice()
1068 {
1069return 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::NONE1073 && let [seg] = trait_ref.trait_ref.path.segments.as_slice()
1074 {
1075return Ok((true, seg.ident, seg.args.as_deref().cloned()));
1076 }
1077 }
1078Err(())
1079 }
1080}