From fdefc162fb248b2f516e2827015f209487820d0e Mon Sep 17 00:00:00 2001 From: Francisco Gouveia Date: Sun, 11 Oct 2026 18:32:31 +0100 Subject: [PATCH 1/3] Add a new `rustc_show_spans` internal attribute 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. --- compiler/rustc_ast_passes/src/diagnostics.rs | 8 +++ compiler/rustc_ast_passes/src/lib.rs | 1 + compiler/rustc_ast_passes/src/show_spans.rs | 67 +++++++++++++++++++ compiler/rustc_attr_ir/src/data_structures.rs | 10 +++ .../rustc_attr_ir/src/encode_cross_crate.rs | 2 + .../src/attributes/rustc_dump.rs | 43 +++++++++++- compiler/rustc_attr_parsing/src/context.rs | 1 + compiler/rustc_feature/src/builtin_attrs.rs | 1 + compiler/rustc_interface/src/passes.rs | 4 ++ compiler/rustc_passes/src/check_attr.rs | 1 + compiler/rustc_span/src/symbol.rs | 1 + 11 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 compiler/rustc_ast_passes/src/show_spans.rs diff --git a/compiler/rustc_ast_passes/src/diagnostics.rs b/compiler/rustc_ast_passes/src/diagnostics.rs index bb006b32ac73e..7ade88ca317c2 100644 --- a/compiler/rustc_ast_passes/src/diagnostics.rs +++ b/compiler/rustc_ast_passes/src/diagnostics.rs @@ -1325,3 +1325,11 @@ pub(crate) struct AbiCustomCannotBeCold { #[label("`extern \"custom\"` because of this")] pub abi_span: Span, } + +#[derive(Diagnostic)] +#[diag("showing the spans of the {$kind}")] +pub(crate) struct ShowSpan { + #[primary_span] + pub span: Span, + pub kind: &'static str, +} diff --git a/compiler/rustc_ast_passes/src/lib.rs b/compiler/rustc_ast_passes/src/lib.rs index 918a623b41752..5742d5a9e1a73 100644 --- a/compiler/rustc_ast_passes/src/lib.rs +++ b/compiler/rustc_ast_passes/src/lib.rs @@ -12,3 +12,4 @@ pub mod ast_validation; mod diagnostics; pub mod feature_gate; +pub mod show_spans; diff --git a/compiler/rustc_ast_passes/src/show_spans.rs b/compiler/rustc_ast_passes/src/show_spans.rs new file mode 100644 index 0000000000000..b28535653b0ed --- /dev/null +++ b/compiler/rustc_ast_passes/src/show_spans.rs @@ -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); +} diff --git a/compiler/rustc_attr_ir/src/data_structures.rs b/compiler/rustc_attr_ir/src/data_structures.rs index 04fee06e2434c..1a6f32aa112cd 100644 --- a/compiler/rustc_attr_ir/src/data_structures.rs +++ b/compiler/rustc_attr_ir/src/data_structures.rs @@ -576,6 +576,13 @@ impl rustc_serialize::Encodable for DocAttribute } } +#[derive(Clone, Debug, StableHash, Encodable, Decodable, PrintAttribute)] +pub enum RustcShowSpansKind { + Expr, + Ty, + Path, +} + #[derive(Clone, Debug, StableHash, Encodable, Decodable, PrintAttribute)] pub enum RustcDumpLayoutKind { Align, @@ -1407,6 +1414,9 @@ pub enum AttributeKind { /// Represents `#[rustc_should_not_be_called_on_const_items]` RustcShouldNotBeCalledOnConstItems, + /// Represents `#[rustc_show_spans]` + RustcShowSpans(ThinVec), + /// Represents `#[rustc_simd_monomorphize_lane_limit = "N"]`. RustcSimdMonomorphizeLaneLimit(Limit), diff --git a/compiler/rustc_attr_ir/src/encode_cross_crate.rs b/compiler/rustc_attr_ir/src/encode_cross_crate.rs index b6733e8c1db54..1bebec4ed0183 100644 --- a/compiler/rustc_attr_ir/src/encode_cross_crate.rs +++ b/compiler/rustc_attr_ir/src/encode_cross_crate.rs @@ -187,6 +187,7 @@ impl AttributeKind { RustcRegions => No, RustcScalableVector { .. } => Yes, RustcShouldNotBeCalledOnConstItems => Yes, + RustcShowSpans(_) => No, RustcSimdMonomorphizeLaneLimit(..) => Yes, // Affects layout computation, which needs to work cross-crate RustcSkipDuringMethodDispatch { .. } => No, RustcSpecializationTrait => No, @@ -401,6 +402,7 @@ impl AttributeKind { RustcRegions => false, RustcScalableVector { .. } => false, RustcShouldNotBeCalledOnConstItems => false, + RustcShowSpans(_) => false, RustcSimdMonomorphizeLaneLimit(..) => false, RustcSkipDuringMethodDispatch { .. } => false, RustcSpecializationTrait => false, diff --git a/compiler/rustc_attr_parsing/src/attributes/rustc_dump.rs b/compiler/rustc_attr_parsing/src/attributes/rustc_dump.rs index 99c7e933b07b4..07ce47b8858e2 100644 --- a/compiler/rustc_attr_parsing/src/attributes/rustc_dump.rs +++ b/compiler/rustc_attr_parsing/src/attributes/rustc_dump.rs @@ -1,5 +1,5 @@ use rustc_attr_ir::target::{AssocCtxt, MethodKind, Target}; -use rustc_attr_ir::{AttributeKind, RustcDumpLayoutKind}; +use rustc_attr_ir::{AttributeKind, RustcDumpLayoutKind, RustcShowSpansKind}; use rustc_feature::AttributeStability; use rustc_span::{Span, Symbol, sym}; @@ -301,3 +301,44 @@ impl NoArgsAttributeParser for RustcDumpVtableParser { const STABILITY: AttributeStability = unstable!(rustc_attrs); const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcDumpVtable; } + +pub(crate) struct RustcShowSpans; + +impl SingleAttributeParser for RustcShowSpans { + const PATH: &[Symbol] = &[sym::rustc_show_spans]; + + const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Fn)]); + + const TEMPLATE: AttributeTemplate = template!(List: &["expr", "ty", "path"]); + const STABILITY: AttributeStability = unstable!(rustc_attrs); + + fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option { + let Some(items) = cx.expect_list(args, cx.attr_span) else { + return None; + }; + + let mut result = ThinVec::new(); + for item in items.mixed() { + let Some(arg) = item.meta_item_no_args() else { + cx.adcx().expected_not_literal(item.span()); + continue; + }; + let Some(ident) = arg.ident() else { + cx.adcx().expected_identifier(arg.span()); + return None; + }; + let kind = match ident.name { + sym::expr => RustcShowSpansKind::Expr, + sym::ty => RustcShowSpansKind::Ty, + sym::path => RustcShowSpansKind::Path, + _ => { + cx.adcx() + .expected_specific_argument(ident.span, &[sym::expr, sym::ty, sym::path]); + continue; + } + }; + result.push(kind); + } + Some(AttributeKind::RustcShowSpans(result)) + } +} diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs index 060fd74c1db21..b6b1ce18faca6 100644 --- a/compiler/rustc_attr_parsing/src/context.rs +++ b/compiler/rustc_attr_parsing/src/context.rs @@ -249,6 +249,7 @@ attribute_parsers!( Single, Single, Single, + Single, Single, Single, Single, diff --git a/compiler/rustc_feature/src/builtin_attrs.rs b/compiler/rustc_feature/src/builtin_attrs.rs index bc8c8f4b2a764..7c3c5c58a596b 100644 --- a/compiler/rustc_feature/src/builtin_attrs.rs +++ b/compiler/rustc_feature/src/builtin_attrs.rs @@ -384,6 +384,7 @@ pub static BUILTIN_ATTRIBUTES: &[Symbol] = &[ // Internal attributes, Testing: // ========================================================================== + sym::rustc_show_spans, sym::rustc_effective_visibility, sym::rustc_dump_inferred_outlives, sym::rustc_capture_analysis, diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 679de1e75613f..f0990d075e396 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -266,6 +266,10 @@ fn configure_and_expand( ) }); + if features.rustc_attrs() { + rustc_ast_passes::show_spans::check_crate(sess, &krate); + } + let crate_types = tcx.crate_types(); let is_executable_crate = crate_types.contains(&CrateType::Executable); let is_proc_macro_crate = crate_types.contains(&CrateType::ProcMacro); diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 3eb6892177b05..8bd68c26b9254 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -383,6 +383,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { AttributeKind::RustcRegions => (), AttributeKind::RustcScalableVector { .. } => (), AttributeKind::RustcShouldNotBeCalledOnConstItems => (), + AttributeKind::RustcShowSpans(_) => (), AttributeKind::RustcSimdMonomorphizeLaneLimit(..) => (), AttributeKind::RustcSkipDuringMethodDispatch { .. } => (), diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index a9275f489308a..3329b7b647279 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1913,6 +1913,7 @@ symbols! { rustc_regions, rustc_scalable_vector, rustc_should_not_be_called_on_const_items, + rustc_show_spans, rustc_simd_monomorphize_lane_limit, rustc_skip_during_method_dispatch, rustc_specialization_trait, From 5448cc68ec6ed5bf2e1496f018143630eaacb615 Mon Sep 17 00:00:00 2001 From: Francisco Gouveia Date: Tue, 13 Oct 2026 18:28:52 +0100 Subject: [PATCH 2/3] Add a test for the `rustc_show_spans` attribute use in macros --- tests/ui/attributes/show_spans_macros.rs | 44 ++++++++++++++++++++ tests/ui/attributes/show_spans_macros.stderr | 18 ++++++++ 2 files changed, 62 insertions(+) create mode 100644 tests/ui/attributes/show_spans_macros.rs create mode 100644 tests/ui/attributes/show_spans_macros.stderr diff --git a/tests/ui/attributes/show_spans_macros.rs b/tests/ui/attributes/show_spans_macros.rs new file mode 100644 index 0000000000000..a8a435b009e16 --- /dev/null +++ b/tests/ui/attributes/show_spans_macros.rs @@ -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() {} diff --git a/tests/ui/attributes/show_spans_macros.stderr b/tests/ui/attributes/show_spans_macros.stderr new file mode 100644 index 0000000000000..0fa76e9cc9392 --- /dev/null +++ b/tests/ui/attributes/show_spans_macros.stderr @@ -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); + | ^ + From 29e3a087d013e13dab3007e3556aab5402e1e61f Mon Sep 17 00:00:00 2001 From: Francisco Gouveia Date: Tue, 13 Oct 2026 18:01:20 +0100 Subject: [PATCH 3/3] Add a test for the `rustc_show_spans` attribute on nested functions --- .../ui/attributes/show_spans-nested-items.rs | 28 +++++++++++++++++++ .../attributes/show_spans-nested-items.stderr | 12 ++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/ui/attributes/show_spans-nested-items.rs create mode 100644 tests/ui/attributes/show_spans-nested-items.stderr diff --git a/tests/ui/attributes/show_spans-nested-items.rs b/tests/ui/attributes/show_spans-nested-items.rs new file mode 100644 index 0000000000000..52ff359ebd129 --- /dev/null +++ b/tests/ui/attributes/show_spans-nested-items.rs @@ -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() {} diff --git a/tests/ui/attributes/show_spans-nested-items.stderr b/tests/ui/attributes/show_spans-nested-items.stderr new file mode 100644 index 0000000000000..c1ca87bbce003 --- /dev/null +++ b/tests/ui/attributes/show_spans-nested-items.stderr @@ -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; + | ^ +