summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_resolve/src/effective_visibilities.rs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_resolve/src/effective_visibilities.rs')
-rw-r--r--compiler/rustc_resolve/src/effective_visibilities.rs84
1 files changed, 62 insertions, 22 deletions
diff --git a/compiler/rustc_resolve/src/effective_visibilities.rs b/compiler/rustc_resolve/src/effective_visibilities.rs
index 7bd90d7e3..bed579f6b 100644
--- a/compiler/rustc_resolve/src/effective_visibilities.rs
+++ b/compiler/rustc_resolve/src/effective_visibilities.rs
@@ -4,12 +4,13 @@ use rustc_ast::visit;
use rustc_ast::visit::Visitor;
use rustc_ast::Crate;
use rustc_ast::EnumDef;
+use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::intern::Interned;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::def_id::CRATE_DEF_ID;
use rustc_middle::middle::privacy::Level;
use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility};
-use rustc_middle::ty::{DefIdTree, Visibility};
+use rustc_middle::ty::Visibility;
use std::mem;
type ImportId<'a> = Interned<'a, NameBinding<'a>>;
@@ -60,7 +61,7 @@ impl Resolver<'_, '_> {
// For mod items `nearest_normal_mod` returns its argument, but we actually need its parent.
let normal_mod_id = self.nearest_normal_mod(def_id);
if normal_mod_id == def_id {
- self.opt_local_parent(def_id).map_or(Visibility::Public, Visibility::Restricted)
+ Visibility::Restricted(self.tcx.local_parent(def_id))
} else {
Visibility::Restricted(normal_mod_id)
}
@@ -70,21 +71,20 @@ impl Resolver<'_, '_> {
impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
/// Fills the `Resolver::effective_visibilities` table with public & exported items
/// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
- /// need access to a TyCtxt for that.
+ /// need access to a TyCtxt for that. Returns the set of ambiguous re-exports.
pub(crate) fn compute_effective_visibilities<'c>(
r: &'r mut Resolver<'a, 'tcx>,
krate: &'c Crate,
- ) {
+ ) -> FxHashSet<Interned<'a, NameBinding<'a>>> {
let mut visitor = EffectiveVisibilitiesVisitor {
r,
def_effective_visibilities: Default::default(),
import_effective_visibilities: Default::default(),
- current_private_vis: Visibility::Public,
+ current_private_vis: Visibility::Restricted(CRATE_DEF_ID),
changed: false,
};
- visitor.update(CRATE_DEF_ID, CRATE_DEF_ID);
- visitor.current_private_vis = Visibility::Restricted(CRATE_DEF_ID);
+ visitor.def_effective_visibilities.update_root();
visitor.set_bindings_effective_visibilities(CRATE_DEF_ID);
while visitor.changed {
@@ -93,18 +93,26 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
}
visitor.r.effective_visibilities = visitor.def_effective_visibilities;
+ let mut exported_ambiguities = FxHashSet::default();
+
// Update visibilities for import def ids. These are not used during the
// `EffectiveVisibilitiesVisitor` pass, because we have more detailed binding-based
// information, but are used by later passes. Effective visibility of an import def id
// is the maximum value among visibilities of bindings corresponding to that def id.
for (binding, eff_vis) in visitor.import_effective_visibilities.iter() {
let NameBindingKind::Import { import, .. } = binding.kind else { unreachable!() };
- if let Some(node_id) = import.id() {
- r.effective_visibilities.update_eff_vis(r.local_def_id(node_id), eff_vis, r.tcx)
+ if !binding.is_ambiguity() {
+ if let Some(node_id) = import.id() {
+ r.effective_visibilities.update_eff_vis(r.local_def_id(node_id), eff_vis, r.tcx)
+ }
+ } else if binding.ambiguity.is_some() && eff_vis.is_public_at_level(Level::Reexported) {
+ exported_ambiguities.insert(*binding);
}
}
info!("resolve::effective_visibilities: {:#?}", r.effective_visibilities);
+
+ exported_ambiguities
}
/// Update effective visibilities of bindings in the given module,
@@ -115,30 +123,38 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
let resolutions = self.r.resolutions(module);
for (_, name_resolution) in resolutions.borrow().iter() {
- if let Some(mut binding) = name_resolution.borrow().binding() && !binding.is_ambiguity() {
+ if let Some(mut binding) = name_resolution.borrow().binding() {
// Set the given effective visibility level to `Level::Direct` and
// sets the rest of the `use` chain to `Level::Reexported` until
// we hit the actual exported item.
+ //
+ // If the binding is ambiguous, put the root ambiguity binding and all reexports
+ // leading to it into the table. They are used by the `ambiguous_glob_reexports`
+ // lint. For all bindings added to the table this way `is_ambiguity` returns true.
let mut parent_id = ParentId::Def(module_id);
while let NameBindingKind::Import { binding: nested_binding, .. } = binding.kind {
let binding_id = ImportId::new_unchecked(binding);
self.update_import(binding_id, parent_id);
+ if binding.ambiguity.is_some() {
+ // Stop at the root ambiguity, further bindings in the chain should not
+ // be reexported because the root ambiguity blocks any access to them.
+ // (Those further bindings are most likely not ambiguities themselves.)
+ break;
+ }
+
parent_id = ParentId::Import(binding_id);
binding = nested_binding;
}
- if let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local()) {
+ if binding.ambiguity.is_none()
+ && let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local()) {
self.update_def(def_id, binding.vis.expect_local(), parent_id);
}
}
}
}
- fn cheap_private_vis(&self, parent_id: ParentId<'_>) -> Option<Visibility> {
- matches!(parent_id, ParentId::Def(_)).then_some(self.current_private_vis)
- }
-
fn effective_vis_or_private(&mut self, parent_id: ParentId<'a>) -> EffectiveVisibility {
// Private nodes are only added to the table for caching, they could be added or removed at
// any moment without consequences, so we don't set `changed` to true when adding them.
@@ -152,15 +168,39 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
}
}
+ /// All effective visibilities for a node are larger or equal than private visibility
+ /// for that node (see `check_invariants` in middle/privacy.rs).
+ /// So if either parent or nominal visibility is the same as private visibility, then
+ /// `min(parent_vis, nominal_vis) <= private_vis`, and the update logic is guaranteed
+ /// to not update anything and we can skip it.
+ ///
+ /// We are checking this condition only if the correct value of private visibility is
+ /// cheaply available, otherwise it does't make sense performance-wise.
+ ///
+ /// `None` is returned if the update can be skipped,
+ /// and cheap private visibility is returned otherwise.
+ fn may_update(
+ &self,
+ nominal_vis: Visibility,
+ parent_id: ParentId<'_>,
+ ) -> Option<Option<Visibility>> {
+ match parent_id {
+ ParentId::Def(def_id) => (nominal_vis != self.current_private_vis
+ && self.r.visibilities[&def_id] != self.current_private_vis)
+ .then_some(Some(self.current_private_vis)),
+ ParentId::Import(_) => Some(None),
+ }
+ }
+
fn update_import(&mut self, binding: ImportId<'a>, parent_id: ParentId<'a>) {
let nominal_vis = binding.vis.expect_local();
- let private_vis = self.cheap_private_vis(parent_id);
+ let Some(cheap_private_vis) = self.may_update(nominal_vis, parent_id) else { return };
let inherited_eff_vis = self.effective_vis_or_private(parent_id);
let tcx = self.r.tcx;
self.changed |= self.import_effective_visibilities.update(
binding,
nominal_vis,
- || private_vis.unwrap_or_else(|| self.r.private_vis_import(binding)),
+ || cheap_private_vis.unwrap_or_else(|| self.r.private_vis_import(binding)),
inherited_eff_vis,
parent_id.level(),
tcx,
@@ -168,20 +208,20 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
}
fn update_def(&mut self, def_id: LocalDefId, nominal_vis: Visibility, parent_id: ParentId<'a>) {
- let private_vis = self.cheap_private_vis(parent_id);
+ let Some(cheap_private_vis) = self.may_update(nominal_vis, parent_id) else { return };
let inherited_eff_vis = self.effective_vis_or_private(parent_id);
let tcx = self.r.tcx;
self.changed |= self.def_effective_visibilities.update(
def_id,
nominal_vis,
- || private_vis.unwrap_or_else(|| self.r.private_vis_def(def_id)),
+ || cheap_private_vis.unwrap_or_else(|| self.r.private_vis_def(def_id)),
inherited_eff_vis,
parent_id.level(),
tcx,
);
}
- fn update(&mut self, def_id: LocalDefId, parent_id: LocalDefId) {
+ fn update_field(&mut self, def_id: LocalDefId, parent_id: LocalDefId) {
self.update_def(def_id, self.r.visibilities[&def_id], ParentId::Def(parent_id));
}
}
@@ -213,14 +253,14 @@ impl<'r, 'ast, 'tcx> Visitor<'ast> for EffectiveVisibilitiesVisitor<'ast, 'r, 't
for variant in variants {
let variant_def_id = self.r.local_def_id(variant.id);
for field in variant.data.fields() {
- self.update(self.r.local_def_id(field.id), variant_def_id);
+ self.update_field(self.r.local_def_id(field.id), variant_def_id);
}
}
}
ast::ItemKind::Struct(ref def, _) | ast::ItemKind::Union(ref def, _) => {
for field in def.fields() {
- self.update(self.r.local_def_id(field.id), def_id);
+ self.update_field(self.r.local_def_id(field.id), def_id);
}
}