Skip to content

Commit b3bfeaf

Browse files
committed
effvis: Stop considering crate root its own parent
It helped to reuse `update_def` for the crate root, but it created confusion and caused some mistakes when I implemented #109500
1 parent ede21e8 commit b3bfeaf

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

compiler/rustc_middle/src/middle/privacy.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_data_structures::fx::FxHashMap;
66
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
77
use rustc_macros::HashStable;
88
use rustc_query_system::ich::StableHashingContext;
9-
use rustc_span::def_id::LocalDefId;
9+
use rustc_span::def_id::{LocalDefId, CRATE_DEF_ID};
1010
use std::hash::Hash;
1111

1212
/// Represents the levels of effective visibility an item can have.
@@ -107,6 +107,10 @@ impl EffectiveVisibilities {
107107
})
108108
}
109109

110+
pub fn update_root(&mut self) {
111+
self.map.insert(CRATE_DEF_ID, EffectiveVisibility::from_vis(Visibility::Public));
112+
}
113+
110114
// FIXME: Share code with `fn update`.
111115
pub fn update_eff_vis(
112116
&mut self,

compiler/rustc_resolve/src/effective_visibilities.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Resolver<'_, '_> {
6161
// For mod items `nearest_normal_mod` returns its argument, but we actually need its parent.
6262
let normal_mod_id = self.nearest_normal_mod(def_id);
6363
if normal_mod_id == def_id {
64-
self.tcx.opt_local_parent(def_id).map_or(Visibility::Public, Visibility::Restricted)
64+
Visibility::Restricted(self.tcx.local_parent(def_id))
6565
} else {
6666
Visibility::Restricted(normal_mod_id)
6767
}
@@ -80,12 +80,11 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
8080
r,
8181
def_effective_visibilities: Default::default(),
8282
import_effective_visibilities: Default::default(),
83-
current_private_vis: Visibility::Public,
83+
current_private_vis: Visibility::Restricted(CRATE_DEF_ID),
8484
changed: false,
8585
};
8686

87-
visitor.update(CRATE_DEF_ID, CRATE_DEF_ID);
88-
visitor.current_private_vis = Visibility::Restricted(CRATE_DEF_ID);
87+
visitor.def_effective_visibilities.update_root();
8988
visitor.set_bindings_effective_visibilities(CRATE_DEF_ID);
9089

9190
while visitor.changed {
@@ -202,7 +201,7 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
202201
);
203202
}
204203

205-
fn update(&mut self, def_id: LocalDefId, parent_id: LocalDefId) {
204+
fn update_field(&mut self, def_id: LocalDefId, parent_id: LocalDefId) {
206205
self.update_def(def_id, self.r.visibilities[&def_id], ParentId::Def(parent_id));
207206
}
208207
}
@@ -234,14 +233,14 @@ impl<'r, 'ast, 'tcx> Visitor<'ast> for EffectiveVisibilitiesVisitor<'ast, 'r, 't
234233
for variant in variants {
235234
let variant_def_id = self.r.local_def_id(variant.id);
236235
for field in variant.data.fields() {
237-
self.update(self.r.local_def_id(field.id), variant_def_id);
236+
self.update_field(self.r.local_def_id(field.id), variant_def_id);
238237
}
239238
}
240239
}
241240

242241
ast::ItemKind::Struct(ref def, _) | ast::ItemKind::Union(ref def, _) => {
243242
for field in def.fields() {
244-
self.update(self.r.local_def_id(field.id), def_id);
243+
self.update_field(self.r.local_def_id(field.id), def_id);
245244
}
246245
}
247246

0 commit comments

Comments
 (0)