Skip to content

Commit 15ccad3

Browse files
Detect if item.span is in a macro, and fall back
If item.span is part of a macro invocation, this has several downstream implications. To name two that were found while working on this: - The dead-code error gets annotated with a "in this macro invocation" - Some errors get canceled if they refer to remote crates Ideally, we should annotate item.ident.span with the same macro info, but this is a larger change (see: #66095), so for now we just fall back to the old behavior if this item was generated by a macro. I use span.macro_backtrace().len() to detect if it's part of a macro, because that (among other things) is what is used by the code which adds the "in this macro invocation" annotations mentioned above.
1 parent 7985510 commit 15ccad3

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/librustc_passes/dead.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -569,16 +569,26 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
569569

570570
fn visit_item(&mut self, item: &'tcx hir::Item) {
571571
if self.should_warn_about_item(item) {
572-
// For items that have a definition with a signature followed by a
573-
// block, point only at the signature.
572+
// For most items, we want to highlight its identifier
574573
let span = match item.kind {
575574
hir::ItemKind::Fn(..) |
576575
hir::ItemKind::Mod(..) |
577576
hir::ItemKind::Enum(..) |
578577
hir::ItemKind::Struct(..) |
579578
hir::ItemKind::Union(..) |
580579
hir::ItemKind::Trait(..) |
581-
hir::ItemKind::Impl(..) => item.ident.span,
580+
hir::ItemKind::Impl(..) => {
581+
// FIXME(66095): Because item.span is annotated with things
582+
// like a macro_backtrace, and ident.span isn't, we use the
583+
// def_span method if it's part of a macro invocation
584+
// We should probably annotate ident.span with the macro
585+
// context, but that's a larger change.
586+
if item.span.macro_backtrace().len() == 0 {
587+
item.ident.span
588+
} else {
589+
self.tcx.sess.source_map().def_span(item.span)
590+
}
591+
},
582592
_ => item.span,
583593
};
584594
let participle = match item.kind {

src/test/ui/span/macro-span-replacement.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
warning: struct is never constructed: `S`
2-
--> $DIR/macro-span-replacement.rs:7:12
2+
--> $DIR/macro-span-replacement.rs:7:14
33
|
44
LL | $b $a;
5-
| ^^
5+
| ^
6+
...
7+
LL | m!(S struct);
8+
| ------------- in this macro invocation
69
|
710
note: lint level defined here
811
--> $DIR/macro-span-replacement.rs:3:9

0 commit comments

Comments
 (0)