This commit is contained in:
Lukas Wirth 2022-02-23 15:55:06 +01:00
parent 789f2b9cb6
commit ffeec9dec9
2 changed files with 20 additions and 30 deletions

View File

@ -1701,8 +1701,9 @@ impl ModCollector<'_, '_> {
{ {
Ok((file_id, is_mod_rs, mod_dir)) => { Ok((file_id, is_mod_rs, mod_dir)) => {
let item_tree = db.file_item_tree(file_id.into()); let item_tree = db.file_item_tree(file_id.into());
let krate = self.def_collector.def_map.krate;
let is_enabled = item_tree let is_enabled = item_tree
.top_level_attrs(db, self.def_collector.def_map.krate) .top_level_attrs(db, krate)
.cfg() .cfg()
.map_or(true, |cfg| self.is_cfg_enabled(&cfg)); .map_or(true, |cfg| self.is_cfg_enabled(&cfg));
if is_enabled { if is_enabled {
@ -1713,7 +1714,7 @@ impl ModCollector<'_, '_> {
&self.item_tree[module.visibility], &self.item_tree[module.visibility],
); );
ModCollector { ModCollector {
def_collector: &mut *self.def_collector, def_collector: self.def_collector,
macro_depth: self.macro_depth, macro_depth: self.macro_depth,
module_id, module_id,
tree_id: TreeId::new(file_id.into(), None), tree_id: TreeId::new(file_id.into(), None),
@ -1723,7 +1724,7 @@ impl ModCollector<'_, '_> {
.collect_in_top_module(item_tree.top_level_items()); .collect_in_top_module(item_tree.top_level_items());
let is_macro_use = is_macro_use let is_macro_use = is_macro_use
|| item_tree || item_tree
.top_level_attrs(db, self.def_collector.def_map.krate) .top_level_attrs(db, krate)
.by_key("macro_use") .by_key("macro_use")
.exists(); .exists();
if is_macro_use { if is_macro_use {
@ -1748,12 +1749,11 @@ impl ModCollector<'_, '_> {
definition: Option<(FileId, bool)>, definition: Option<(FileId, bool)>,
visibility: &crate::visibility::RawVisibility, visibility: &crate::visibility::RawVisibility,
) -> LocalModuleId { ) -> LocalModuleId {
let vis = self let def_map = &mut self.def_collector.def_map;
.def_collector let vis = def_map
.def_map
.resolve_visibility(self.def_collector.db, self.module_id, visibility) .resolve_visibility(self.def_collector.db, self.module_id, visibility)
.unwrap_or(Visibility::Public); .unwrap_or(Visibility::Public);
let modules = &mut self.def_collector.def_map.modules; let modules = &mut def_map.modules;
let origin = match definition { let origin = match definition {
None => ModuleOrigin::Inline { definition: declaration }, None => ModuleOrigin::Inline { definition: declaration },
Some((definition, is_mod_rs)) => { Some((definition, is_mod_rs)) => {
@ -1768,10 +1768,10 @@ impl ModCollector<'_, '_> {
} }
modules[self.module_id].children.insert(name.clone(), res); modules[self.module_id].children.insert(name.clone(), res);
let module = self.def_collector.def_map.module_id(res); let module = def_map.module_id(res);
let def = ModuleDefId::from(module); let def = ModuleDefId::from(module);
self.def_collector.def_map.modules[self.module_id].scope.declare(def); def_map.modules[self.module_id].scope.declare(def);
self.def_collector.update( self.def_collector.update(
self.module_id, self.module_id,
&[(Some(name), PerNs::from_def(def, vis, false))], &[(Some(name), PerNs::from_def(def, vis, false))],

View File

@ -1,6 +1,6 @@
//! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`). //! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`).
use std::sync::Arc; use std::{iter, sync::Arc};
use hir_expand::{hygiene::Hygiene, InFile}; use hir_expand::{hygiene::Hygiene, InFile};
use la_arena::ArenaMap; use la_arena::ArenaMap;
@ -25,7 +25,7 @@ pub enum RawVisibility {
} }
impl RawVisibility { impl RawVisibility {
pub(crate) fn private() -> RawVisibility { pub(crate) const fn private() -> RawVisibility {
RawVisibility::Module(ModPath::from_kind(PathKind::Super(0))) RawVisibility::Module(ModPath::from_kind(PathKind::Super(0)))
} }
@ -113,10 +113,7 @@ impl Visibility {
} }
pub(crate) fn is_visible_from_other_crate(self) -> bool { pub(crate) fn is_visible_from_other_crate(self) -> bool {
match self { matches!(self, Visibility::Public)
Visibility::Module(_) => false,
Visibility::Public => true,
}
} }
pub(crate) fn is_visible_from_def_map( pub(crate) fn is_visible_from_def_map(
@ -145,10 +142,7 @@ impl Visibility {
arc = to_module.def_map(db); arc = to_module.def_map(db);
&arc &arc
}; };
let is_block_root = match to_module.block { let is_block_root = matches!(to_module.block, Some(_) if to_module_def_map[to_module.local_id].parent.is_none());
Some(_) => to_module_def_map[to_module.local_id].parent.is_none(),
None => false,
};
if is_block_root { if is_block_root {
to_module = to_module_def_map.containing_module(to_module.local_id).unwrap(); to_module = to_module_def_map.containing_module(to_module.local_id).unwrap();
} }
@ -161,9 +155,7 @@ impl Visibility {
return true; return true;
} }
match def_map[from_module].parent { match def_map[from_module].parent {
Some(parent) => { Some(parent) => from_module = parent,
from_module = parent;
}
None => { None => {
match def_map.parent() { match def_map.parent() {
Some(module) => { Some(module) => {
@ -171,10 +163,8 @@ impl Visibility {
def_map = &*parent_arc; def_map = &*parent_arc;
from_module = module.local_id; from_module = module.local_id;
} }
None => { // Reached the root module, nothing left to check.
// Reached the root module, nothing left to check. None => return false,
return false;
}
} }
} }
} }
@ -194,12 +184,12 @@ impl Visibility {
return None; return None;
} }
let mut a_ancestors = std::iter::successors(Some(mod_a.local_id), |m| { let mut a_ancestors = iter::successors(Some(mod_a.local_id), |&m| {
let parent_id = def_map[*m].parent?; let parent_id = def_map[m].parent?;
Some(parent_id) Some(parent_id)
}); });
let mut b_ancestors = std::iter::successors(Some(mod_b.local_id), |m| { let mut b_ancestors = iter::successors(Some(mod_b.local_id), |&m| {
let parent_id = def_map[*m].parent?; let parent_id = def_map[m].parent?;
Some(parent_id) Some(parent_id)
}); });