From 338823f73aefbf7957b928ad76fc5f55cc43df9c Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 23 Feb 2021 17:56:16 +0100 Subject: is_visible_from_def_map: handle block expressions --- crates/hir_def/src/visibility.rs | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) (limited to 'crates/hir_def/src/visibility.rs') diff --git a/crates/hir_def/src/visibility.rs b/crates/hir_def/src/visibility.rs index 38da3132b..0e3951910 100644 --- a/crates/hir_def/src/visibility.rs +++ b/crates/hir_def/src/visibility.rs @@ -103,7 +103,7 @@ impl Visibility { return false; } let def_map = from_module.def_map(db); - self.is_visible_from_def_map(&def_map, from_module.local_id) + self.is_visible_from_def_map(db, &def_map, from_module.local_id) } pub(crate) fn is_visible_from_other_crate(self) -> bool { @@ -115,19 +115,41 @@ impl Visibility { pub(crate) fn is_visible_from_def_map( self, + db: &dyn DefDatabase, def_map: &DefMap, - from_module: crate::LocalModuleId, + mut from_module: crate::LocalModuleId, ) -> bool { let to_module = match self { Visibility::Module(m) => m, Visibility::Public => return true, }; + // from_module needs to be a descendant of to_module - let mut ancestors = std::iter::successors(Some(from_module), |m| { - let parent_id = def_map[*m].parent?; - Some(parent_id) - }); - ancestors.any(|m| m == to_module.local_id) + let mut def_map = def_map; + let mut parent_arc; + loop { + if def_map.module_id(from_module) == to_module { + return true; + } + match def_map[from_module].parent { + Some(parent) => { + from_module = parent; + } + None => { + match def_map.parent() { + Some(module) => { + parent_arc = module.def_map(db); + def_map = &*parent_arc; + from_module = module.local_id; + } + None => { + // Reached the root module, nothing left to check. + return false; + } + } + } + } + } } /// Returns the most permissive visibility of `self` and `other`. -- cgit v1.2.3