Skip to content

Commit 0e9b126

Browse files
Also support statements and patterns for macro expansion
1 parent bd5f3d7 commit 0e9b126

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/librustdoc/html/macro_expansion.rs

Lines changed: 18 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};
2-
use rustc_ast::{Crate, Expr, Item};
1+
use rustc_ast::visit::{Visitor, walk_crate, walk_expr, walk_item, walk_pat, walk_stmt};
2+
use rustc_ast::{Crate, Expr, Item, Pat, Stmt};
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_span::source_map::SourceMap;
55
use rustc_span::{BytePos, Span};
@@ -137,4 +137,20 @@ impl<'ast> Visitor<'ast> for ExpandedCodeVisitor<'ast> {
137137
walk_item(self, item);
138138
}
139139
}
140+
141+
fn visit_stmt(&mut self, stmt: &'ast Stmt) {
142+
if stmt.span.from_expansion() {
143+
self.handle_new_span(stmt.span, || rustc_ast_pretty::pprust::stmt_to_string(stmt));
144+
} else {
145+
walk_stmt(self, stmt);
146+
}
147+
}
148+
149+
fn visit_pat(&mut self, pat: &'ast Pat) {
150+
if pat.span.from_expansion() {
151+
self.handle_new_span(pat.span, || rustc_ast_pretty::pprust::pat_to_string(pat));
152+
} else {
153+
walk_pat(self, pat);
154+
}
155+
}
140156
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This test checks that patterns and statements are also getting expanded.
2+
3+
//@ compile-flags: -Zunstable-options --generate-macro-expansion
4+
5+
#![crate_name = "foo"]
6+
7+
//@ has 'src/foo/macro_expansion.rs.html'
8+
//@ count - '//span[@class="expansion"]' 2
9+
10+
macro_rules! pat {
11+
($x:literal) => {
12+
Some($x)
13+
}
14+
}
15+
16+
macro_rules! stmt {
17+
($x:expr) => {{
18+
let _ = $x;
19+
}}
20+
}
21+
22+
fn bar() {
23+
match Some("hello") {
24+
pat!("blolb") => {}
25+
_ => {}
26+
}
27+
stmt!(1)
28+
}

0 commit comments

Comments
 (0)