Skip to content

Commit 8805a76

Browse files
committed
check if the container is trait and inherit the visibility
1 parent 0704568 commit 8805a76

File tree

3 files changed

+33
-70
lines changed

3 files changed

+33
-70
lines changed

crates/hir-def/src/data.rs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use crate::{
1515
type_ref::{TraitRef, TypeBound, TypeRef},
1616
visibility::RawVisibility,
1717
AssocItemId, AstIdWithPath, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
18-
InheritedVisibilityLoc, Intern, ItemContainerId, Lookup, Macro2Id, MacroRulesId, ModuleId,
19-
ProcMacroId, StaticId, TraitId, TypeAliasId, TypeAliasLoc,
18+
Intern, ItemContainerId, Lookup, Macro2Id, MacroRulesId, ModuleId, ProcMacroId, StaticId,
19+
TraitId, TypeAliasId, TypeAliasLoc,
2020
};
2121

2222
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -40,9 +40,8 @@ impl FunctionData {
4040
let cfg_options = &crate_graph[krate].cfg_options;
4141
let item_tree = loc.id.item_tree(db);
4242
let func = &item_tree[loc.id.value];
43-
44-
let visibility = if let Some(inherited_vis) = loc.inherited_visibility {
45-
inherited_vis.tree_id.item_tree(db)[inherited_vis.raw_visibility_id].clone()
43+
let visibility = if let ItemContainerId::TraitId(trait_id) = loc.container {
44+
db.trait_data(trait_id).visibility.clone()
4645
} else {
4746
item_tree[func.visibility].clone()
4847
};
@@ -177,8 +176,8 @@ impl TypeAliasData {
177176
let loc = typ.lookup(db);
178177
let item_tree = loc.id.item_tree(db);
179178
let typ = &item_tree[loc.id.value];
180-
let visibility = if let Some(inherited_vis) = loc.inherited_visibility {
181-
inherited_vis.tree_id.item_tree(db)[inherited_vis.raw_visibility_id].clone()
179+
let visibility = if let ItemContainerId::TraitId(trait_id) = loc.container {
180+
db.trait_data(trait_id).visibility.clone()
182181
} else {
183182
item_tree[typ.visibility].clone()
184183
};
@@ -232,7 +231,6 @@ impl TraitData {
232231
module_id,
233232
tr_loc.id.file_id(),
234233
ItemContainerId::TraitId(tr),
235-
Some(InheritedVisibilityLoc::new(tr_def.visibility, tr_loc.id.tree_id())),
236234
);
237235
collector.collect(tr_loc.id.tree_id(), &tr_def.items);
238236

@@ -300,7 +298,6 @@ impl ImplData {
300298
module_id,
301299
impl_loc.id.file_id(),
302300
ItemContainerId::ImplId(id),
303-
None,
304301
);
305302
collector.collect(impl_loc.id.tree_id(), &impl_def.items);
306303

@@ -398,8 +395,8 @@ impl ConstData {
398395
let loc = konst.lookup(db);
399396
let item_tree = loc.id.item_tree(db);
400397
let konst = &item_tree[loc.id.value];
401-
let visibility = if let Some(inherited_vis) = loc.inherited_visibility {
402-
inherited_vis.tree_id.item_tree(db)[inherited_vis.raw_visibility_id].clone()
398+
let visibility = if let ItemContainerId::TraitId(trait_id) = loc.container {
399+
db.trait_data(trait_id).visibility.clone()
403400
} else {
404401
item_tree[konst.visibility].clone()
405402
};
@@ -446,8 +443,6 @@ struct AssocItemCollector<'a> {
446443

447444
items: Vec<(Name, AssocItemId)>,
448445
attr_calls: Vec<(AstId<ast::Item>, MacroCallId)>,
449-
450-
inherited_visibility: Option<InheritedVisibilityLoc>,
451446
}
452447

453448
impl<'a> AssocItemCollector<'a> {
@@ -456,7 +451,6 @@ impl<'a> AssocItemCollector<'a> {
456451
module_id: ModuleId,
457452
file_id: HirFileId,
458453
container: ItemContainerId,
459-
inherited_visibility: Option<InheritedVisibilityLoc>,
460454
) -> Self {
461455
Self {
462456
db,
@@ -467,8 +461,6 @@ impl<'a> AssocItemCollector<'a> {
467461

468462
items: Vec::new(),
469463
attr_calls: Vec::new(),
470-
471-
inherited_visibility,
472464
}
473465
}
474466

@@ -511,12 +503,9 @@ impl<'a> AssocItemCollector<'a> {
511503
match item {
512504
AssocItem::Function(id) => {
513505
let item = &item_tree[id];
514-
let def = FunctionLoc {
515-
container: self.container,
516-
id: ItemTreeId::new(tree_id, id),
517-
inherited_visibility: self.inherited_visibility,
518-
}
519-
.intern(self.db);
506+
let def =
507+
FunctionLoc { container: self.container, id: ItemTreeId::new(tree_id, id) }
508+
.intern(self.db);
520509
self.items.push((item.name.clone(), def.into()));
521510
}
522511
AssocItem::Const(id) => {
@@ -525,20 +514,16 @@ impl<'a> AssocItemCollector<'a> {
525514
Some(name) => name,
526515
None => continue,
527516
};
528-
let def = ConstLoc {
529-
container: self.container,
530-
id: ItemTreeId::new(tree_id, id),
531-
inherited_visibility: self.inherited_visibility,
532-
}
533-
.intern(self.db);
517+
let def =
518+
ConstLoc { container: self.container, id: ItemTreeId::new(tree_id, id) }
519+
.intern(self.db);
534520
self.items.push((name, def.into()));
535521
}
536522
AssocItem::TypeAlias(id) => {
537523
let item = &item_tree[id];
538524
let def = TypeAliasLoc {
539525
container: self.container,
540526
id: ItemTreeId::new(tree_id, id),
541-
inherited_visibility: self.inherited_visibility,
542527
}
543528
.intern(self.db);
544529
self.items.push((item.name.clone(), def.into()));

crates/hir-def/src/lib.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use hir_expand::{
7070
AstId, ExpandError, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId,
7171
MacroDefKind, UnresolvedMacro,
7272
};
73-
use item_tree::{ExternBlock, RawVisibilityId, TreeId};
73+
use item_tree::ExternBlock;
7474
use la_arena::Idx;
7575
use nameres::DefMap;
7676
use stdx::impl_from;
@@ -156,25 +156,19 @@ impl<N: ItemTreeNode> Hash for ItemLoc<N> {
156156
}
157157
}
158158

159-
#[derive(Debug, Clone, Copy)]
160-
pub struct InheritedVisibilityLoc {
161-
pub raw_visibility_id: RawVisibilityId,
162-
pub tree_id: TreeId,
163-
}
164-
165-
impl InheritedVisibilityLoc {
166-
pub fn new(visibility_id: RawVisibilityId, tree_id: TreeId) -> Self {
167-
Self { raw_visibility_id: visibility_id, tree_id }
168-
}
169-
}
170-
171-
#[derive(Debug, Clone, Copy)]
159+
#[derive(Debug)]
172160
pub struct AssocItemLoc<N: ItemTreeNode> {
173161
pub container: ItemContainerId,
174162
pub id: ItemTreeId<N>,
175-
pub inherited_visibility: Option<InheritedVisibilityLoc>,
176163
}
177164

165+
impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
166+
fn clone(&self) -> Self {
167+
Self { container: self.container, id: self.id }
168+
}
169+
}
170+
171+
impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
178172
impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
179173
fn eq(&self, other: &Self) -> bool {
180174
self.container == other.container && self.id == other.id

crates/hir-def/src/nameres/collector.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,12 +1549,8 @@ impl ModCollector<'_, '_> {
15491549
}
15501550
ModItem::Function(id) => {
15511551
let it = &self.item_tree[id];
1552-
let fn_id = FunctionLoc {
1553-
container,
1554-
id: ItemTreeId::new(self.tree_id, id),
1555-
inherited_visibility: None,
1556-
}
1557-
.intern(db);
1552+
let fn_id =
1553+
FunctionLoc { container, id: ItemTreeId::new(self.tree_id, id) }.intern(db);
15581554

15591555
let vis = resolve_vis(def_map, &self.item_tree[it.visibility]);
15601556
if self.def_collector.is_proc_macro {
@@ -1617,12 +1613,8 @@ impl ModCollector<'_, '_> {
16171613
}
16181614
ModItem::Const(id) => {
16191615
let it = &self.item_tree[id];
1620-
let const_id = ConstLoc {
1621-
container,
1622-
id: ItemTreeId::new(self.tree_id, id),
1623-
inherited_visibility: None,
1624-
}
1625-
.intern(db);
1616+
let const_id =
1617+
ConstLoc { container, id: ItemTreeId::new(self.tree_id, id) }.intern(db);
16261618

16271619
match &it.name {
16281620
Some(name) => {
@@ -1643,13 +1635,9 @@ impl ModCollector<'_, '_> {
16431635
let vis = resolve_vis(def_map, &self.item_tree[it.visibility]);
16441636
update_def(
16451637
self.def_collector,
1646-
StaticLoc {
1647-
container,
1648-
id: ItemTreeId::new(self.tree_id, id),
1649-
inherited_visibility: None,
1650-
}
1651-
.intern(db)
1652-
.into(),
1638+
StaticLoc { container, id: ItemTreeId::new(self.tree_id, id) }
1639+
.intern(db)
1640+
.into(),
16531641
&it.name,
16541642
vis,
16551643
false,
@@ -1675,13 +1663,9 @@ impl ModCollector<'_, '_> {
16751663
let vis = resolve_vis(def_map, &self.item_tree[it.visibility]);
16761664
update_def(
16771665
self.def_collector,
1678-
TypeAliasLoc {
1679-
container,
1680-
id: ItemTreeId::new(self.tree_id, id),
1681-
inherited_visibility: None,
1682-
}
1683-
.intern(db)
1684-
.into(),
1666+
TypeAliasLoc { container, id: ItemTreeId::new(self.tree_id, id) }
1667+
.intern(db)
1668+
.into(),
16851669
&it.name,
16861670
vis,
16871671
false,

0 commit comments

Comments
 (0)