Skip to content

Commit c25a56b

Browse files
Lower spans in AttrPath correctly
1 parent 65d34fc commit c25a56b

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::convert::identity;
2+
13
use rustc_ast::token::Delimiter;
24
use rustc_ast::tokenstream::DelimSpan;
35
use rustc_ast::{AttrItem, Attribute, CRATE_NODE_ID, LitKind, NodeId, ast, token};
@@ -353,7 +355,7 @@ pub fn parse_cfg_attr(
353355
span,
354356
attr_span: cfg_attr.span,
355357
template: CFG_ATTR_TEMPLATE,
356-
path: AttrPath::from_ast(&cfg_attr.get_normal_item().path),
358+
path: AttrPath::from_ast(&cfg_attr.get_normal_item().path, identity),
357359
description: ParsedDescription::Attribute,
358360
reason,
359361
suggestions: CFG_ATTR_TEMPLATE

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,10 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
300300
// }
301301
ast::AttrKind::Normal(n) => {
302302
attr_paths.push(PathParser(Cow::Borrowed(&n.item.path)));
303+
let attr_path = AttrPath::from_ast(&n.item.path, lower_span);
303304

304305
self.check_attribute_safety(
305-
&AttrPath::from_ast(&n.item.path),
306+
&attr_path,
306307
lower_span(n.item.span()),
307308
n.item.unsafety,
308309
&mut emit_lint,
@@ -321,7 +322,6 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
321322
) else {
322323
continue;
323324
};
324-
let path = parser.path();
325325
let args = parser.args();
326326
for accept in accepts {
327327
let mut cx: AcceptContext<'_, 'sess, S> = AcceptContext {
@@ -336,7 +336,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
336336
attr_style: attr.style,
337337
parsed_description: ParsedDescription::Attribute,
338338
template: &accept.template,
339-
attr_path: path.get_attribute_path(),
339+
attr_path: attr_path.clone(),
340340
};
341341

342342
(accept.accept_fn)(&mut cx, args);
@@ -361,7 +361,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
361361
// );
362362

363363
attributes.push(Attribute::Unparsed(Box::new(AttrItem {
364-
path: AttrPath::from_ast(&n.item.path),
364+
path: attr_path.clone(),
365365
args: self.lower_attr_args(&n.item.args, lower_span),
366366
id: HashIgnoredAttrId { attr_id: attr.id },
367367
style: attr.style,

compiler/rustc_attr_parsing/src/validate_attr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Meta-syntax validation logic of attributes for post-expansion.
22
3+
use std::convert::identity;
34
use std::slice;
45

56
use rustc_ast::token::Delimiter;
@@ -157,7 +158,7 @@ pub fn deny_builtin_meta_unsafety(diag: DiagCtxtHandle<'_>, unsafety: Safety, na
157158
if let Safety::Unsafe(unsafe_span) = unsafety {
158159
diag.emit_err(errors::InvalidAttrUnsafe {
159160
span: unsafe_span,
160-
name: AttrPath::from_ast(&name),
161+
name: AttrPath::from_ast(&name, identity),
161162
});
162163
}
163164
}
@@ -178,7 +179,7 @@ pub fn check_builtin_meta_item(
178179
if deny_unsafety && let Safety::Unsafe(unsafe_span) = meta.unsafety {
179180
psess.dcx().emit_err(errors::InvalidAttrUnsafe {
180181
span: unsafe_span,
181-
name: AttrPath::from_ast(&meta.path),
182+
name: AttrPath::from_ast(&meta.path, identity),
182183
});
183184
}
184185
}

compiler/rustc_hir/src/hir.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,10 +1193,15 @@ impl IntoDiagArg for AttrPath {
11931193
}
11941194

11951195
impl AttrPath {
1196-
pub fn from_ast(path: &ast::Path) -> Self {
1196+
pub fn from_ast(path: &ast::Path, lower_span: impl Copy + Fn(Span) -> Span) -> Self {
11971197
AttrPath {
1198-
segments: path.segments.iter().map(|i| i.ident).collect::<Vec<_>>().into_boxed_slice(),
1199-
span: path.span,
1198+
segments: path
1199+
.segments
1200+
.iter()
1201+
.map(|i| Ident { name: i.ident.name, span: lower_span(i.ident.span) })
1202+
.collect::<Vec<_>>()
1203+
.into_boxed_slice(),
1204+
span: lower_span(path.span),
12001205
}
12011206
}
12021207
}

0 commit comments

Comments
 (0)