Skip to content

Commit d3da042

Browse files
Merge #8082
8082: Proper handle inner recursive macro rules cases r=edwin0cheng a=edwin0cheng Fixes #7645 cc @jonas-schievink bors r+ Co-authored-by: Edwin Cheng <[email protected]>
2 parents 80d497e + 13f30e9 commit d3da042

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

crates/hir_def/src/item_tree.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ impl ItemTree {
115115
// still need to collect inner items.
116116
ctx.lower_inner_items(stmt.syntax())
117117
},
118+
ast::Item(item) => {
119+
// Macros can expand to stmt and other item, and we add it as top level item
120+
ctx.lower_single_item(item)
121+
},
118122
_ => {
119123
panic!("cannot create item tree from {:?} {}", syntax, syntax);
120124
},

crates/hir_def/src/item_tree/lower.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ impl Ctx {
8787
self.tree
8888
}
8989

90+
pub(super) fn lower_single_item(mut self, item: ast::Item) -> ItemTree {
91+
self.tree.top_level = self
92+
.lower_mod_item(&item, false)
93+
.map(|item| item.0)
94+
.unwrap_or_else(|| Default::default());
95+
self.tree
96+
}
97+
9098
pub(super) fn lower_inner_items(mut self, within: &SyntaxNode) -> ItemTree {
9199
self.collect_inner_items(within);
92100
self.tree

crates/hir_ty/src/tests/macros.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,28 @@ fn expr_macro_expanded_in_stmts() {
231231
);
232232
}
233233

234+
#[test]
235+
fn recursive_inner_item_macro_rules() {
236+
check_infer(
237+
r#"
238+
macro_rules! mac {
239+
() => { mac!($)};
240+
($x:tt) => { macro_rules! blub { () => { 1 }; } };
241+
}
242+
fn foo() {
243+
mac!();
244+
let a = blub!();
245+
}
246+
"#,
247+
expect![[r#"
248+
!0..1 '1': i32
249+
!0..7 'mac!($)': {unknown}
250+
107..143 '{ ...!(); }': ()
251+
129..130 'a': i32
252+
"#]],
253+
);
254+
}
255+
234256
#[test]
235257
fn infer_type_value_macro_having_same_name() {
236258
check_infer(

0 commit comments

Comments
 (0)