Rollup merge of #156587 - GuillaumeGomez:assoc-items-macro-expansion, r=Urgau

Correctly handle associated items in rustdoc macro expansion

Fixes rust-lang/rust#156075.

The bug was simply that it didn't cover associated items.

r? @Urgau
This commit is contained in:
Jonathan Brouwer
2026-05-15 18:16:40 +02:00
committed by GitHub
3 changed files with 64 additions and 2 deletions
+15 -2
View File
@@ -1,5 +1,8 @@
use rustc_ast::visit::{Visitor, walk_crate, walk_expr, walk_item, walk_pat, walk_stmt, walk_ty};
use rustc_ast::{Crate, Expr, Item, Pat, Stmt, Ty};
use rustc_ast::visit::{
AssocCtxt, Visitor, walk_assoc_item, walk_crate, walk_expr, walk_item, walk_pat, walk_stmt,
walk_ty,
};
use rustc_ast::{AssocItem, Crate, Expr, Item, Pat, Stmt, Ty};
use rustc_data_structures::fx::FxHashMap;
use rustc_span::source_map::SourceMap;
use rustc_span::{BytePos, Span};
@@ -161,4 +164,14 @@ fn visit_ty(&mut self, ty: &'ast Ty) {
walk_ty(self, ty);
}
}
fn visit_assoc_item(&mut self, item: &'ast AssocItem, ctxt: AssocCtxt) -> Self::Result {
if item.span.from_expansion() {
self.handle_new_span(item.span, || {
rustc_ast_pretty::pprust::assoc_item_to_string(item)
});
} else {
walk_assoc_item(self, item, ctxt);
}
}
}
@@ -0,0 +1,24 @@
// Ensure assoc items work for decl macros.
// Regression test for <https://github.com/rust-lang/rust/issues/156075>.
//@ compile-flags: -Zunstable-options --generate-macro-expansion
#![crate_name = "foo"]
#![feature(decl_macro)]
//@ has 'src/foo/assoc-items-decl-macro.rs.html'
pub macro first() {
type P1 = bool;
fn u1() {}
}
trait C1 {
type P1;
fn u1();
}
impl C1 for u32 {
//@ matches - '//*[@class="expansion"]/*[@class="expanded"]' 'type P1 = bool;\nfn u1\(\) {}'
first!();
}
@@ -0,0 +1,25 @@
// Ensure assoc items work for macro rules.
// Regression test for <https://github.com/rust-lang/rust/issues/156075>.
//@ compile-flags: -Zunstable-options --generate-macro-expansion
#![crate_name = "foo"]
//@ has 'src/foo/assoc-items-macro.rs.html'
macro_rules! first {
() => {
type P1 = bool;
fn u1() {}
}
}
trait C1 {
type P1;
fn u1();
}
impl C1 for u32 {
//@ matches - '//*[@class="expansion"]/*[@class="expanded"]' 'type P1 = bool;\nfn u1\(\) {}'
first!();
}