2013-08-08 18:38:10 +00:00
|
|
|
//! A pass that checks to make sure private fields and methods aren't used
|
2013-08-07 07:11:34 +00:00
|
|
|
//! outside their scopes. This pass will also generate a set of exported items
|
|
|
|
//! which are available for use externally when compiled as a library.
|
2022-09-25 11:25:02 +00:00
|
|
|
use crate::ty::{DefIdTree, Visibility};
|
2019-12-24 04:02:53 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-11-14 15:35:31 +00:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2018-12-03 00:14:35 +00:00
|
|
|
use rustc_macros::HashStable;
|
2022-04-04 20:19:25 +00:00
|
|
|
use rustc_query_system::ich::StableHashingContext;
|
2022-10-29 10:58:37 +00:00
|
|
|
use rustc_span::def_id::LocalDefId;
|
2015-11-19 11:16:35 +00:00
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
/// Represents the levels of effective visibility an item can have.
|
2020-12-22 04:00:18 +00:00
|
|
|
///
|
2022-09-22 13:19:53 +00:00
|
|
|
/// The variants are sorted in ascending order of directness.
|
2018-12-03 00:14:35 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, HashStable)]
|
2022-09-22 13:19:53 +00:00
|
|
|
pub enum Level {
|
|
|
|
/// Superset of `Reachable` including items leaked through return position `impl Trait`.
|
|
|
|
ReachableThroughImplTrait,
|
|
|
|
/// Item is either reexported, or leaked through any kind of interface.
|
|
|
|
/// For example, if function `fn f() -> T {...}` is directly public, then type `T` is publicly
|
|
|
|
/// reachable and its values can be obtained by other crates even if the type itself is not
|
|
|
|
/// nameable.
|
2015-11-19 11:16:35 +00:00
|
|
|
Reachable,
|
2022-09-22 13:19:53 +00:00
|
|
|
/// Item is accessible either directly, or with help of `use` reexports.
|
|
|
|
Reexported,
|
|
|
|
/// Item is directly accessible, without help of reexports.
|
|
|
|
Direct,
|
2015-11-19 11:16:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
impl Level {
|
|
|
|
pub fn all_levels() -> [Level; 4] {
|
|
|
|
[Level::Direct, Level::Reexported, Level::Reachable, Level::ReachableThroughImplTrait]
|
2022-09-25 11:25:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable)]
|
2022-09-12 07:57:34 +00:00
|
|
|
pub struct EffectiveVisibility {
|
2022-09-22 13:19:53 +00:00
|
|
|
direct: Visibility,
|
|
|
|
reexported: Visibility,
|
2022-09-25 11:25:02 +00:00
|
|
|
reachable: Visibility,
|
2022-09-22 13:19:53 +00:00
|
|
|
reachable_through_impl_trait: Visibility,
|
2022-09-12 07:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EffectiveVisibility {
|
2022-09-22 13:19:53 +00:00
|
|
|
pub fn at_level(&self, level: Level) -> &Visibility {
|
|
|
|
match level {
|
|
|
|
Level::Direct => &self.direct,
|
|
|
|
Level::Reexported => &self.reexported,
|
|
|
|
Level::Reachable => &self.reachable,
|
|
|
|
Level::ReachableThroughImplTrait => &self.reachable_through_impl_trait,
|
2022-09-12 07:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
fn at_level_mut(&mut self, level: Level) -> &mut Visibility {
|
|
|
|
match level {
|
|
|
|
Level::Direct => &mut self.direct,
|
|
|
|
Level::Reexported => &mut self.reexported,
|
|
|
|
Level::Reachable => &mut self.reachable,
|
|
|
|
Level::ReachableThroughImplTrait => &mut self.reachable_through_impl_trait,
|
2022-09-12 07:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
pub fn is_public_at_level(&self, level: Level) -> bool {
|
|
|
|
self.at_level(level).is_public()
|
2022-09-25 11:25:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 17:09:03 +00:00
|
|
|
pub fn from_vis(vis: Visibility) -> EffectiveVisibility {
|
2022-09-25 11:25:02 +00:00
|
|
|
EffectiveVisibility {
|
2022-09-22 13:19:53 +00:00
|
|
|
direct: vis,
|
|
|
|
reexported: vis,
|
2022-09-25 11:25:02 +00:00
|
|
|
reachable: vis,
|
2022-09-22 13:19:53 +00:00
|
|
|
reachable_through_impl_trait: vis,
|
2022-09-25 11:25:02 +00:00
|
|
|
}
|
2022-09-12 07:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
/// Holds a map of effective visibilities for reachable HIR nodes.
|
2022-10-29 10:58:37 +00:00
|
|
|
#[derive(Default, Clone, Debug)]
|
|
|
|
pub struct EffectiveVisibilities {
|
|
|
|
map: FxHashMap<LocalDefId, EffectiveVisibility>,
|
2015-11-19 11:16:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 10:58:37 +00:00
|
|
|
impl EffectiveVisibilities {
|
|
|
|
pub fn is_public_at_level(&self, id: LocalDefId, level: Level) -> bool {
|
2022-09-22 13:19:53 +00:00
|
|
|
self.effective_vis(id)
|
|
|
|
.map_or(false, |effective_vis| effective_vis.is_public_at_level(level))
|
2022-09-12 07:57:34 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
/// See `Level::Reachable`.
|
2022-10-29 10:58:37 +00:00
|
|
|
pub fn is_reachable(&self, id: LocalDefId) -> bool {
|
2022-09-22 13:19:53 +00:00
|
|
|
self.is_public_at_level(id, Level::Reachable)
|
2015-11-19 11:16:35 +00:00
|
|
|
}
|
2019-01-26 19:30:52 +00:00
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
/// See `Level::Reexported`.
|
2022-10-29 10:58:37 +00:00
|
|
|
pub fn is_exported(&self, id: LocalDefId) -> bool {
|
2022-09-22 13:19:53 +00:00
|
|
|
self.is_public_at_level(id, Level::Reexported)
|
2015-11-19 11:16:35 +00:00
|
|
|
}
|
2019-01-26 19:30:52 +00:00
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
/// See `Level::Direct`.
|
2022-10-29 10:58:37 +00:00
|
|
|
pub fn is_directly_public(&self, id: LocalDefId) -> bool {
|
2022-09-22 13:19:53 +00:00
|
|
|
self.is_public_at_level(id, Level::Direct)
|
2022-09-12 07:57:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 10:58:37 +00:00
|
|
|
pub fn public_at_level(&self, id: LocalDefId) -> Option<Level> {
|
2022-09-22 13:19:53 +00:00
|
|
|
self.effective_vis(id).and_then(|effective_vis| {
|
|
|
|
for level in Level::all_levels() {
|
2022-09-12 07:57:34 +00:00
|
|
|
if effective_vis.is_public_at_level(level) {
|
|
|
|
return Some(level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-29 10:58:37 +00:00
|
|
|
pub fn effective_vis(&self, id: LocalDefId) -> Option<&EffectiveVisibility> {
|
2022-09-12 07:57:34 +00:00
|
|
|
self.map.get(&id)
|
|
|
|
}
|
|
|
|
|
2022-10-29 10:58:37 +00:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (&LocalDefId, &EffectiveVisibility)> {
|
2022-09-12 07:57:34 +00:00
|
|
|
self.map.iter()
|
|
|
|
}
|
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
pub fn set_public_at_level(
|
2022-09-25 11:25:02 +00:00
|
|
|
&mut self,
|
2022-10-29 10:58:37 +00:00
|
|
|
id: LocalDefId,
|
2022-09-25 11:25:02 +00:00
|
|
|
default_vis: impl FnOnce() -> Visibility,
|
2022-09-22 13:19:53 +00:00
|
|
|
level: Level,
|
2022-09-25 11:25:02 +00:00
|
|
|
) {
|
|
|
|
let mut effective_vis = self
|
2022-09-22 13:19:53 +00:00
|
|
|
.effective_vis(id)
|
2022-09-25 11:25:02 +00:00
|
|
|
.copied()
|
|
|
|
.unwrap_or_else(|| EffectiveVisibility::from_vis(default_vis()));
|
2022-09-22 13:19:53 +00:00
|
|
|
for l in Level::all_levels() {
|
|
|
|
if l <= level {
|
|
|
|
*effective_vis.at_level_mut(l) = Visibility::Public;
|
2022-09-25 11:25:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.map.insert(id, effective_vis);
|
|
|
|
}
|
|
|
|
|
|
|
|
// `parent_id` is not necessarily a parent in source code tree,
|
|
|
|
// it is the node from which the maximum effective visibility is inherited.
|
|
|
|
pub fn update(
|
|
|
|
&mut self,
|
2022-10-29 10:58:37 +00:00
|
|
|
id: LocalDefId,
|
2022-09-25 11:25:02 +00:00
|
|
|
nominal_vis: Visibility,
|
|
|
|
default_vis: impl FnOnce() -> Visibility,
|
2022-10-29 10:58:37 +00:00
|
|
|
parent_id: LocalDefId,
|
2022-09-22 13:19:53 +00:00
|
|
|
level: Level,
|
2022-09-25 11:25:02 +00:00
|
|
|
tree: impl DefIdTree,
|
2022-10-17 17:09:03 +00:00
|
|
|
) -> bool {
|
2022-09-25 11:25:02 +00:00
|
|
|
let mut changed = false;
|
2022-09-22 13:19:53 +00:00
|
|
|
let mut current_effective_vis = self.effective_vis(id).copied().unwrap_or_else(|| {
|
2022-10-29 10:58:37 +00:00
|
|
|
if id.is_top_level_module() {
|
2022-10-17 17:09:03 +00:00
|
|
|
EffectiveVisibility::from_vis(Visibility::Public)
|
|
|
|
} else {
|
|
|
|
EffectiveVisibility::from_vis(default_vis())
|
|
|
|
}
|
|
|
|
});
|
2022-09-22 13:19:53 +00:00
|
|
|
if let Some(inherited_effective_vis) = self.effective_vis(parent_id) {
|
|
|
|
let mut inherited_effective_vis_at_prev_level =
|
|
|
|
*inherited_effective_vis.at_level(level);
|
2022-10-17 17:09:03 +00:00
|
|
|
let mut calculated_effective_vis = inherited_effective_vis_at_prev_level;
|
2022-09-22 13:19:53 +00:00
|
|
|
for l in Level::all_levels() {
|
|
|
|
if level >= l {
|
|
|
|
let inherited_effective_vis_at_level = *inherited_effective_vis.at_level(l);
|
|
|
|
let current_effective_vis_at_level = current_effective_vis.at_level_mut(l);
|
2022-10-17 17:09:03 +00:00
|
|
|
// effective visibility for id shouldn't be recalculated if
|
|
|
|
// inherited from parent_id effective visibility isn't changed at next level
|
|
|
|
if !(inherited_effective_vis_at_prev_level == inherited_effective_vis_at_level
|
2022-09-22 13:19:53 +00:00
|
|
|
&& level != l)
|
2022-10-17 17:09:03 +00:00
|
|
|
{
|
|
|
|
calculated_effective_vis =
|
|
|
|
if nominal_vis.is_at_least(inherited_effective_vis_at_level, tree) {
|
|
|
|
inherited_effective_vis_at_level
|
|
|
|
} else {
|
|
|
|
nominal_vis
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// effective visibility can't be decreased at next update call for the
|
|
|
|
// same id
|
|
|
|
if *current_effective_vis_at_level != calculated_effective_vis
|
|
|
|
&& calculated_effective_vis
|
|
|
|
.is_at_least(*current_effective_vis_at_level, tree)
|
|
|
|
{
|
|
|
|
changed = true;
|
|
|
|
*current_effective_vis_at_level = calculated_effective_vis;
|
|
|
|
}
|
|
|
|
inherited_effective_vis_at_prev_level = inherited_effective_vis_at_level;
|
2022-09-25 11:25:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.map.insert(id, current_effective_vis);
|
2022-10-17 17:09:03 +00:00
|
|
|
changed
|
2022-09-25 11:25:02 +00:00
|
|
|
}
|
2015-11-19 11:16:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 13:19:53 +00:00
|
|
|
impl<'a> HashStable<StableHashingContext<'a>> for EffectiveVisibilities {
|
2020-11-14 15:35:31 +00:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2022-09-22 13:19:53 +00:00
|
|
|
let EffectiveVisibilities { ref map } = *self;
|
2022-04-04 20:19:25 +00:00
|
|
|
map.hash_stable(hcx, hasher);
|
2020-11-14 15:35:31 +00:00
|
|
|
}
|
|
|
|
}
|