Skip to content

Add a new rustc_show_spans internal attribute - #164165

Open
FranciscoTGouveia wants to merge 1 commit into
rust-lang:mainfrom
FranciscoTGouveia:rustc-show-spans-attr
Open

FranciscoTGouveia wants to merge 1 commit into
rust-lang:mainfrom
FranciscoTGouveia:rustc-show-spans-attr

Conversation

@FranciscoTGouveia

@FranciscoTGouveia FranciscoTGouveia commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

This new internal attribute is a first step for solving #126763.

Given the code:

#![feature(rustc_attrs)]

mod x { pub const Y: u8 = 1; }

macro_rules! path  { ($a:tt, $b:tt) => { let _ = $a::$b; }; }

#[rustc_show_spans(path)]
fn no_macro() { let _ = x::Y; }

#[rustc_show_spans(path)]
fn path_bug() { path!(x, Y); }

We can now report the spans and thus show the bug of the span not falling inside the macro context:

note: showing the spans of the path
 --> test.rs:8:25
  |
8 | fn no_macro() { let _ = x::Y; }
  |                         ^^^^

note: showing the spans of the path
  --> test.rs:11:23
   |
11 | fn path_bug() { path!(x, Y); }
   |                       ^^^^

r? @petrochenkov

@rustbot

rustbot commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_attr_ir

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 11, 2026
Comment thread compiler/rustc_attr_parsing/src/attributes/rustc_dump.rs
// Internal attributes, Testing:
// ==========================================================================

sym::rustc_show_spans,

@FranciscoTGouveia FranciscoTGouveia Oct 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was undecided about whether to consider this new attribute as Testing or Misc.
Since it seems to be temporary for the migration to the new span combining scheme, I went with the former.

View changes since the review

impl<'a> Visitor<'a> for ShowSpans<'a> {
fn visit_item(&mut self, item: &'a ast::Item) {
let saved = (self.expr, self.ty, self.path);
(self.expr, self.ty, self.path) = (false, false, false);

@FranciscoTGouveia FranciscoTGouveia Oct 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before visiting a new item, I saved the current state of ShowSpans and reset it to support nested items.
Is this necessary, or is it redundant?

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That depends on whether this should print the path. I'd, perhaps naively, expect it to do print it, in which case resetting is wrong.

#[rustc_show_spans(path)]
fn bar() {
    fn foo() {
        let _ = a::b::c;
    }
}

(this is also something that should have a test)

@mejrs mejrs left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add some tests?

It need not be completely implemented, but its current state must be reflected in the test suite.

View changes since this review

if let ItemKind::Fn(_) = &item.kind
&& item.attrs.iter().any(|attr| attr.has_name(sym::rustc_show_spans))
&& let Some(Attribute::Parsed(AttributeKind::RustcShowSpans(kinds))) =
AttributeParser::parse_limited(self.sess, &item.attrs, &|attr| {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use parse_limited_sym in this case.

Also, no need for the item.attrs.iter().any(|attr| attr.has_name(sym::rustc_show_spans)) check, attribute parsing will check this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I did not know about parse_limited_sym; thanks!

The idea with item.attrs.iter().any(|attr| attr.has_name(sym::rustc_show_spans)) was to have a cheaper check to exit early. However, I am now realizing this is not a hot path, so the benefit is nil.

Comment thread compiler/rustc_attr_parsing/src/attributes/rustc_dump.rs
impl<'a> Visitor<'a> for ShowSpans<'a> {
fn visit_item(&mut self, item: &'a ast::Item) {
let saved = (self.expr, self.ty, self.path);
(self.expr, self.ty, self.path) = (false, false, false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That depends on whether this should print the path. I'd, perhaps naively, expect it to do print it, in which case resetting is wrong.

#[rustc_show_spans(path)]
fn bar() {
    fn foo() {
        let _ = a::b::c;
    }
}

(this is also something that should have a test)

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 12, 2026
@rustbot

rustbot commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

This new attribute is a first step for solving issue 126763.
Now, we can easily test out where spans end up, particularly useful for
ensuring that spans end up in the best suitable macro context.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants