2022-10-19 13:56:41 +00:00
|
|
|
use crate::{ImportKind, NameBindingKind, Resolver};
|
2021-07-26 03:38:16 +00:00
|
|
|
use rustc_ast::ast;
|
|
|
|
use rustc_ast::visit;
|
|
|
|
use rustc_ast::visit::Visitor;
|
|
|
|
use rustc_ast::Crate;
|
|
|
|
use rustc_ast::EnumDef;
|
|
|
|
use rustc_hir::def_id::LocalDefId;
|
|
|
|
use rustc_hir::def_id::CRATE_DEF_ID;
|
2022-09-22 13:19:53 +00:00
|
|
|
use rustc_middle::middle::privacy::Level;
|
2022-09-25 11:25:02 +00:00
|
|
|
use rustc_middle::ty::{DefIdTree, Visibility};
|
2021-07-26 03:38:16 +00:00
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
pub struct EffectiveVisibilitiesVisitor<'r, 'a> {
|
2021-07-26 03:38:16 +00:00
|
|
|
r: &'r mut Resolver<'a>,
|
|
|
|
changed: bool,
|
|
|
|
}
|
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
|
|
|
|
/// Fills the `Resolver::effective_visibilities` table with public & exported items
|
2021-07-26 03:38:16 +00:00
|
|
|
/// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
|
|
|
|
/// need access to a TyCtxt for that.
|
2022-09-22 13:19:53 +00:00
|
|
|
pub fn compute_effective_visibilities<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) {
|
|
|
|
let mut visitor = EffectiveVisibilitiesVisitor { r, changed: false };
|
2021-07-26 03:38:16 +00:00
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
visitor.update(CRATE_DEF_ID, Visibility::Public, CRATE_DEF_ID, Level::Direct);
|
|
|
|
visitor.set_bindings_effective_visibilities(CRATE_DEF_ID);
|
2021-07-26 03:38:16 +00:00
|
|
|
|
|
|
|
while visitor.changed {
|
|
|
|
visitor.reset();
|
|
|
|
visit::walk_crate(&mut visitor, krate);
|
|
|
|
}
|
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
info!("resolve::effective_visibilities: {:#?}", r.effective_visibilities);
|
2021-07-26 03:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn reset(&mut self) {
|
|
|
|
self.changed = false;
|
|
|
|
}
|
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
/// Update effective visibilities of bindings in the given module,
|
|
|
|
/// including their whole reexport chains.
|
|
|
|
fn set_bindings_effective_visibilities(&mut self, module_id: LocalDefId) {
|
2021-07-26 03:38:16 +00:00
|
|
|
assert!(self.r.module_map.contains_key(&&module_id.to_def_id()));
|
2022-08-14 14:04:30 +00:00
|
|
|
let module = self.r.get_module(module_id.to_def_id()).unwrap();
|
|
|
|
let resolutions = self.r.resolutions(module);
|
|
|
|
|
2022-10-19 13:56:41 +00:00
|
|
|
for (_, name_resolution) in resolutions.borrow().iter() {
|
2022-09-25 11:25:02 +00:00
|
|
|
if let Some(mut binding) = name_resolution.borrow().binding() && !binding.is_ambiguity() {
|
2022-09-22 13:19:53 +00:00
|
|
|
// Set the given effective visibility level to `Level::Direct` and
|
|
|
|
// sets the rest of the `use` chain to `Level::Reexported` until
|
2022-09-25 11:25:02 +00:00
|
|
|
// we hit the actual exported item.
|
|
|
|
|
2022-10-19 13:56:41 +00:00
|
|
|
// FIXME: tag and is_public() condition should be removed, but assertions occur.
|
2022-09-22 13:19:53 +00:00
|
|
|
let tag = if binding.is_import() { Level::Reexported } else { Level::Direct };
|
2022-09-25 11:25:02 +00:00
|
|
|
if binding.vis.is_public() {
|
|
|
|
let mut prev_parent_id = module_id;
|
2022-09-22 13:19:53 +00:00
|
|
|
let mut level = Level::Direct;
|
2022-09-25 11:25:02 +00:00
|
|
|
while let NameBindingKind::Import { binding: nested_binding, import, .. } =
|
|
|
|
binding.kind
|
|
|
|
{
|
2022-10-30 09:35:31 +00:00
|
|
|
let mut update = |node_id| {
|
|
|
|
self.update(
|
|
|
|
self.r.local_def_id(node_id),
|
|
|
|
binding.vis.expect_local(),
|
|
|
|
prev_parent_id,
|
|
|
|
level,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
match import.kind {
|
|
|
|
ImportKind::Single { id, additional_ids, .. } => {
|
|
|
|
// In theory all the import IDs have individual visibilities and
|
|
|
|
// effective visibilities, but in practice these IDs go straigth to
|
|
|
|
// HIR where all their few uses assume that their (effective)
|
|
|
|
// visibility applies to the whole syntactic `use` item. So we
|
|
|
|
// update them all to the maximum value among the potential
|
|
|
|
// individual effective visibilities. Maybe HIR for imports
|
|
|
|
// shouldn't use three IDs at all.
|
|
|
|
update(id);
|
|
|
|
update(additional_ids.0);
|
|
|
|
update(additional_ids.1);
|
|
|
|
prev_parent_id = self.r.local_def_id(id);
|
|
|
|
}
|
|
|
|
ImportKind::Glob { id, .. } | ImportKind::ExternCrate { id, .. } => {
|
|
|
|
update(id);
|
|
|
|
prev_parent_id = self.r.local_def_id(id);
|
|
|
|
}
|
|
|
|
ImportKind::MacroUse => {
|
|
|
|
// In theory we should reset the parent id to something private
|
|
|
|
// here, but `macro_use` imports always refer to external items,
|
|
|
|
// so it doesn't matter and we can just do nothing.
|
|
|
|
}
|
2022-10-30 11:55:58 +00:00
|
|
|
ImportKind::MacroExport => {
|
|
|
|
// In theory we should reset the parent id to something public
|
|
|
|
// here, but it has the same effect as leaving the previous parent,
|
|
|
|
// so we can just do nothing.
|
|
|
|
}
|
2022-10-19 13:56:41 +00:00
|
|
|
}
|
2022-09-25 11:25:02 +00:00
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
level = Level::Reexported;
|
2022-09-25 11:25:02 +00:00
|
|
|
binding = nested_binding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 14:04:30 +00:00
|
|
|
if let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local()) {
|
2022-09-25 11:25:02 +00:00
|
|
|
self.update(def_id, binding.vis.expect_local(), module_id, tag);
|
2021-07-26 03:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 11:25:02 +00:00
|
|
|
fn update(
|
2021-07-26 03:38:16 +00:00
|
|
|
&mut self,
|
|
|
|
def_id: LocalDefId,
|
2022-09-25 11:25:02 +00:00
|
|
|
nominal_vis: Visibility,
|
|
|
|
parent_id: LocalDefId,
|
2022-09-22 13:19:53 +00:00
|
|
|
tag: Level,
|
2022-09-25 11:25:02 +00:00
|
|
|
) {
|
2022-10-17 17:09:03 +00:00
|
|
|
let module_id = self
|
|
|
|
.r
|
|
|
|
.get_nearest_non_block_module(def_id.to_def_id())
|
|
|
|
.nearest_parent_mod()
|
|
|
|
.expect_local();
|
|
|
|
if nominal_vis == Visibility::Restricted(module_id)
|
|
|
|
|| self.r.visibilities[&parent_id] == Visibility::Restricted(module_id)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-09-22 13:19:53 +00:00
|
|
|
let mut effective_visibilities = std::mem::take(&mut self.r.effective_visibilities);
|
|
|
|
self.changed |= effective_visibilities.update(
|
2022-09-25 11:25:02 +00:00
|
|
|
def_id,
|
|
|
|
nominal_vis,
|
|
|
|
|| Visibility::Restricted(module_id),
|
|
|
|
parent_id,
|
|
|
|
tag,
|
|
|
|
&*self.r,
|
|
|
|
);
|
2022-09-22 13:19:53 +00:00
|
|
|
self.r.effective_visibilities = effective_visibilities;
|
2021-07-26 03:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
impl<'r, 'ast> Visitor<'ast> for EffectiveVisibilitiesVisitor<'ast, 'r> {
|
2021-07-26 03:38:16 +00:00
|
|
|
fn visit_item(&mut self, item: &'ast ast::Item) {
|
2022-08-14 14:04:30 +00:00
|
|
|
let def_id = self.r.local_def_id(item.id);
|
2022-09-22 13:19:53 +00:00
|
|
|
// Update effective visibilities of nested items.
|
2022-08-14 14:04:30 +00:00
|
|
|
// If it's a mod, also make the visitor walk all of its items
|
|
|
|
match item.kind {
|
2021-07-26 03:38:16 +00:00
|
|
|
// Resolved in rustc_privacy when types are available
|
|
|
|
ast::ItemKind::Impl(..) => return,
|
|
|
|
|
|
|
|
// Should be unreachable at this stage
|
|
|
|
ast::ItemKind::MacCall(..) => panic!(
|
|
|
|
"ast::ItemKind::MacCall encountered, this should not anymore appear at this stage"
|
|
|
|
),
|
|
|
|
|
2022-08-14 14:04:30 +00:00
|
|
|
// Foreign modules inherit level from parents.
|
|
|
|
ast::ItemKind::ForeignMod(..) => {
|
2022-09-25 11:25:02 +00:00
|
|
|
let parent_id = self.r.local_parent(def_id);
|
2022-09-22 13:19:53 +00:00
|
|
|
self.update(def_id, Visibility::Public, parent_id, Level::Direct);
|
2022-08-14 14:04:30 +00:00
|
|
|
}
|
2021-07-26 03:38:16 +00:00
|
|
|
|
2022-08-14 14:04:30 +00:00
|
|
|
ast::ItemKind::Mod(..) => {
|
2022-09-22 13:19:53 +00:00
|
|
|
self.set_bindings_effective_visibilities(def_id);
|
2021-07-26 03:38:16 +00:00
|
|
|
visit::walk_item(self, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::ItemKind::Enum(EnumDef { ref variants }, _) => {
|
2022-09-22 13:19:53 +00:00
|
|
|
self.set_bindings_effective_visibilities(def_id);
|
2021-07-26 03:38:16 +00:00
|
|
|
for variant in variants {
|
2022-08-14 14:04:30 +00:00
|
|
|
let variant_def_id = self.r.local_def_id(variant.id);
|
2021-07-26 03:38:16 +00:00
|
|
|
for field in variant.data.fields() {
|
2022-09-25 11:25:02 +00:00
|
|
|
let field_def_id = self.r.local_def_id(field.id);
|
|
|
|
let vis = self.r.visibilities[&field_def_id];
|
2022-09-22 13:19:53 +00:00
|
|
|
self.update(field_def_id, vis, variant_def_id, Level::Direct);
|
2021-07-26 03:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 14:04:30 +00:00
|
|
|
ast::ItemKind::Struct(ref def, _) | ast::ItemKind::Union(ref def, _) => {
|
2021-07-26 03:38:16 +00:00
|
|
|
for field in def.fields() {
|
2022-09-25 11:25:02 +00:00
|
|
|
let field_def_id = self.r.local_def_id(field.id);
|
|
|
|
let vis = self.r.visibilities[&field_def_id];
|
2022-09-22 13:19:53 +00:00
|
|
|
self.update(field_def_id, vis, def_id, Level::Direct);
|
2021-07-26 03:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-14 14:04:30 +00:00
|
|
|
|
|
|
|
ast::ItemKind::Trait(..) => {
|
2022-09-22 13:19:53 +00:00
|
|
|
self.set_bindings_effective_visibilities(def_id);
|
2021-07-26 03:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ast::ItemKind::ExternCrate(..)
|
|
|
|
| ast::ItemKind::Use(..)
|
|
|
|
| ast::ItemKind::Static(..)
|
|
|
|
| ast::ItemKind::Const(..)
|
|
|
|
| ast::ItemKind::GlobalAsm(..)
|
|
|
|
| ast::ItemKind::TyAlias(..)
|
|
|
|
| ast::ItemKind::TraitAlias(..)
|
|
|
|
| ast::ItemKind::MacroDef(..)
|
|
|
|
| ast::ItemKind::Fn(..) => return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|