rustdoc: handle macro expansions in types

This commit is contained in:
Iris Shi 2025-12-21 18:24:44 +08:00
parent cb79c42008
commit 74af408790
No known key found for this signature in database
GPG key ID: 598500EEDEADBEEF
3 changed files with 40 additions and 2 deletions

View file

@ -33,6 +33,9 @@ pub fn where_bound_predicate_to_string(where_bound_predicate: &ast::WhereBoundPr
State::new().where_bound_predicate_to_string(where_bound_predicate)
}
/// # Panics
///
/// Panics if `pat.kind` is `PatKind::Missing`.
pub fn pat_to_string(pat: &ast::Pat) -> String {
State::new().pat_to_string(pat)
}

View file

@ -1,5 +1,5 @@
use rustc_ast::visit::{Visitor, walk_crate, walk_expr, walk_item, walk_pat, walk_stmt};
use rustc_ast::{Crate, Expr, Item, Pat, Stmt};
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_data_structures::fx::FxHashMap;
use rustc_span::source_map::SourceMap;
use rustc_span::{BytePos, Span};
@ -153,4 +153,12 @@ impl<'ast> Visitor<'ast> for ExpandedCodeVisitor<'ast> {
walk_pat(self, pat);
}
}
fn visit_ty(&mut self, ty: &'ast Ty) {
if ty.span.from_expansion() {
self.handle_new_span(ty.span, || rustc_ast_pretty::pprust::ty_to_string(ty));
} else {
walk_ty(self, ty);
}
}
}

View file

@ -0,0 +1,27 @@
// Ensure macro invocations at type position are expanded correctly
//@ compile-flags: -Zunstable-options --generate-macro-expansion
#![crate_name = "foo"]
//@ has 'src/foo/type-macro-expansion.rs.html'
macro_rules! foo {
() => {
fn(())
};
($_arg:expr) => {
[(); 1]
};
}
fn bar() {
//@ has - '//*[@class="expansion"]/*[@class="original"]/*[@class="macro"]' 'foo!'
//@ has - '//*[@class="expansion"]/*[@class="original"]' 'foo!()'
//@ has - '//*[@class="expansion"]/*[@class="expanded"]' 'fn(())'
let _: foo!();
//@ has - '//*[@class="expansion"]/*[@class="original"]/*[@class="macro"]' 'foo!'
//@ has - '//*[@class="expansion"]/*[@class="original"]' 'foo!(42)'
//@ has - '//*[@class="expansion"]/*[@class="expanded"]' '[(); 1]'
let _: foo!(42);
}