From b777d46ae61fce3c3a891eeda5b5d7c91fda3871 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 19 Apr 2021 01:06:04 +0200 Subject: Fix visibility of items in block modules --- crates/hir_def/src/lib.rs | 12 ++++++++++++ crates/hir_def/src/visibility.rs | 10 +++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index 000567d99..5ac1670b5 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -108,6 +108,18 @@ impl ModuleId { pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option { self.def_map(db).containing_module(self.local_id) } + + /// Returns `true` if this module represents a block expression. + /// + /// Returns `false` if this module is a submodule *inside* a block expression + /// (eg. `m` in `{ mod m {} }`). + pub fn is_block_root(&self, db: &dyn db::DefDatabase) -> bool { + if self.block.is_none() { + return false; + } + + self.def_map(db)[self.local_id].parent.is_none() + } } /// An ID of a module, **local** to a specific crate diff --git a/crates/hir_def/src/visibility.rs b/crates/hir_def/src/visibility.rs index 9908cd926..d4b7c9970 100644 --- a/crates/hir_def/src/visibility.rs +++ b/crates/hir_def/src/visibility.rs @@ -123,11 +123,19 @@ impl Visibility { def_map: &DefMap, mut from_module: crate::LocalModuleId, ) -> bool { - let to_module = match self { + let mut to_module = match self { Visibility::Module(m) => m, Visibility::Public => return true, }; + // `to_module` might be the root module of a block expression. Those have the same + // visibility as the containing module (even though no items are directly nameable from + // there, getting this right is important for method resolution). + // In that case, we adjust the visibility of `to_module` to point to the containing module. + if to_module.is_block_root(db) { + to_module = to_module.containing_module(db).unwrap(); + } + // from_module needs to be a descendant of to_module let mut def_map = def_map; let mut parent_arc; -- cgit v1.2.3