Skip to content

Commit 8c1ea13

Browse files
committed
Optimize private visibility resolution
1 parent 5bbf8aa commit 8c1ea13

File tree

8 files changed

+62
-18
lines changed

8 files changed

+62
-18
lines changed

crates/hir-def/src/expr_store/pretty.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ pub fn print_variant_body_hir(db: &dyn DefDatabase, owner: VariantId, edition: E
141141
let FieldData { name, type_ref, visibility, is_unsafe } = data;
142142
match visibility {
143143
crate::item_tree::RawVisibility::Module(interned, _visibility_explicitness) => {
144-
w!(p, "{}", interned.display(db, p.edition))
144+
w!(p, "pub(in {})", interned.display(db, p.edition))
145145
}
146146
crate::item_tree::RawVisibility::Public => w!(p, "pub "),
147147
crate::item_tree::RawVisibility::PubCrate => w!(p, "pub(crate) "),
148+
crate::item_tree::RawVisibility::PubSelf(_) => w!(p, "pub(self) "),
148149
}
149150
if *is_unsafe {
150151
w!(p, "unsafe ");

crates/hir-def/src/item_tree.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -414,23 +414,15 @@ impl Index<RawVisibilityId> for ItemTree {
414414
type Output = RawVisibility;
415415
fn index(&self, index: RawVisibilityId) -> &Self::Output {
416416
static VIS_PUB: RawVisibility = RawVisibility::Public;
417-
static VIS_PRIV_IMPLICIT: OnceLock<RawVisibility> = OnceLock::new();
418-
static VIS_PRIV_EXPLICIT: OnceLock<RawVisibility> = OnceLock::new();
417+
static VIS_PRIV_IMPLICIT: RawVisibility =
418+
RawVisibility::PubSelf(VisibilityExplicitness::Implicit);
419+
static VIS_PRIV_EXPLICIT: RawVisibility =
420+
RawVisibility::PubSelf(VisibilityExplicitness::Explicit);
419421
static VIS_PUB_CRATE: RawVisibility = RawVisibility::PubCrate;
420422

421423
match index {
422-
RawVisibilityId::PRIV_IMPLICIT => VIS_PRIV_IMPLICIT.get_or_init(|| {
423-
RawVisibility::Module(
424-
Interned::new(ModPath::from_kind(PathKind::SELF)),
425-
VisibilityExplicitness::Implicit,
426-
)
427-
}),
428-
RawVisibilityId::PRIV_EXPLICIT => VIS_PRIV_EXPLICIT.get_or_init(|| {
429-
RawVisibility::Module(
430-
Interned::new(ModPath::from_kind(PathKind::SELF)),
431-
VisibilityExplicitness::Explicit,
432-
)
433-
}),
424+
RawVisibilityId::PRIV_IMPLICIT => &VIS_PRIV_IMPLICIT,
425+
RawVisibilityId::PRIV_EXPLICIT => &VIS_PRIV_EXPLICIT,
434426
RawVisibilityId::PUB => &VIS_PUB,
435427
RawVisibilityId::PUB_CRATE => &VIS_PUB_CRATE,
436428
_ => &self.vis.arena[index.0 as usize],
@@ -550,6 +542,8 @@ pub enum RawVisibility {
550542
/// `pub(in module)`, `pub(crate)` or `pub(super)`. Also private, which is
551543
/// equivalent to `pub(self)`.
552544
Module(Interned<ModPath>, VisibilityExplicitness),
545+
/// `pub(self)`.
546+
PubSelf(VisibilityExplicitness),
553547
/// `pub(crate)`.
554548
PubCrate,
555549
/// `pub`.

crates/hir-def/src/item_tree/pretty.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ impl Printer<'_> {
108108
fn print_visibility(&mut self, vis: RawVisibilityId) {
109109
match &self.tree[vis] {
110110
RawVisibility::Module(path, _expl) => {
111-
w!(self, "pub({}) ", path.display(self.db, self.edition))
111+
w!(self, "pub(in {}) ", path.display(self.db, self.edition))
112112
}
113113
RawVisibility::Public => w!(self, "pub "),
114114
RawVisibility::PubCrate => w!(self, "pub(crate) "),
115+
RawVisibility::PubSelf(_) => w!(self, "pub(self) "),
115116
};
116117
}
117118

crates/hir-def/src/item_tree/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use a::{c, d::{e}};
3939
pub(self) extern crate self as renamed;
4040
4141
// AstId: ExternCrate[7E1C, 0]
42-
pub(super) extern crate bli;
42+
pub(in super) extern crate bli;
4343
4444
// AstId: Use[0000, 0]
4545
pub use crate::path::{nested, items as renamed, Trait as _};

crates/hir-def/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl<N: AstIdNode> HasModule for ItemLoc<N> {
144144

145145
#[derive(Debug)]
146146
pub struct AssocItemLoc<N: AstIdNode> {
147+
// FIXME: Store this as an erased `salsa::Id` to save space
147148
pub container: ItemContainerId,
148149
pub id: AstId<N>,
149150
}
@@ -445,6 +446,7 @@ impl HasModule for ModuleId {
445446

446447
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
447448
pub struct FieldId {
449+
// FIXME: Store this as an erased `salsa::Id` to save space
448450
pub parent: VariantId,
449451
pub local_id: LocalFieldId,
450452
}
@@ -460,6 +462,7 @@ pub struct TupleFieldId {
460462

461463
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
462464
pub struct TypeOrConstParamId {
465+
// FIXME: Store this as an erased `salsa::Id` to save space
463466
pub parent: GenericDefId,
464467
pub local_id: LocalTypeOrConstParamId,
465468
}
@@ -518,6 +521,7 @@ impl From<ConstParamId> for TypeOrConstParamId {
518521

519522
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
520523
pub struct LifetimeParamId {
524+
// FIXME: Store this as an erased `salsa::Id` to save space
521525
pub parent: GenericDefId,
522526
pub local_id: LocalLifetimeParamId,
523527
}
@@ -661,7 +665,7 @@ impl DefWithBodyId {
661665
}
662666
}
663667

664-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
668+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, salsa_macros::Supertype)]
665669
pub enum AssocItemId {
666670
FunctionId(FunctionId),
667671
ConstId(ConstId),

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ impl DefMap {
128128
}
129129
}
130130
}
131+
RawVisibility::PubSelf(explicitness) => {
132+
Visibility::Module(original_module, *explicitness)
133+
}
131134
RawVisibility::Public => Visibility::Public,
132135
RawVisibility::PubCrate => Visibility::PubCrate(self.krate),
133136
};

crates/hir-def/src/resolver.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ impl<'db> Resolver<'db> {
305305
}),
306306
)
307307
}
308+
RawVisibility::PubSelf(explicitness) => {
309+
Some(Visibility::Module(self.module(), *explicitness))
310+
}
308311
RawVisibility::PubCrate => Some(Visibility::PubCrate(self.krate())),
309312
RawVisibility::Public => Some(Visibility::Public),
310313
}

crates/hir-def/src/visibility.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ impl Visibility {
5151
Visibility::PubCrate(krate) => return from_module.krate(db) == krate,
5252
Visibility::Public => return true,
5353
};
54+
if from_module == to_module {
55+
// if the modules are the same, visibility is trivially satisfied
56+
return true;
57+
}
5458
// if they're not in the same crate, it can't be visible
5559
if from_module.krate(db) != to_module.krate(db) {
5660
return false;
@@ -73,6 +77,10 @@ impl Visibility {
7377
Visibility::PubCrate(krate) => return from_module.krate(db) == krate,
7478
Visibility::Public => return true,
7579
};
80+
if from_module == to_module {
81+
// if the modules are the same, visibility is trivially satisfied
82+
return true;
83+
}
7684
// if they're not in the same crate, it can't be visible
7785
if def_map.krate() != to_module.krate(db) {
7886
return false;
@@ -161,6 +169,21 @@ impl Visibility {
161169
if mod_.krate(db) == krate { Some(Visibility::PubCrate(krate)) } else { None }
162170
}
163171
(Visibility::Module(mod_a, expl_a), Visibility::Module(mod_b, expl_b)) => {
172+
if mod_a == mod_b {
173+
// Most module visibilities are `pub(self)`, and assuming no errors
174+
// this will be the common and thus fast path.
175+
return Some(Visibility::Module(
176+
mod_a,
177+
match (expl_a, expl_b) {
178+
(VisibilityExplicitness::Explicit, _)
179+
| (_, VisibilityExplicitness::Explicit) => {
180+
VisibilityExplicitness::Explicit
181+
}
182+
_ => VisibilityExplicitness::Implicit,
183+
},
184+
));
185+
}
186+
164187
if mod_a.krate(db) != def_map.krate() || mod_b.krate(db) != def_map.krate() {
165188
return None;
166189
}
@@ -212,6 +235,21 @@ impl Visibility {
212235
if mod_.krate(db) == krate { Some(Visibility::Module(mod_, exp)) } else { None }
213236
}
214237
(Visibility::Module(mod_a, expl_a), Visibility::Module(mod_b, expl_b)) => {
238+
if mod_a == mod_b {
239+
// Most module visibilities are `pub(self)`, and assuming no errors
240+
// this will be the common and thus fast path.
241+
return Some(Visibility::Module(
242+
mod_a,
243+
match (expl_a, expl_b) {
244+
(VisibilityExplicitness::Explicit, _)
245+
| (_, VisibilityExplicitness::Explicit) => {
246+
VisibilityExplicitness::Explicit
247+
}
248+
_ => VisibilityExplicitness::Implicit,
249+
},
250+
));
251+
}
252+
215253
if mod_a.krate(db) != def_map.krate() || mod_b.krate(db) != def_map.krate() {
216254
return None;
217255
}

0 commit comments

Comments
 (0)