Skip to content

Commit ccc3e42

Browse files
committed
rustdoc: handle macro expansions in types
1 parent b996a98 commit ccc3e42

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/librustdoc/html/macro_expansion.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use rustc_ast::visit::{Visitor, walk_crate, walk_expr, walk_item, walk_pat, walk_stmt};
2-
use rustc_ast::{Crate, Expr, Item, Pat, PatKind, Stmt};
1+
use rustc_ast::visit::{Visitor, walk_crate, walk_expr, walk_item, walk_pat, walk_stmt, walk_ty};
2+
use rustc_ast::{Crate, Expr, Item, Pat, PatKind, Stmt, Ty};
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_span::source_map::SourceMap;
55
use rustc_span::{BytePos, Span};
@@ -153,4 +153,12 @@ impl<'ast> Visitor<'ast> for ExpandedCodeVisitor<'ast> {
153153
walk_pat(self, pat);
154154
}
155155
}
156+
157+
fn visit_ty(&mut self, ty: &'ast Ty) {
158+
if ty.span.from_expansion() {
159+
self.handle_new_span(ty.span, || rustc_ast_pretty::pprust::ty_to_string(ty));
160+
} else {
161+
walk_ty(self, ty);
162+
}
163+
}
156164
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Ensure macro invocations at type position are expanded correctly
2+
3+
//@ compile-flags: -Zunstable-options --generate-macro-expansion
4+
5+
#![crate_name = "foo"]
6+
7+
//@ has 'src/foo/type-macro-expansion.rs.html'
8+
9+
macro_rules! foo {
10+
() => {
11+
fn(())
12+
};
13+
($_arg:expr) => {
14+
[(); 1]
15+
};
16+
}
17+
18+
fn bar() {
19+
//@ has - '//*[@class="expansion"]/*[@class="original"]/*[@class="macro"]' 'foo!'
20+
//@ has - '//*[@class="expansion"]/*[@class="original"]' 'foo!()'
21+
//@ has - '//*[@class="expansion"]/*[@class="expanded"]' 'fn(())'
22+
let _: foo!();
23+
//@ has - '//*[@class="expansion"]/*[@class="original"]/*[@class="macro"]' 'foo!'
24+
//@ has - '//*[@class="expansion"]/*[@class="original"]' 'foo!(42)'
25+
//@ has - '//*[@class="expansion"]/*[@class="expanded"]' '[(); 1]'
26+
let _: foo!(42);
27+
}
28+

0 commit comments

Comments
 (0)