From 6a3f2ce76ca06c5541b3bdd8c626abedbc812685 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Thu, 1 Apr 2021 03:45:21 +0800 Subject: Fix block inner item defined in macro --- crates/hir_def/src/item_tree/lower.rs | 6 ++++++ crates/hir_ty/src/tests/macros.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'crates') diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index 8d3862811..124dcc866 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs @@ -174,6 +174,12 @@ impl Ctx { let forced_vis = self.forced_visibility.take(); let mut block_stack = Vec::new(); + + // if container itself is block, add it to the stack + if let Some(block) = ast::BlockExpr::cast(container.clone()) { + block_stack.push(self.source_ast_id_map.ast_id(&block)); + } + for event in container.preorder().skip(1) { match event { WalkEvent::Enter(node) => { diff --git a/crates/hir_ty/src/tests/macros.rs b/crates/hir_ty/src/tests/macros.rs index 3eb01dbd0..86e3d8b86 100644 --- a/crates/hir_ty/src/tests/macros.rs +++ b/crates/hir_ty/src/tests/macros.rs @@ -372,6 +372,32 @@ fn recursive_inner_item_macro_rules() { ); } +#[test] +fn infer_macro_defining_block_with_items() { + check_infer( + r#" + macro_rules! foo { + () => {{ + fn bar() -> usize { 0 } + bar() + }}; + } + fn main() { + let _a = foo!(); + } + "#, + expect![[r#" + !15..18 '{0}': usize + !16..17 '0': usize + !0..24 '{fnbar...bar()}': usize + !18..21 'bar': fn bar() -> usize + !18..23 'bar()': usize + 98..122 '{ ...!(); }': () + 108..110 '_a': usize + "#]], + ); +} + #[test] fn infer_type_value_macro_having_same_name() { check_infer( -- cgit v1.2.3