Skip to content

Commit 4f13692

Browse files
committed
resolve: Try removing questionable optimizations in eff vis computation
1 parent 4feb722 commit 4f13692

2 files changed

Lines changed: 34 additions & 27 deletions

File tree

compiler/rustc_resolve/src/effective_visibilities.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,18 @@ pub(crate) struct EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
3838
}
3939

4040
impl Resolver<'_, '_> {
41-
fn nearest_normal_mod(&self, def_id: LocalDefId) -> LocalDefId {
42-
self.get_nearest_non_block_module(def_id.to_def_id()).nearest_parent_mod().expect_local()
43-
}
44-
45-
fn private_vis_import(&self, decl: Decl<'_>) -> Visibility {
46-
let DeclKind::Import { import, .. } = decl.kind else { unreachable!() };
41+
fn private_vis_decl(&self, decl: Decl<'_>) -> Visibility {
4742
Visibility::Restricted(
48-
import
49-
.id()
50-
.map(|id| self.nearest_normal_mod(self.local_def_id(id)))
51-
.unwrap_or(CRATE_DEF_ID),
43+
decl.parent_module.map_or(CRATE_DEF_ID, |m| m.nearest_parent_mod().expect_local()),
5244
)
5345
}
5446

5547
fn private_vis_def(&self, def_id: LocalDefId) -> Visibility {
56-
// For mod items `nearest_normal_mod` returns its argument, but we actually need its parent.
57-
let normal_mod_id = self.nearest_normal_mod(def_id);
48+
// For mod items `normal_mod_id` will be equal to `def_id`, but we actually need its parent.
49+
let normal_mod_id = self
50+
.get_nearest_non_block_module(def_id.to_def_id())
51+
.nearest_parent_mod()
52+
.expect_local();
5853
if normal_mod_id == def_id {
5954
Visibility::Restricted(self.tcx.local_parent(def_id))
6055
} else {
@@ -120,15 +115,23 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
120115
// Set the given effective visibility level to `Level::Direct` and
121116
// sets the rest of the `use` chain to `Level::Reexported` until
122117
// we hit the actual exported item.
118+
let orig_private_vis = self.current_private_vis;
123119
let mut parent_id = ParentId::Def(module_id);
124120
while let DeclKind::Import { source_decl, .. } = decl.kind {
121+
if matches!(parent_id, ParentId::Import(_)) {
122+
self.current_private_vis = self.r.private_vis_decl(decl);
123+
}
125124
self.update_import(decl, parent_id);
126125
parent_id = ParentId::Import(decl);
127126
decl = source_decl;
128127
}
129128
if let Some(def_id) = decl.res().opt_def_id().and_then(|id| id.as_local()) {
129+
if matches!(parent_id, ParentId::Import(_)) {
130+
self.current_private_vis = self.r.private_vis_decl(decl);
131+
}
130132
self.update_def(def_id, decl.vis().expect_local(), parent_id);
131133
}
134+
self.current_private_vis = orig_private_vis;
132135
}
133136
}
134137

@@ -141,7 +144,7 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
141144
.effective_vis_or_private(def_id, || self.r.private_vis_def(def_id)),
142145
ParentId::Import(binding) => self
143146
.import_effective_visibilities
144-
.effective_vis_or_private(binding, || self.r.private_vis_import(binding)),
147+
.effective_vis_or_private(binding, || self.r.private_vis_decl(binding)),
145148
}
146149
}
147150

@@ -156,28 +159,30 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
156159
///
157160
/// `None` is returned if the update can be skipped,
158161
/// and cheap private visibility is returned otherwise.
159-
fn may_update(
160-
&self,
161-
nominal_vis: Visibility,
162-
parent_id: ParentId<'_>,
163-
) -> Option<Option<Visibility>> {
162+
fn may_update(&self, nominal_vis: Visibility, parent_id: ParentId<'_>) -> bool {
164163
match parent_id {
165-
ParentId::Def(def_id) => (nominal_vis != self.current_private_vis
166-
&& self.r.tcx.local_visibility(def_id) != self.current_private_vis)
167-
.then_some(Some(self.current_private_vis)),
168-
ParentId::Import(_) => Some(None),
164+
ParentId::Def(def_id) => {
165+
nominal_vis != self.current_private_vis
166+
&& self.r.tcx.local_visibility(def_id) != self.current_private_vis
167+
}
168+
ParentId::Import(decl) => {
169+
nominal_vis != self.current_private_vis
170+
&& decl.vis().expect_local() != self.current_private_vis
171+
}
169172
}
170173
}
171174

172175
fn update_import(&mut self, decl: Decl<'ra>, parent_id: ParentId<'ra>) {
173176
let nominal_vis = decl.vis().expect_local();
174-
let Some(cheap_private_vis) = self.may_update(nominal_vis, parent_id) else { return };
177+
if !self.may_update(nominal_vis, parent_id) {
178+
return;
179+
};
175180
let inherited_eff_vis = self.effective_vis_or_private(parent_id);
176181
let tcx = self.r.tcx;
177182
self.changed |= self.import_effective_visibilities.update(
178183
decl,
179184
Some(nominal_vis),
180-
|| cheap_private_vis.unwrap_or_else(|| self.r.private_vis_import(decl)),
185+
|| self.current_private_vis,
181186
inherited_eff_vis,
182187
parent_id.level(),
183188
tcx,
@@ -194,13 +199,15 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
194199
nominal_vis: Visibility,
195200
parent_id: ParentId<'ra>,
196201
) {
197-
let Some(cheap_private_vis) = self.may_update(nominal_vis, parent_id) else { return };
202+
if !self.may_update(nominal_vis, parent_id) {
203+
return;
204+
};
198205
let inherited_eff_vis = self.effective_vis_or_private(parent_id);
199206
let tcx = self.r.tcx;
200207
self.changed |= self.def_effective_visibilities.update(
201208
def_id,
202209
Some(nominal_vis),
203-
|| cheap_private_vis.unwrap_or_else(|| self.r.private_vis_def(def_id)),
210+
|| self.current_private_vis,
204211
inherited_eff_vis,
205212
parent_id.level(),
206213
tcx,

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,7 @@ impl<'ra> ResolverArenas<'ra> {
15651565
vis,
15661566
span,
15671567
LocalExpnId::ROOT,
1568-
None,
1568+
parent,
15691569
)),
15701570
ModuleKind::Block => None,
15711571
};

0 commit comments

Comments
 (0)