Skip to main content

rustc_middle/middle/
privacy.rs

1//! A pass that checks to make sure private fields and methods aren't used
2//! outside their scopes. This pass will also generate a set of exported items
3//! which are available for use externally when compiled as a library.
4
5use std::cmp::Ordering;
6use std::hash::Hash;
7
8use rustc_data_structures::fx::{FxIndexMap, IndexEntry};
9use rustc_data_structures::stable_hash::{StableHash, StableHashCtxt, StableHasher};
10use rustc_hir::def::DefKind;
11use rustc_hir::{ItemKind, Node, UseKind};
12use rustc_macros::StableHash;
13use rustc_span::def_id::{CRATE_DEF_ID, LocalDefId};
14
15use crate::ty::{TyCtxt, Visibility};
16
17/// Represents the levels of effective visibility an item can have.
18///
19/// The variants are sorted in ascending order of directness.
20#[derive(#[automatically_derived]
impl ::core::clone::Clone for Level {
    #[inline]
    fn clone(&self) -> Level { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Level { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for Level {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                Level::ReachableThroughImplTrait =>
                    "ReachableThroughImplTrait",
                Level::Reachable => "Reachable",
                Level::Reexported => "Reexported",
                Level::Direct => "Direct",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for Level {
    #[inline]
    fn eq(&self, other: &Level) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Level {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialOrd for Level {
    #[inline]
    fn partial_cmp(&self, other: &Level)
        -> ::core::option::Option<::core::cmp::Ordering> {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        ::core::cmp::PartialOrd::partial_cmp(&__self_discr, &__arg1_discr)
    }
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Ord for Level {
    #[inline]
    fn cmp(&self, other: &Level) -> ::core::cmp::Ordering {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        ::core::cmp::Ord::cmp(&__self_discr, &__arg1_discr)
    }
}Ord, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for Level {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                ::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
                match *self {
                    Level::ReachableThroughImplTrait => {}
                    Level::Reachable => {}
                    Level::Reexported => {}
                    Level::Direct => {}
                }
            }
        }
    };StableHash)]
21pub enum Level {
22    /// Superset of `Reachable` including items leaked through return position `impl Trait`.
23    ReachableThroughImplTrait,
24    /// Item is either reexported, or leaked through any kind of interface.
25    /// For example, if function `fn f() -> T {...}` is directly public, then type `T` is publicly
26    /// reachable and its values can be obtained by other crates even if the type itself is not
27    /// nameable.
28    Reachable,
29    /// Item is accessible either directly, or with help of `use` reexports.
30    Reexported,
31    /// Item is directly accessible, without help of reexports.
32    Direct,
33}
34
35impl Level {
36    pub fn all_levels() -> [Level; 4] {
37        [Level::Direct, Level::Reexported, Level::Reachable, Level::ReachableThroughImplTrait]
38    }
39}
40
41#[derive(#[automatically_derived]
impl ::core::clone::Clone for EffectiveVisibility {
    #[inline]
    fn clone(&self) -> EffectiveVisibility {
        let _: ::core::clone::AssertParamIsClone<Visibility>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for EffectiveVisibility { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for EffectiveVisibility {
    #[inline]
    fn eq(&self, other: &EffectiveVisibility) -> bool {
        self.direct == other.direct && self.reexported == other.reexported &&
                self.reachable == other.reachable &&
            self.reachable_through_impl_trait ==
                other.reachable_through_impl_trait
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for EffectiveVisibility {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Visibility>;
    }
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for EffectiveVisibility {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field4_finish(f,
            "EffectiveVisibility", "direct", &self.direct, "reexported",
            &self.reexported, "reachable", &self.reachable,
            "reachable_through_impl_trait",
            &&self.reachable_through_impl_trait)
    }
}Debug, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for
            EffectiveVisibility {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                match *self {
                    EffectiveVisibility {
                        direct: ref __binding_0,
                        reexported: ref __binding_1,
                        reachable: ref __binding_2,
                        reachable_through_impl_trait: ref __binding_3 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                        { __binding_2.stable_hash(__hcx, __hasher); }
                        { __binding_3.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
42pub struct EffectiveVisibility {
43    direct: Visibility,
44    reexported: Visibility,
45    reachable: Visibility,
46    reachable_through_impl_trait: Visibility,
47}
48
49impl EffectiveVisibility {
50    pub fn at_level(&self, level: Level) -> &Visibility {
51        match level {
52            Level::Direct => &self.direct,
53            Level::Reexported => &self.reexported,
54            Level::Reachable => &self.reachable,
55            Level::ReachableThroughImplTrait => &self.reachable_through_impl_trait,
56        }
57    }
58
59    fn at_level_mut(&mut self, level: Level) -> &mut Visibility {
60        match level {
61            Level::Direct => &mut self.direct,
62            Level::Reexported => &mut self.reexported,
63            Level::Reachable => &mut self.reachable,
64            Level::ReachableThroughImplTrait => &mut self.reachable_through_impl_trait,
65        }
66    }
67
68    pub fn public_at_level(&self) -> Option<Level> {
69        Level::all_levels().into_iter().find(|&level| self.is_public_at_level(level))
70    }
71
72    pub fn is_public_at_level(&self, level: Level) -> bool {
73        self.at_level(level).is_public()
74    }
75
76    pub const fn from_vis(vis: Visibility) -> EffectiveVisibility {
77        EffectiveVisibility {
78            direct: vis,
79            reexported: vis,
80            reachable: vis,
81            reachable_through_impl_trait: vis,
82        }
83    }
84
85    #[must_use]
86    pub fn min(mut self, lhs: EffectiveVisibility, tcx: TyCtxt<'_>) -> Self {
87        for l in Level::all_levels() {
88            let rhs_vis = self.at_level_mut(l);
89            let lhs_vis = *lhs.at_level(l);
90            // FIXME: figure out why unordered visibilities occur here,
91            // and what the behavior for them should be.
92            if rhs_vis.partial_cmp(lhs_vis, tcx) == Some(Ordering::Greater) {
93                *rhs_vis = lhs_vis;
94            };
95        }
96        self
97    }
98}
99
100/// Holds a map of effective visibilities for reachable HIR nodes.
101#[derive(#[automatically_derived]
impl<Id: ::core::clone::Clone> ::core::clone::Clone for
    EffectiveVisibilities<Id> {
    #[inline]
    fn clone(&self) -> EffectiveVisibilities<Id> {
        EffectiveVisibilities { map: ::core::clone::Clone::clone(&self.map) }
    }
}Clone, #[automatically_derived]
impl<Id: ::core::fmt::Debug> ::core::fmt::Debug for EffectiveVisibilities<Id>
    {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f,
            "EffectiveVisibilities", "map", &&self.map)
    }
}Debug)]
102pub struct EffectiveVisibilities<Id = LocalDefId> {
103    map: FxIndexMap<Id, EffectiveVisibility>,
104}
105
106impl EffectiveVisibilities {
107    pub fn is_public_at_level(&self, id: LocalDefId, level: Level) -> bool {
108        self.effective_vis(id).is_some_and(|effective_vis| effective_vis.is_public_at_level(level))
109    }
110
111    /// See `Level::Reachable`.
112    pub fn is_reachable(&self, id: LocalDefId) -> bool {
113        self.is_public_at_level(id, Level::Reachable)
114    }
115
116    /// See `Level::Reexported`.
117    pub fn is_exported(&self, id: LocalDefId) -> bool {
118        self.is_public_at_level(id, Level::Reexported)
119    }
120
121    /// See `Level::Direct`.
122    pub fn is_directly_public(&self, id: LocalDefId) -> bool {
123        self.is_public_at_level(id, Level::Direct)
124    }
125
126    pub fn public_at_level(&self, id: LocalDefId) -> Option<Level> {
127        self.effective_vis(id).and_then(|effective_vis| effective_vis.public_at_level())
128    }
129
130    pub fn update_root(&mut self) {
131        self.map.insert(CRATE_DEF_ID, EffectiveVisibility::from_vis(Visibility::Public));
132    }
133
134    // FIXME: Share code with `fn update`.
135    pub fn update_eff_vis(
136        &mut self,
137        def_id: LocalDefId,
138        eff_vis: &EffectiveVisibility,
139        tcx: TyCtxt<'_>,
140    ) {
141        match self.map.entry(def_id) {
142            IndexEntry::Occupied(mut occupied) => {
143                let old_eff_vis = occupied.get_mut();
144                for l in Level::all_levels() {
145                    let vis_at_level = eff_vis.at_level(l);
146                    let old_vis_at_level = old_eff_vis.at_level_mut(l);
147                    if vis_at_level.greater_than(*old_vis_at_level, tcx) {
148                        *old_vis_at_level = *vis_at_level
149                    }
150                }
151                old_eff_vis
152            }
153            IndexEntry::Vacant(vacant) => vacant.insert(*eff_vis),
154        };
155    }
156
157    pub fn check_invariants(&self, tcx: TyCtxt<'_>) {
158        if !truecfg!(debug_assertions) {
159            return;
160        }
161        for (&def_id, ev) in &self.map {
162            // More direct visibility levels can never go farther than less direct ones,
163            // and all effective visibilities are larger or equal than private visibility.
164            let private_vis = Visibility::Restricted(tcx.parent_module_from_def_id(def_id));
165            let span = tcx.def_span(def_id.to_def_id());
166            if private_vis.greater_than(ev.direct, tcx) {
167                crate::util::bug::span_bug_fmt(span,
    format_args!("private {0:?} > direct {1:?}", private_vis, ev.direct));span_bug!(span, "private {:?} > direct {:?}", private_vis, ev.direct);
168            }
169            if ev.direct.greater_than(ev.reexported, tcx) {
170                crate::util::bug::span_bug_fmt(span,
    format_args!("direct {0:?} > reexported {1:?}", ev.direct,
        ev.reexported));span_bug!(span, "direct {:?} > reexported {:?}", ev.direct, ev.reexported);
171            }
172            if ev.reexported.greater_than(ev.reachable, tcx) {
173                crate::util::bug::span_bug_fmt(span,
    format_args!("reexported {0:?} > reachable {1:?}", ev.reexported,
        ev.reachable));span_bug!(span, "reexported {:?} > reachable {:?}", ev.reexported, ev.reachable);
174            }
175            if ev.reachable.greater_than(ev.reachable_through_impl_trait, tcx) {
176                crate::util::bug::span_bug_fmt(span,
    format_args!("reachable {0:?} > reachable_through_impl_trait {1:?}",
        ev.reachable, ev.reachable_through_impl_trait));span_bug!(
177                    span,
178                    "reachable {:?} > reachable_through_impl_trait {:?}",
179                    ev.reachable,
180                    ev.reachable_through_impl_trait
181                );
182            }
183            // All effective visibilities except `reachable_through_impl_trait` are limited to
184            // nominal visibility. For some items nominal visibility doesn't make sense so we
185            // don't check this condition for them.
186            let is_impl = #[allow(non_exhaustive_omitted_patterns)] match tcx.def_kind(def_id) {
    DefKind::Impl { .. } => true,
    _ => false,
}matches!(tcx.def_kind(def_id), DefKind::Impl { .. });
187            if !is_impl && tcx.trait_impl_of_assoc(def_id.to_def_id()).is_none() {
188                let nominal_vis = tcx.visibility(def_id);
189                if ev.reachable.greater_than(nominal_vis, tcx) {
190                    if let Node::Item(item) = tcx.hir_node_by_def_id(def_id)
191                        && let ItemKind::Use(_, UseKind::Glob) = item.kind
192                    {
193                        // Glob import visibilities can be increased by other
194                        // more public glob imports in cases of ambiguity.
195                    } else {
196                        crate::util::bug::span_bug_fmt(span,
    format_args!("{0:?}: reachable {1:?} > nominal {2:?}", def_id,
        ev.reachable, nominal_vis));span_bug!(
197                            span,
198                            "{:?}: reachable {:?} > nominal {:?}",
199                            def_id,
200                            ev.reachable,
201                            nominal_vis,
202                        );
203                    }
204                }
205            }
206        }
207    }
208}
209
210impl<Id: Eq + Hash> EffectiveVisibilities<Id> {
211    pub fn iter(&self) -> impl Iterator<Item = (&Id, &EffectiveVisibility)> {
212        self.map.iter()
213    }
214
215    pub fn effective_vis(&self, id: Id) -> Option<&EffectiveVisibility> {
216        self.map.get(&id)
217    }
218
219    pub fn effective_vis_or_private(
220        &mut self,
221        id: Id,
222        lazy_private_vis: impl FnOnce() -> Visibility,
223    ) -> &mut EffectiveVisibility {
224        self.map.entry(id).or_insert_with(|| EffectiveVisibility::from_vis(lazy_private_vis()))
225    }
226
227    pub fn update(
228        &mut self,
229        id: Id,
230        max_vis: Option<Visibility>,
231        private_vis: Visibility,
232        inherited_effective_vis: EffectiveVisibility,
233        level: Level,
234        tcx: TyCtxt<'_>,
235    ) -> bool {
236        let mut changed = false;
237        let current_effective_vis = self.effective_vis_or_private(id, || private_vis);
238
239        let mut inherited_effective_vis_at_prev_level = *inherited_effective_vis.at_level(level);
240        let mut calculated_effective_vis = inherited_effective_vis_at_prev_level;
241        for l in Level::all_levels() {
242            if level >= l {
243                let inherited_effective_vis_at_level = *inherited_effective_vis.at_level(l);
244                let current_effective_vis_at_level = current_effective_vis.at_level_mut(l);
245                // effective visibility for id shouldn't be recalculated if
246                // inherited from parent_id effective visibility isn't changed at next level
247                if !(inherited_effective_vis_at_prev_level == inherited_effective_vis_at_level
248                    && level != l)
249                {
250                    // FIXME: figure out why unordered visibilities occur here,
251                    // and what the behavior for them should be.
252                    calculated_effective_vis = if let Some(max_vis) = max_vis
253                        && inherited_effective_vis_at_level.partial_cmp(max_vis, tcx)
254                            == Some(Ordering::Greater)
255                    {
256                        max_vis
257                    } else {
258                        inherited_effective_vis_at_level
259                    }
260                }
261                // effective visibility can't be decreased at next update call for the
262                // same id
263                // FIXME: figure out why unordered visibilities occur here,
264                // and what the behavior for them should be.
265                if calculated_effective_vis.partial_cmp(*current_effective_vis_at_level, tcx)
266                    == Some(Ordering::Greater)
267                {
268                    changed = true;
269                    *current_effective_vis_at_level = calculated_effective_vis;
270                }
271                inherited_effective_vis_at_prev_level = inherited_effective_vis_at_level;
272            }
273        }
274
275        changed
276    }
277}
278
279impl<Id> Default for EffectiveVisibilities<Id> {
280    fn default() -> Self {
281        EffectiveVisibilities { map: Default::default() }
282    }
283}
284
285impl StableHash for EffectiveVisibilities {
286    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
287        let EffectiveVisibilities { ref map } = *self;
288        map.stable_hash(hcx, hasher);
289    }
290}