From 31d289c9bc0bea53942cea6ccca760db53e6e3bd Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Tue, 2 Dec 2025 19:42:41 +0100 Subject: [PATCH] Lower spans in `AttrPath` correctly --- compiler/rustc_attr_parsing/src/attributes/cfg.rs | 4 +++- compiler/rustc_attr_parsing/src/interface.rs | 8 ++++---- compiler/rustc_attr_parsing/src/validate_attr.rs | 3 ++- compiler/rustc_hir/src/hir.rs | 11 ++++++++--- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_attr_parsing/src/attributes/cfg.rs b/compiler/rustc_attr_parsing/src/attributes/cfg.rs index e47fa075fbf7..bd228315b2c9 100644 --- a/compiler/rustc_attr_parsing/src/attributes/cfg.rs +++ b/compiler/rustc_attr_parsing/src/attributes/cfg.rs @@ -1,3 +1,5 @@ +use std::convert::identity; + use rustc_ast::token::Delimiter; use rustc_ast::tokenstream::DelimSpan; use rustc_ast::{AttrItem, Attribute, CRATE_NODE_ID, LitKind, NodeId, ast, token}; @@ -353,7 +355,7 @@ pub fn parse_cfg_attr( span, attr_span: cfg_attr.span, template: CFG_ATTR_TEMPLATE, - path: AttrPath::from_ast(&cfg_attr.get_normal_item().path), + path: AttrPath::from_ast(&cfg_attr.get_normal_item().path, identity), description: ParsedDescription::Attribute, reason, suggestions: CFG_ATTR_TEMPLATE diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index c6d8411e8356..87e29b7b0de6 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -300,9 +300,10 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> { // } ast::AttrKind::Normal(n) => { attr_paths.push(PathParser(Cow::Borrowed(&n.item.path))); + let attr_path = AttrPath::from_ast(&n.item.path, lower_span); self.check_attribute_safety( - &AttrPath::from_ast(&n.item.path), + &attr_path, lower_span(n.item.span()), n.item.unsafety, &mut emit_lint, @@ -321,7 +322,6 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> { ) else { continue; }; - let path = parser.path(); let args = parser.args(); for accept in accepts { let mut cx: AcceptContext<'_, 'sess, S> = AcceptContext { @@ -336,7 +336,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> { attr_style: attr.style, parsed_description: ParsedDescription::Attribute, template: &accept.template, - attr_path: path.get_attribute_path(), + attr_path: attr_path.clone(), }; (accept.accept_fn)(&mut cx, args); @@ -361,7 +361,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> { // ); attributes.push(Attribute::Unparsed(Box::new(AttrItem { - path: AttrPath::from_ast(&n.item.path), + path: attr_path.clone(), args: self.lower_attr_args(&n.item.args, lower_span), id: HashIgnoredAttrId { attr_id: attr.id }, style: attr.style, diff --git a/compiler/rustc_attr_parsing/src/validate_attr.rs b/compiler/rustc_attr_parsing/src/validate_attr.rs index 9034fe45427f..c57e0baea05f 100644 --- a/compiler/rustc_attr_parsing/src/validate_attr.rs +++ b/compiler/rustc_attr_parsing/src/validate_attr.rs @@ -1,5 +1,6 @@ //! Meta-syntax validation logic of attributes for post-expansion. +use std::convert::identity; use std::slice; use rustc_ast::token::Delimiter; @@ -163,7 +164,7 @@ pub fn check_builtin_meta_item( if deny_unsafety && let Safety::Unsafe(unsafe_span) = meta.unsafety { psess.dcx().emit_err(errors::InvalidAttrUnsafe { span: unsafe_span, - name: AttrPath::from_ast(&meta.path), + name: AttrPath::from_ast(&meta.path, identity), }); } } diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 8f297677ff85..94c47b06e635 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1193,10 +1193,15 @@ impl IntoDiagArg for AttrPath { } impl AttrPath { - pub fn from_ast(path: &ast::Path) -> Self { + pub fn from_ast(path: &ast::Path, lower_span: impl Copy + Fn(Span) -> Span) -> Self { AttrPath { - segments: path.segments.iter().map(|i| i.ident).collect::>().into_boxed_slice(), - span: path.span, + segments: path + .segments + .iter() + .map(|i| Ident { name: i.ident.name, span: lower_span(i.ident.span) }) + .collect::>() + .into_boxed_slice(), + span: lower_span(path.span), } } }