Repository navigation
Add a new rustc_show_spans internal attribute
#164165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rust-bors
merged 3 commits into
rust-lang:main
from
FranciscoTGouveia:rustc-show-spans-attr
Oct 14, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,4 @@ | |
| pub mod ast_validation; | ||
| mod diagnostics; | ||
| pub mod feature_gate; | ||
| pub mod show_spans; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| use rustc_ast::visit::{self, Visitor}; | ||
| use rustc_ast::{self as ast, ItemKind}; | ||
| use rustc_attr_ir::{Attribute, AttributeKind, RustcShowSpansKind}; | ||
| use rustc_attr_parsing::AttributeParser; | ||
| use rustc_session::Session; | ||
| use rustc_span::sym; | ||
|
|
||
| use crate::diagnostics; | ||
|
|
||
| struct ShowSpans<'a> { | ||
| sess: &'a Session, | ||
| expr: bool, | ||
| ty: bool, | ||
| path: bool, | ||
| } | ||
|
|
||
| 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); | ||
|
|
||
| if let ItemKind::Fn(_) = &item.kind | ||
| && let Some(Attribute::Parsed(AttributeKind::RustcShowSpans(kinds))) = | ||
| AttributeParser::parse_limited_sym(self.sess, &item.attrs, &[sym::rustc_show_spans]) | ||
| { | ||
| for kind in kinds { | ||
| match kind { | ||
| RustcShowSpansKind::Expr => self.expr = true, | ||
| RustcShowSpansKind::Ty => self.ty = true, | ||
| RustcShowSpansKind::Path => self.path = true, | ||
| } | ||
| } | ||
| } | ||
| visit::walk_item(self, item); | ||
|
|
||
| // Recover the state. Needed if we are looking into nested items. | ||
| (self.expr, self.ty, self.path) = saved; | ||
| } | ||
|
|
||
| // We override it as to not report attribute paths as paths. | ||
| fn visit_attribute(&mut self, _: &'a ast::Attribute) {} | ||
|
|
||
| fn visit_expr(&mut self, e: &'a ast::Expr) { | ||
| if self.expr { | ||
| self.sess.dcx().emit_note(diagnostics::ShowSpan { span: e.span, kind: "expr" }); | ||
| } | ||
| visit::walk_expr(self, e); | ||
| } | ||
|
|
||
| fn visit_ty(&mut self, t: &'a ast::Ty) { | ||
| if self.ty { | ||
| self.sess.dcx().emit_note(diagnostics::ShowSpan { span: t.span, kind: "ty" }); | ||
| } | ||
| visit::walk_ty(self, t); | ||
| } | ||
|
|
||
| fn visit_path(&mut self, p: &'a ast::Path) { | ||
| if self.path { | ||
| self.sess.dcx().emit_note(diagnostics::ShowSpan { span: p.span, kind: "path" }); | ||
| } | ||
| visit::walk_path(self, p); | ||
| } | ||
| } | ||
|
|
||
| pub fn check_crate(sess: &Session, krate: &ast::Crate) { | ||
| visit::walk_crate(&mut ShowSpans { sess, expr: false, ty: false, path: false }, krate); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Illustrates the behavior of the `rustc_show_spans` internal attribute | ||
| // in the case of nested functions. | ||
|
|
||
| //@ check-pass | ||
| #![feature(rustc_attrs)] | ||
|
|
||
| #[rustc_show_spans(expr)] | ||
| fn outer() { | ||
| // No note because it's a nested function. | ||
| fn inner() { | ||
| let _ = 0; | ||
| } | ||
|
|
||
| // The span will be over the `ty` (i.e, `u8`), not the `expr` (i.e., `1`). | ||
| #[rustc_show_spans(ty)] | ||
| fn inner_annotated() { | ||
| let _: u8 = 1; //~ NOTE showing the spans of the ty | ||
| } | ||
|
|
||
| let _: u8 = 2; //~ NOTE showing the spans of the expr | ||
| } | ||
|
|
||
| // No note as the annotation of the previous function does not affect this one. | ||
| fn not_annotated() { | ||
| let _ = 3; | ||
| } | ||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| note: showing the spans of the ty | ||
| --> $DIR/show_spans-nested-items.rs:17:16 | ||
| | | ||
| LL | let _: u8 = 1; | ||
| | ^^ | ||
|
|
||
| note: showing the spans of the expr | ||
| --> $DIR/show_spans-nested-items.rs:20:17 | ||
| | | ||
| LL | let _: u8 = 2; | ||
| | ^ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Illustrates the behavior of the `rustc_show_spans` internal attribute. | ||
| // It shows how the parser combines spans of nodes built from tokens of | ||
| // different macro contexts. | ||
| // | ||
| // The objective is for a node's span to be a chain over its internal | ||
| // members and not only from first to last. That is, we should converge | ||
| // to `span(first).to(span(second))...to(span(last))` instead of just | ||
| // `span(first).to(span(last))`. This way we can report spans inside | ||
| // macro definitions when the children come from different contexts. | ||
|
|
||
| //@ check-pass | ||
| #![feature(rustc_attrs)] | ||
|
|
||
| mod foo { | ||
| pub const BAR: u8 = 1; | ||
| } | ||
|
|
||
| // FIXME(#126763): the span should be `$a::$b` in the macro body. | ||
| macro_rules! path { | ||
| ($a:tt, $b:tt) => { | ||
| let _ = $a::$b; | ||
| }; | ||
| } | ||
|
|
||
| #[rustc_show_spans(path)] | ||
| fn path_from_tts() { | ||
| path!(foo, BAR); | ||
| } | ||
|
|
||
| // FIXME(#126763): the field access span should be `$a.$f` in the macro body. | ||
| macro_rules! field { | ||
| ($a:tt, $f:tt) => { | ||
| let _ = $a.$f; | ||
| }; | ||
| } | ||
|
|
||
| const T: (u8,) = (1,); | ||
|
|
||
| #[rustc_show_spans(expr)] | ||
| fn field_access() { | ||
| field!(T, 0); | ||
| } | ||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| note: showing the spans of the path | ||
| --> $DIR/show_spans_macros.rs:27:11 | ||
| | | ||
| LL | path!(foo, BAR); | ||
| | ^^^^^^^^ | ||
|
|
||
| note: showing the spans of the expr | ||
| --> $DIR/show_spans_macros.rs:41:12 | ||
| | | ||
| LL | field!(T, 0); | ||
| | ^^^^ | ||
|
|
||
| note: showing the spans of the expr | ||
| --> $DIR/show_spans_macros.rs:41:12 | ||
| | | ||
| LL | field!(T, 0); | ||
| | ^ | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.