Lower spans in AttrPath correctly

This commit is contained in:
Jonathan Brouwer 2025-12-02 19:42:41 +01:00
parent 892bd18ad8
commit 31d289c9bc
No known key found for this signature in database
GPG key ID: 13619B051B673C52
4 changed files with 17 additions and 9 deletions

View file

@ -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

View file

@ -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,

View file

@ -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),
});
}
}

View file

@ -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::<Vec<_>>().into_boxed_slice(),
span: path.span,
segments: path
.segments
.iter()
.map(|i| Ident { name: i.ident.name, span: lower_span(i.ident.span) })
.collect::<Vec<_>>()
.into_boxed_slice(),
span: lower_span(path.span),
}
}
}