2013-08-08 11:38:10 -07:00
|
|
|
//! A pass that checks to make sure private fields and methods aren't used
|
2013-08-07 00:11:34 -07: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 14:25:02 +03:00
|
|
|
use crate::ty::{DefIdTree, Visibility};
|
2019-12-24 05:02:53 +01:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-11-14 16:35:31 +01:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2018-12-03 01:14:35 +01:00
|
|
|
use rustc_macros::HashStable;
|
2022-04-04 22:19:25 +02:00
|
|
|
use rustc_query_system::ich::StableHashingContext;
|
2022-09-25 14:25:02 +03:00
|
|
|
use rustc_span::def_id::{DefId, LocalDefId};
|
2015-11-19 14:16:35 +03:00
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
2020-12-21 20:00:18 -08:00
|
|
|
/// Represents the levels of accessibility an item can have.
|
|
|
|
|
///
|
|
|
|
|
/// The variants are sorted in ascending order of accessibility.
|
2018-12-03 01:14:35 +01:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, HashStable)]
|
2015-11-19 14:16:35 +03:00
|
|
|
pub enum AccessLevel {
|
2019-01-26 20:30:52 +01:00
|
|
|
/// Superset of `AccessLevel::Reachable` used to mark impl Trait items.
|
2018-08-21 00:11:59 +01:00
|
|
|
ReachableFromImplTrait,
|
2019-01-26 20:30:52 +01:00
|
|
|
/// Exported items + items participating in various kinds of public interfaces,
|
|
|
|
|
/// but not directly nameable. For example, if function `fn f() -> T {...}` is
|
|
|
|
|
/// public, then type `T` is reachable. Its values can be obtained by other crates
|
|
|
|
|
/// even if the type itself is not nameable.
|
2015-11-19 14:16:35 +03:00
|
|
|
Reachable,
|
2020-12-21 20:00:18 -08:00
|
|
|
/// Public items + items accessible to other crates with the help of `pub use` re-exports.
|
2015-11-19 14:16:35 +03:00
|
|
|
Exported,
|
2020-12-21 20:00:18 -08:00
|
|
|
/// Items accessible to other crates directly, without the help of re-exports.
|
2015-11-19 14:16:35 +03:00
|
|
|
Public,
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-25 14:25:02 +03:00
|
|
|
impl AccessLevel {
|
|
|
|
|
pub fn all_levels() -> [AccessLevel; 4] {
|
|
|
|
|
[
|
|
|
|
|
AccessLevel::Public,
|
|
|
|
|
AccessLevel::Exported,
|
|
|
|
|
AccessLevel::Reachable,
|
|
|
|
|
AccessLevel::ReachableFromImplTrait,
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable)]
|
2022-09-12 10:57:34 +03:00
|
|
|
pub struct EffectiveVisibility {
|
2022-09-25 14:25:02 +03:00
|
|
|
public: Visibility,
|
|
|
|
|
exported: Visibility,
|
|
|
|
|
reachable: Visibility,
|
|
|
|
|
reachable_from_impl_trait: Visibility,
|
2022-09-12 10:57:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl EffectiveVisibility {
|
2022-09-25 14:25:02 +03:00
|
|
|
pub fn get(&self, tag: AccessLevel) -> &Visibility {
|
2022-09-12 10:57:34 +03:00
|
|
|
match tag {
|
|
|
|
|
AccessLevel::Public => &self.public,
|
|
|
|
|
AccessLevel::Exported => &self.exported,
|
|
|
|
|
AccessLevel::Reachable => &self.reachable,
|
|
|
|
|
AccessLevel::ReachableFromImplTrait => &self.reachable_from_impl_trait,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-25 14:25:02 +03:00
|
|
|
fn get_mut(&mut self, tag: AccessLevel) -> &mut Visibility {
|
2022-09-12 10:57:34 +03:00
|
|
|
match tag {
|
|
|
|
|
AccessLevel::Public => &mut self.public,
|
|
|
|
|
AccessLevel::Exported => &mut self.exported,
|
|
|
|
|
AccessLevel::Reachable => &mut self.reachable,
|
|
|
|
|
AccessLevel::ReachableFromImplTrait => &mut self.reachable_from_impl_trait,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_public_at_level(&self, tag: AccessLevel) -> bool {
|
2022-09-25 14:25:02 +03:00
|
|
|
self.get(tag).is_public()
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 20:09:03 +03:00
|
|
|
pub fn from_vis(vis: Visibility) -> EffectiveVisibility {
|
2022-09-25 14:25:02 +03:00
|
|
|
EffectiveVisibility {
|
|
|
|
|
public: vis,
|
|
|
|
|
exported: vis,
|
|
|
|
|
reachable: vis,
|
|
|
|
|
reachable_from_impl_trait: vis,
|
|
|
|
|
}
|
2022-09-12 10:57:34 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 20:00:18 -08:00
|
|
|
/// Holds a map of accessibility levels for reachable HIR nodes.
|
2021-07-26 05:38:16 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2021-07-28 18:23:40 +03:00
|
|
|
pub struct AccessLevels<Id = LocalDefId> {
|
2022-09-12 10:57:34 +03:00
|
|
|
map: FxHashMap<Id, EffectiveVisibility>,
|
2015-11-19 14:16:35 +03:00
|
|
|
}
|
|
|
|
|
|
2022-09-12 10:57:34 +03:00
|
|
|
impl<Id: Hash + Eq + Copy> AccessLevels<Id> {
|
|
|
|
|
pub fn is_public_at_level(&self, id: Id, tag: AccessLevel) -> bool {
|
|
|
|
|
self.get_effective_vis(id)
|
|
|
|
|
.map_or(false, |effective_vis| effective_vis.is_public_at_level(tag))
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:30:52 +01:00
|
|
|
/// See `AccessLevel::Reachable`.
|
2015-11-19 14:16:35 +03:00
|
|
|
pub fn is_reachable(&self, id: Id) -> bool {
|
2022-09-12 10:57:34 +03:00
|
|
|
self.is_public_at_level(id, AccessLevel::Reachable)
|
2015-11-19 14:16:35 +03:00
|
|
|
}
|
2019-01-26 20:30:52 +01:00
|
|
|
|
|
|
|
|
/// See `AccessLevel::Exported`.
|
2015-11-19 14:16:35 +03:00
|
|
|
pub fn is_exported(&self, id: Id) -> bool {
|
2022-09-12 10:57:34 +03:00
|
|
|
self.is_public_at_level(id, AccessLevel::Exported)
|
2015-11-19 14:16:35 +03:00
|
|
|
}
|
2019-01-26 20:30:52 +01:00
|
|
|
|
|
|
|
|
/// See `AccessLevel::Public`.
|
2015-11-19 14:16:35 +03:00
|
|
|
pub fn is_public(&self, id: Id) -> bool {
|
2022-09-12 10:57:34 +03:00
|
|
|
self.is_public_at_level(id, AccessLevel::Public)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_access_level(&self, id: Id) -> Option<AccessLevel> {
|
|
|
|
|
self.get_effective_vis(id).and_then(|effective_vis| {
|
2022-09-25 14:25:02 +03:00
|
|
|
for level in AccessLevel::all_levels() {
|
2022-09-12 10:57:34 +03:00
|
|
|
if effective_vis.is_public_at_level(level) {
|
|
|
|
|
return Some(level);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_effective_vis(&self, id: Id) -> Option<&EffectiveVisibility> {
|
|
|
|
|
self.map.get(&id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (&Id, &EffectiveVisibility)> {
|
|
|
|
|
self.map.iter()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn map_id<OutId: Hash + Eq + Copy>(&self, f: impl Fn(Id) -> OutId) -> AccessLevels<OutId> {
|
|
|
|
|
AccessLevels { map: self.map.iter().map(|(k, v)| (f(*k), *v)).collect() }
|
2015-11-19 14:16:35 +03:00
|
|
|
}
|
2022-09-25 14:25:02 +03:00
|
|
|
|
|
|
|
|
pub fn set_access_level(
|
|
|
|
|
&mut self,
|
|
|
|
|
id: Id,
|
|
|
|
|
default_vis: impl FnOnce() -> Visibility,
|
|
|
|
|
tag: AccessLevel,
|
|
|
|
|
) {
|
|
|
|
|
let mut effective_vis = self
|
|
|
|
|
.get_effective_vis(id)
|
|
|
|
|
.copied()
|
|
|
|
|
.unwrap_or_else(|| EffectiveVisibility::from_vis(default_vis()));
|
|
|
|
|
for level in AccessLevel::all_levels() {
|
|
|
|
|
if level <= tag {
|
|
|
|
|
*effective_vis.get_mut(level) = Visibility::Public;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.map.insert(id, effective_vis);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<Id: Hash + Eq + Copy + Into<DefId>> AccessLevels<Id> {
|
|
|
|
|
// `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,
|
|
|
|
|
id: Id,
|
|
|
|
|
nominal_vis: Visibility,
|
|
|
|
|
default_vis: impl FnOnce() -> Visibility,
|
|
|
|
|
parent_id: Id,
|
|
|
|
|
tag: AccessLevel,
|
|
|
|
|
tree: impl DefIdTree,
|
2022-10-17 20:09:03 +03:00
|
|
|
) -> bool {
|
2022-09-25 14:25:02 +03:00
|
|
|
let mut changed = false;
|
2022-10-17 20:09:03 +03:00
|
|
|
let mut current_effective_vis = self.get_effective_vis(id).copied().unwrap_or_else(|| {
|
|
|
|
|
if id.into().is_crate_root() {
|
|
|
|
|
EffectiveVisibility::from_vis(Visibility::Public)
|
|
|
|
|
} else {
|
|
|
|
|
EffectiveVisibility::from_vis(default_vis())
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-09-25 14:25:02 +03:00
|
|
|
if let Some(inherited_effective_vis) = self.get_effective_vis(parent_id) {
|
2022-10-17 20:09:03 +03:00
|
|
|
let mut inherited_effective_vis_at_prev_level = *inherited_effective_vis.get(tag);
|
|
|
|
|
let mut calculated_effective_vis = inherited_effective_vis_at_prev_level;
|
2022-09-25 14:25:02 +03:00
|
|
|
for level in AccessLevel::all_levels() {
|
|
|
|
|
if tag >= level {
|
|
|
|
|
let inherited_effective_vis_at_level = *inherited_effective_vis.get(level);
|
2022-10-17 20:09:03 +03:00
|
|
|
let current_effective_vis_at_level = current_effective_vis.get_mut(level);
|
|
|
|
|
// 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
|
|
|
|
|
&& tag != level)
|
|
|
|
|
{
|
|
|
|
|
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 14:25:02 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.map.insert(id, current_effective_vis);
|
2022-10-17 20:09:03 +03:00
|
|
|
changed
|
2022-09-25 14:25:02 +03:00
|
|
|
}
|
2015-11-19 14:16:35 +03:00
|
|
|
}
|
|
|
|
|
|
2021-07-28 18:23:40 +03:00
|
|
|
impl<Id> Default for AccessLevels<Id> {
|
2015-11-19 14:16:35 +03:00
|
|
|
fn default() -> Self {
|
|
|
|
|
AccessLevels { map: Default::default() }
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-14 16:35:31 +01:00
|
|
|
|
|
|
|
|
impl<'a> HashStable<StableHashingContext<'a>> for AccessLevels {
|
|
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2022-04-04 22:19:25 +02:00
|
|
|
let AccessLevels { ref map } = *self;
|
|
|
|
|
map.hash_stable(hcx, hasher);
|
2020-11-14 16:35:31 +01:00
|
|
|
}
|
|
|
|
|
}
|