Skip to content

Commit 394f153

Browse files
committed
Auto merge of #156185 - petrochenkov:queffvis, r=<try>
resolve: Try removing questionable optimizations in eff vis computation
2 parents 4feb722 + 8508f17 commit 394f153

1 file changed

Lines changed: 31 additions & 26 deletions

File tree

compiler/rustc_resolve/src/effective_visibilities.rs

Lines changed: 31 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,21 @@ 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+
self.current_private_vis = self.r.private_vis_decl(decl);
130130
self.update_def(def_id, decl.vis().expect_local(), parent_id);
131131
}
132+
self.current_private_vis = orig_private_vis;
132133
}
133134
}
134135

@@ -141,7 +142,7 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
141142
.effective_vis_or_private(def_id, || self.r.private_vis_def(def_id)),
142143
ParentId::Import(binding) => self
143144
.import_effective_visibilities
144-
.effective_vis_or_private(binding, || self.r.private_vis_import(binding)),
145+
.effective_vis_or_private(binding, || self.r.private_vis_decl(binding)),
145146
}
146147
}
147148

@@ -156,28 +157,30 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
156157
///
157158
/// `None` is returned if the update can be skipped,
158159
/// 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>> {
160+
fn may_update(&self, nominal_vis: Visibility, parent_id: ParentId<'_>) -> bool {
164161
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),
162+
ParentId::Def(def_id) => {
163+
nominal_vis != self.current_private_vis
164+
&& self.r.tcx.local_visibility(def_id) != self.current_private_vis
165+
}
166+
ParentId::Import(decl) => {
167+
nominal_vis != self.current_private_vis
168+
&& decl.vis().expect_local() != self.current_private_vis
169+
}
169170
}
170171
}
171172

172173
fn update_import(&mut self, decl: Decl<'ra>, parent_id: ParentId<'ra>) {
173174
let nominal_vis = decl.vis().expect_local();
174-
let Some(cheap_private_vis) = self.may_update(nominal_vis, parent_id) else { return };
175+
if !self.may_update(nominal_vis, parent_id) {
176+
return;
177+
};
175178
let inherited_eff_vis = self.effective_vis_or_private(parent_id);
176179
let tcx = self.r.tcx;
177180
self.changed |= self.import_effective_visibilities.update(
178181
decl,
179182
Some(nominal_vis),
180-
|| cheap_private_vis.unwrap_or_else(|| self.r.private_vis_import(decl)),
183+
|| self.current_private_vis,
181184
inherited_eff_vis,
182185
parent_id.level(),
183186
tcx,
@@ -194,13 +197,15 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
194197
nominal_vis: Visibility,
195198
parent_id: ParentId<'ra>,
196199
) {
197-
let Some(cheap_private_vis) = self.may_update(nominal_vis, parent_id) else { return };
200+
if !self.may_update(nominal_vis, parent_id) {
201+
return;
202+
};
198203
let inherited_eff_vis = self.effective_vis_or_private(parent_id);
199204
let tcx = self.r.tcx;
200205
self.changed |= self.def_effective_visibilities.update(
201206
def_id,
202207
Some(nominal_vis),
203-
|| cheap_private_vis.unwrap_or_else(|| self.r.private_vis_def(def_id)),
208+
|| self.current_private_vis,
204209
inherited_eff_vis,
205210
parent_id.level(),
206211
tcx,

0 commit comments

Comments
 (0)