about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/parser/path.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-08-06 23:23:26 +0000
committerbors <bors@rust-lang.org>2025-08-06 23:23:26 +0000
commit6bcdcc73bd11568fd85f5a38b58e1eda054ad1cd (patch)
tree7404b3b99695ca9e1343e7ae067f38a3aae59a2a /compiler/rustc_parse/src/parser/path.rs
parent7d82b83ed57d188ab3f2441a765a6419685a88a3 (diff)
parentd369a1fe5e7fbf17cc3ed74057928b875fe40ce7 (diff)
downloadrust-6bcdcc73bd11568fd85f5a38b58e1eda054ad1cd.tar.gz
rust-6bcdcc73bd11568fd85f5a38b58e1eda054ad1cd.zip
Auto merge of #145020 - GuillaumeGomez:rollup-5prs9kw, r=GuillaumeGomez
Rollup of 15 pull requests

Successful merges:

 - rust-lang/rust#144195 (Parser: Recover from attributes applied to types and generic args)
 - rust-lang/rust#144794 (Port `#[coroutine]` to the new attribute system)
 - rust-lang/rust#144835 (Anonymize binders in tail call sig)
 - rust-lang/rust#144861 (Stabilize `panic_payload_as_str` feature)
 - rust-lang/rust#144917 (Enforce tail call type is related to body return type in borrowck)
 - rust-lang/rust#144948 (we only merge candidates for trait and normalizes-to goals)
 - rust-lang/rust#144956 (Gate const trait syntax)
 - rust-lang/rust#144970 (rustdoc: fix caching of intra-doc links on reexports)
 - rust-lang/rust#144972 (add code example showing that file_prefix treats dotfiles as the name of a file, not an extension)
 - rust-lang/rust#144975 (`File::set_times`: Update documentation and example to support setting timestamps on directories)
 - rust-lang/rust#144977 (Fortify generic param default checks)
 - rust-lang/rust#144996 (simplifycfg: Mark as changed when start is modified in collapse goto chain)
 - rust-lang/rust#144998 (mir: Do not modify NonUse in `super_projection_elem`)
 - rust-lang/rust#145000 (Remove unneeded `stage` parameter when setting up stdlib Cargo)
 - rust-lang/rust#145008 (Fix rustdoc scrape examples crash)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_parse/src/parser/path.rs')
-rw-r--r--compiler/rustc_parse/src/parser/path.rs31
1 files changed, 28 insertions, 3 deletions
diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs
index 1f4049f197f..8e65ab99c5e 100644
--- a/compiler/rustc_parse/src/parser/path.rs
+++ b/compiler/rustc_parse/src/parser/path.rs
@@ -17,11 +17,11 @@ use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
 use super::{Parser, Restrictions, TokenType};
 use crate::ast::{PatKind, TyKind};
 use crate::errors::{
-    self, FnPathFoundNamedParams, PathFoundAttributeInParams, PathFoundCVariadicParams,
-    PathSingleColon, PathTripleColon,
+    self, AttributeOnEmptyType, AttributeOnGenericArg, FnPathFoundNamedParams,
+    PathFoundAttributeInParams, PathFoundCVariadicParams, PathSingleColon, PathTripleColon,
 };
 use crate::exp;
-use crate::parser::{CommaRecoveryMode, RecoverColon, RecoverComma};
+use crate::parser::{CommaRecoveryMode, ExprKind, RecoverColon, RecoverComma};
 
 /// Specifies how to parse a path.
 #[derive(Copy, Clone, PartialEq)]
@@ -880,6 +880,12 @@ impl<'a> Parser<'a> {
         &mut self,
         ty_generics: Option<&Generics>,
     ) -> PResult<'a, Option<GenericArg>> {
+        let mut attr_span: Option<Span> = None;
+        if self.token == token::Pound && self.look_ahead(1, |t| *t == token::OpenBracket) {
+            let attrs_wrapper = self.parse_outer_attributes()?;
+            let raw_attrs = attrs_wrapper.take_for_recovery(self.psess);
+            attr_span = Some(raw_attrs[0].span.to(raw_attrs.last().unwrap().span));
+        }
         let start = self.token.span;
         let arg = if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) {
             // Parse lifetime argument.
@@ -934,6 +940,9 @@ impl<'a> Parser<'a> {
             }
         } else if self.token.is_keyword(kw::Const) {
             return self.recover_const_param_declaration(ty_generics);
+        } else if let Some(attr_span) = attr_span {
+            let diag = self.dcx().create_err(AttributeOnEmptyType { span: attr_span });
+            return Err(diag);
         } else {
             // Fall back by trying to parse a const-expr expression. If we successfully do so,
             // then we should report an error that it needs to be wrapped in braces.
@@ -953,6 +962,22 @@ impl<'a> Parser<'a> {
                 }
             }
         };
+
+        if let Some(attr_span) = attr_span {
+            let guar = self.dcx().emit_err(AttributeOnGenericArg {
+                span: attr_span,
+                fix_span: attr_span.until(arg.span()),
+            });
+            return Ok(Some(match arg {
+                GenericArg::Type(_) => GenericArg::Type(self.mk_ty(attr_span, TyKind::Err(guar))),
+                GenericArg::Const(_) => {
+                    let error_expr = self.mk_expr(attr_span, ExprKind::Err(guar));
+                    GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value: error_expr })
+                }
+                GenericArg::Lifetime(lt) => GenericArg::Lifetime(lt),
+            }));
+        }
+
         Ok(Some(arg))
     }