Skip to content

Commit 7ff1758

Browse files
authored
Unrolled build for #150221
Rollup merge of #150221 - el-ev:missing-par-ice-150154, r=fmease rustdoc: handle macro expansions in types - Closes #150154. - Closes #150197. This PR implements `visit_ty` in `ExpandedCodeVisitor` to correctly handle macro expansions in type positions (e.g., `let _: foo!();`). This also fixes a crash in rustdoc caused by the same issue. Before: <img width="419" height="182" alt="image" src="https://github.com/user-attachments/assets/89627230-032d-4ba1-af2e-8aa9a9cc6627" /> After: <img width="288" height="107" alt="image" src="https://github.com/user-attachments/assets/c06caeda-28b7-45b8-845a-e7fe784a82e4" />
2 parents a6525d5 + 74af408 commit 7ff1758

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

compiler/rustc_ast_pretty/src/pprust/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ pub fn where_bound_predicate_to_string(where_bound_predicate: &ast::WhereBoundPr
3333
State::new().where_bound_predicate_to_string(where_bound_predicate)
3434
}
3535

36+
/// # Panics
37+
///
38+
/// Panics if `pat.kind` is `PatKind::Missing`.
3639
pub fn pat_to_string(pat: &ast::Pat) -> String {
3740
State::new().pat_to_string(pat)
3841
}

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, 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, 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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
 (0)