From ad254f4c972b7a211fe2ebae4ecea47a79d02d9b Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 27 Jan 2021 19:16:29 +0100 Subject: [PATCH] Fix legacy macro resolution in block expressions --- crates/hir_def/src/nameres.rs | 11 +++++++++++ crates/hir_def/src/nameres/collector.rs | 5 ++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 199771e9a83..005b36e0288 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs @@ -289,6 +289,17 @@ pub(crate) fn resolve_path( (res.resolved_def, res.segment_index) } + /// Iterates over the containing `DefMap`s, if `self` is a `DefMap` corresponding to a block + /// expression. + fn ancestor_maps( + &self, + local_mod: LocalModuleId, + ) -> impl Iterator { + std::iter::successors(Some((self, local_mod)), |(map, _)| { + map.block.as_ref().map(|block| (&*block.parent, block.parent_module)) + }) + } + // FIXME: this can use some more human-readable format (ideally, an IR // even), as this should be a great debugging aid. pub fn dump(&self) -> String { diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 393170b32f1..761b29c868c 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -1443,7 +1443,10 @@ fn collect_macro_call(&mut self, mac: &MacroCall) { if let Some(macro_call_id) = ast_id.as_call_id(self.def_collector.db, self.def_collector.def_map.krate, |path| { path.as_ident().and_then(|name| { - self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name) + self.def_collector + .def_map + .ancestor_maps(self.module_id) + .find_map(|(map, module)| map[module].scope.get_legacy_macro(&name)) }) }) {