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)) => {
let item_tree = db.file_item_tree(file_id.into());
let krate = self.def_collector.def_map.krate;
let is_enabled = item_tree
.top_level_attrs(db, self.def_collector.def_map.krate)
.top_level_attrs(db, krate)
.cfg()
.map_or(true, |cfg| self.is_cfg_enabled(&cfg));
if is_enabled {
@ -1713,7 +1714,7 @@ impl ModCollector<'_, '_> {
&self.item_tree[module.visibility],
);
ModCollector {
def_collector: &mut *self.def_collector,
def_collector: self.def_collector,
macro_depth: self.macro_depth,
module_id,
tree_id: TreeId::new(file_id.into(), None),
@ -1723,7 +1724,7 @@ impl ModCollector<'_, '_> {
.collect_in_top_module(item_tree.top_level_items());
let is_macro_use = is_macro_use
|| item_tree
.top_level_attrs(db, self.def_collector.def_map.krate)
.top_level_attrs(db, krate)
.by_key("macro_use")
.exists();
if is_macro_use {
@ -1748,12 +1749,11 @@ impl ModCollector<'_, '_> {
definition: Option<(FileId, bool)>,
visibility: &crate::visibility::RawVisibility,
) -> LocalModuleId {
let vis = self
.def_collector
.def_map
let def_map = &mut self.def_collector.def_map;
let vis = def_map
.resolve_visibility(self.def_collector.db, self.module_id, visibility)
.unwrap_or(Visibility::Public);
let modules = &mut self.def_collector.def_map.modules;
let modules = &mut def_map.modules;
let origin = match definition {
None => ModuleOrigin::Inline { definition: declaration },
Some((definition, is_mod_rs)) => {
@ -1768,10 +1768,10 @@ impl ModCollector<'_, '_> {
}
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);
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.module_id,
&[(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)`).
use std::sync::Arc;
use std::{iter, sync::Arc};
use hir_expand::{hygiene::Hygiene, InFile};
use la_arena::ArenaMap;
@ -25,7 +25,7 @@ pub enum RawVisibility {
}
impl RawVisibility {
pub(crate) fn private() -> RawVisibility {
pub(crate) const fn private() -> RawVisibility {
RawVisibility::Module(ModPath::from_kind(PathKind::Super(0)))
}
@ -113,10 +113,7 @@ impl Visibility {
}
pub(crate) fn is_visible_from_other_crate(self) -> bool {
match self {
Visibility::Module(_) => false,
Visibility::Public => true,
}
matches!(self, Visibility::Public)
}
pub(crate) fn is_visible_from_def_map(
@ -145,10 +142,7 @@ impl Visibility {
arc = to_module.def_map(db);
&arc
};
let is_block_root = match to_module.block {
Some(_) => to_module_def_map[to_module.local_id].parent.is_none(),
None => false,
};
let is_block_root = matches!(to_module.block, Some(_) if to_module_def_map[to_module.local_id].parent.is_none());
if is_block_root {
to_module = to_module_def_map.containing_module(to_module.local_id).unwrap();
}
@ -161,9 +155,7 @@ impl Visibility {
return true;
}
match def_map[from_module].parent {
Some(parent) => {
from_module = parent;
}
Some(parent) => from_module = parent,
None => {
match def_map.parent() {
Some(module) => {
@ -171,10 +163,8 @@ impl Visibility {
def_map = &*parent_arc;
from_module = module.local_id;
}
None => {
// Reached the root module, nothing left to check.
return false;
}
// Reached the root module, nothing left to check.
None => return false,
}
}
}
@ -194,12 +184,12 @@ impl Visibility {
return None;
}
let mut a_ancestors = std::iter::successors(Some(mod_a.local_id), |m| {
let parent_id = def_map[*m].parent?;
let mut a_ancestors = iter::successors(Some(mod_a.local_id), |&m| {
let parent_id = def_map[m].parent?;
Some(parent_id)
});
let mut b_ancestors = std::iter::successors(Some(mod_b.local_id), |m| {
let parent_id = def_map[*m].parent?;
let mut b_ancestors = iter::successors(Some(mod_b.local_id), |&m| {
let parent_id = def_map[m].parent?;
Some(parent_id)
});