check if the container is trait and inherit the visibility

This commit is contained in:
Hongxu Xu 2022-06-15 07:47:06 +08:00
parent 070456838d
commit 8805a768d4
3 changed files with 33 additions and 70 deletions

View file

@ -15,8 +15,8 @@ use crate::{
type_ref::{TraitRef, TypeBound, TypeRef}, type_ref::{TraitRef, TypeBound, TypeRef},
visibility::RawVisibility, visibility::RawVisibility,
AssocItemId, AstIdWithPath, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId, AssocItemId, AstIdWithPath, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
InheritedVisibilityLoc, Intern, ItemContainerId, Lookup, Macro2Id, MacroRulesId, ModuleId, Intern, ItemContainerId, Lookup, Macro2Id, MacroRulesId, ModuleId, ProcMacroId, StaticId,
ProcMacroId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, TraitId, TypeAliasId, TypeAliasLoc,
}; };
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -40,9 +40,8 @@ impl FunctionData {
let cfg_options = &crate_graph[krate].cfg_options; let cfg_options = &crate_graph[krate].cfg_options;
let item_tree = loc.id.item_tree(db); let item_tree = loc.id.item_tree(db);
let func = &item_tree[loc.id.value]; let func = &item_tree[loc.id.value];
let visibility = if let ItemContainerId::TraitId(trait_id) = loc.container {
let visibility = if let Some(inherited_vis) = loc.inherited_visibility { db.trait_data(trait_id).visibility.clone()
inherited_vis.tree_id.item_tree(db)[inherited_vis.raw_visibility_id].clone()
} else { } else {
item_tree[func.visibility].clone() item_tree[func.visibility].clone()
}; };
@ -177,8 +176,8 @@ impl TypeAliasData {
let loc = typ.lookup(db); let loc = typ.lookup(db);
let item_tree = loc.id.item_tree(db); let item_tree = loc.id.item_tree(db);
let typ = &item_tree[loc.id.value]; let typ = &item_tree[loc.id.value];
let visibility = if let Some(inherited_vis) = loc.inherited_visibility { let visibility = if let ItemContainerId::TraitId(trait_id) = loc.container {
inherited_vis.tree_id.item_tree(db)[inherited_vis.raw_visibility_id].clone() db.trait_data(trait_id).visibility.clone()
} else { } else {
item_tree[typ.visibility].clone() item_tree[typ.visibility].clone()
}; };
@ -232,7 +231,6 @@ impl TraitData {
module_id, module_id,
tr_loc.id.file_id(), tr_loc.id.file_id(),
ItemContainerId::TraitId(tr), ItemContainerId::TraitId(tr),
Some(InheritedVisibilityLoc::new(tr_def.visibility, tr_loc.id.tree_id())),
); );
collector.collect(tr_loc.id.tree_id(), &tr_def.items); collector.collect(tr_loc.id.tree_id(), &tr_def.items);
@ -300,7 +298,6 @@ impl ImplData {
module_id, module_id,
impl_loc.id.file_id(), impl_loc.id.file_id(),
ItemContainerId::ImplId(id), ItemContainerId::ImplId(id),
None,
); );
collector.collect(impl_loc.id.tree_id(), &impl_def.items); collector.collect(impl_loc.id.tree_id(), &impl_def.items);
@ -398,8 +395,8 @@ impl ConstData {
let loc = konst.lookup(db); let loc = konst.lookup(db);
let item_tree = loc.id.item_tree(db); let item_tree = loc.id.item_tree(db);
let konst = &item_tree[loc.id.value]; let konst = &item_tree[loc.id.value];
let visibility = if let Some(inherited_vis) = loc.inherited_visibility { let visibility = if let ItemContainerId::TraitId(trait_id) = loc.container {
inherited_vis.tree_id.item_tree(db)[inherited_vis.raw_visibility_id].clone() db.trait_data(trait_id).visibility.clone()
} else { } else {
item_tree[konst.visibility].clone() item_tree[konst.visibility].clone()
}; };
@ -446,8 +443,6 @@ struct AssocItemCollector<'a> {
items: Vec<(Name, AssocItemId)>, items: Vec<(Name, AssocItemId)>,
attr_calls: Vec<(AstId<ast::Item>, MacroCallId)>, attr_calls: Vec<(AstId<ast::Item>, MacroCallId)>,
inherited_visibility: Option<InheritedVisibilityLoc>,
} }
impl<'a> AssocItemCollector<'a> { impl<'a> AssocItemCollector<'a> {
@ -456,7 +451,6 @@ impl<'a> AssocItemCollector<'a> {
module_id: ModuleId, module_id: ModuleId,
file_id: HirFileId, file_id: HirFileId,
container: ItemContainerId, container: ItemContainerId,
inherited_visibility: Option<InheritedVisibilityLoc>,
) -> Self { ) -> Self {
Self { Self {
db, db,
@ -467,8 +461,6 @@ impl<'a> AssocItemCollector<'a> {
items: Vec::new(), items: Vec::new(),
attr_calls: Vec::new(), attr_calls: Vec::new(),
inherited_visibility,
} }
} }
@ -511,11 +503,8 @@ impl<'a> AssocItemCollector<'a> {
match item { match item {
AssocItem::Function(id) => { AssocItem::Function(id) => {
let item = &item_tree[id]; let item = &item_tree[id];
let def = FunctionLoc { let def =
container: self.container, FunctionLoc { container: self.container, id: ItemTreeId::new(tree_id, id) }
id: ItemTreeId::new(tree_id, id),
inherited_visibility: self.inherited_visibility,
}
.intern(self.db); .intern(self.db);
self.items.push((item.name.clone(), def.into())); self.items.push((item.name.clone(), def.into()));
} }
@ -525,11 +514,8 @@ impl<'a> AssocItemCollector<'a> {
Some(name) => name, Some(name) => name,
None => continue, None => continue,
}; };
let def = ConstLoc { let def =
container: self.container, ConstLoc { container: self.container, id: ItemTreeId::new(tree_id, id) }
id: ItemTreeId::new(tree_id, id),
inherited_visibility: self.inherited_visibility,
}
.intern(self.db); .intern(self.db);
self.items.push((name, def.into())); self.items.push((name, def.into()));
} }
@ -538,7 +524,6 @@ impl<'a> AssocItemCollector<'a> {
let def = TypeAliasLoc { let def = TypeAliasLoc {
container: self.container, container: self.container,
id: ItemTreeId::new(tree_id, id), id: ItemTreeId::new(tree_id, id),
inherited_visibility: self.inherited_visibility,
} }
.intern(self.db); .intern(self.db);
self.items.push((item.name.clone(), def.into())); self.items.push((item.name.clone(), def.into()));

View file

@ -70,7 +70,7 @@ use hir_expand::{
AstId, ExpandError, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, AstId, ExpandError, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId,
MacroDefKind, UnresolvedMacro, MacroDefKind, UnresolvedMacro,
}; };
use item_tree::{ExternBlock, RawVisibilityId, TreeId}; use item_tree::ExternBlock;
use la_arena::Idx; use la_arena::Idx;
use nameres::DefMap; use nameres::DefMap;
use stdx::impl_from; use stdx::impl_from;
@ -156,25 +156,19 @@ impl<N: ItemTreeNode> Hash for ItemLoc<N> {
} }
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug)]
pub struct InheritedVisibilityLoc {
pub raw_visibility_id: RawVisibilityId,
pub tree_id: TreeId,
}
impl InheritedVisibilityLoc {
pub fn new(visibility_id: RawVisibilityId, tree_id: TreeId) -> Self {
Self { raw_visibility_id: visibility_id, tree_id }
}
}
#[derive(Debug, Clone, Copy)]
pub struct AssocItemLoc<N: ItemTreeNode> { pub struct AssocItemLoc<N: ItemTreeNode> {
pub container: ItemContainerId, pub container: ItemContainerId,
pub id: ItemTreeId<N>, pub id: ItemTreeId<N>,
pub inherited_visibility: Option<InheritedVisibilityLoc>,
} }
impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
fn clone(&self) -> Self {
Self { container: self.container, id: self.id }
}
}
impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> { impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.container == other.container && self.id == other.id self.container == other.container && self.id == other.id

View file

@ -1549,12 +1549,8 @@ impl ModCollector<'_, '_> {
} }
ModItem::Function(id) => { ModItem::Function(id) => {
let it = &self.item_tree[id]; let it = &self.item_tree[id];
let fn_id = FunctionLoc { let fn_id =
container, FunctionLoc { container, id: ItemTreeId::new(self.tree_id, id) }.intern(db);
id: ItemTreeId::new(self.tree_id, id),
inherited_visibility: None,
}
.intern(db);
let vis = resolve_vis(def_map, &self.item_tree[it.visibility]); let vis = resolve_vis(def_map, &self.item_tree[it.visibility]);
if self.def_collector.is_proc_macro { if self.def_collector.is_proc_macro {
@ -1617,12 +1613,8 @@ impl ModCollector<'_, '_> {
} }
ModItem::Const(id) => { ModItem::Const(id) => {
let it = &self.item_tree[id]; let it = &self.item_tree[id];
let const_id = ConstLoc { let const_id =
container, ConstLoc { container, id: ItemTreeId::new(self.tree_id, id) }.intern(db);
id: ItemTreeId::new(self.tree_id, id),
inherited_visibility: None,
}
.intern(db);
match &it.name { match &it.name {
Some(name) => { Some(name) => {
@ -1643,11 +1635,7 @@ impl ModCollector<'_, '_> {
let vis = resolve_vis(def_map, &self.item_tree[it.visibility]); let vis = resolve_vis(def_map, &self.item_tree[it.visibility]);
update_def( update_def(
self.def_collector, self.def_collector,
StaticLoc { StaticLoc { container, id: ItemTreeId::new(self.tree_id, id) }
container,
id: ItemTreeId::new(self.tree_id, id),
inherited_visibility: None,
}
.intern(db) .intern(db)
.into(), .into(),
&it.name, &it.name,
@ -1675,11 +1663,7 @@ impl ModCollector<'_, '_> {
let vis = resolve_vis(def_map, &self.item_tree[it.visibility]); let vis = resolve_vis(def_map, &self.item_tree[it.visibility]);
update_def( update_def(
self.def_collector, self.def_collector,
TypeAliasLoc { TypeAliasLoc { container, id: ItemTreeId::new(self.tree_id, id) }
container,
id: ItemTreeId::new(self.tree_id, id),
inherited_visibility: None,
}
.intern(db) .intern(db)
.into(), .into(),
&it.name, &it.name,