about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-08-01 16:57:39 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2025-08-23 00:57:29 +0200
commit0e9b126695847418a681b96b87b5976fb069394a (patch)
treea162b2114f5f63c4210d2a0737fb99060bfc1871
parentbd5f3d76789028e0cdb432ff20f744b6fb24efc2 (diff)
downloadrust-0e9b126695847418a681b96b87b5976fb069394a.tar.gz
rust-0e9b126695847418a681b96b87b5976fb069394a.zip
Also support statements and patterns for macro expansion
-rw-r--r--src/librustdoc/html/macro_expansion.rs20
-rw-r--r--tests/rustdoc/macro/macro_expansion.rs28
2 files changed, 46 insertions, 2 deletions
diff --git a/src/librustdoc/html/macro_expansion.rs b/src/librustdoc/html/macro_expansion.rs
index 7b6758868b7..d76663a3d1a 100644
--- a/src/librustdoc/html/macro_expansion.rs
+++ b/src/librustdoc/html/macro_expansion.rs
@@ -1,5 +1,5 @@
-use rustc_ast::visit::{Visitor, walk_crate, walk_expr, walk_item};
-use rustc_ast::{Crate, Expr, Item};
+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_data_structures::fx::FxHashMap;
 use rustc_span::source_map::SourceMap;
 use rustc_span::{BytePos, Span};
@@ -137,4 +137,20 @@ impl<'ast> Visitor<'ast> for ExpandedCodeVisitor<'ast> {
             walk_item(self, item);
         }
     }
+
+    fn visit_stmt(&mut self, stmt: &'ast Stmt) {
+        if stmt.span.from_expansion() {
+            self.handle_new_span(stmt.span, || rustc_ast_pretty::pprust::stmt_to_string(stmt));
+        } else {
+            walk_stmt(self, stmt);
+        }
+    }
+
+    fn visit_pat(&mut self, pat: &'ast Pat) {
+        if pat.span.from_expansion() {
+            self.handle_new_span(pat.span, || rustc_ast_pretty::pprust::pat_to_string(pat));
+        } else {
+            walk_pat(self, pat);
+        }
+    }
 }
diff --git a/tests/rustdoc/macro/macro_expansion.rs b/tests/rustdoc/macro/macro_expansion.rs
new file mode 100644
index 00000000000..c989ccad967
--- /dev/null
+++ b/tests/rustdoc/macro/macro_expansion.rs
@@ -0,0 +1,28 @@
+// This test checks that patterns and statements are also getting expanded.
+
+//@ compile-flags: -Zunstable-options --generate-macro-expansion
+
+#![crate_name = "foo"]
+
+//@ has 'src/foo/macro_expansion.rs.html'
+//@ count - '//span[@class="expansion"]' 2
+
+macro_rules! pat {
+    ($x:literal) => {
+        Some($x)
+    }
+}
+
+macro_rules! stmt {
+    ($x:expr) => {{
+        let _ = $x;
+    }}
+}
+
+fn bar() {
+    match Some("hello") {
+        pat!("blolb") => {}
+        _ => {}
+    }
+    stmt!(1)
+}