Skip to main content

rustc_resolve/
effective_visibilities.rs

1use std::mem;
2
3use rustc_ast::visit::Visitor;
4use rustc_ast::{Attribute, Crate, EnumDef, ast, visit};
5use rustc_data_structures::fx::FxHashSet;
6use rustc_hir::def::{DefKind, Res};
7use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
8use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility, Level};
9use rustc_middle::ty::Visibility;
10use rustc_span::sym;
11use tracing::info;
12
13use crate::{Decl, DeclKind, Resolver};
14
15#[derive(#[automatically_derived]
impl<'ra> ::core::clone::Clone for ParentId<'ra> {
    #[inline]
    fn clone(&self) -> ParentId<'ra> {
        let _: ::core::clone::AssertParamIsClone<LocalDefId>;
        let _: ::core::clone::AssertParamIsClone<Decl<'ra>>;
        *self
    }
}Clone, #[automatically_derived]
impl<'ra> ::core::marker::Copy for ParentId<'ra> { }Copy)]
16enum ParentId<'ra> {
17    Def(LocalDefId),
18    Import(Decl<'ra>),
19}
20
21impl ParentId<'_> {
22    fn level(self) -> Level {
23        match self {
24            ParentId::Def(_) => Level::Direct,
25            ParentId::Import(_) => Level::Reexported,
26        }
27    }
28}
29
30pub(crate) struct EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
31    r: &'a mut Resolver<'ra, 'tcx>,
32    def_effective_visibilities: EffectiveVisibilities,
33    /// While walking import chains we need to track effective visibilities per-decl, and def id
34    /// keys in `Resolver::effective_visibilities` are not enough for that, because multiple
35    /// declarations can correspond to a single def id in imports. So we keep a separate table.
36    import_effective_visibilities: EffectiveVisibilities<Decl<'ra>>,
37    // It's possible to recalculate this at any point, but it's relatively expensive.
38    current_private_vis: Visibility,
39    /// A set of pairs corresponding to modules, where the first module is
40    /// reachable via a macro that's defined in the second module. This cannot
41    /// be represented as reachable because it can't handle the following case:
42    ///
43    /// pub mod n {                         // Should be `Public`
44    ///     pub(crate) mod p {              // Should *not* be accessible
45    ///         pub fn f() -> i32 { 12 }    // Must be `Reachable`
46    ///     }
47    /// }
48    /// pub macro m() {
49    ///     n::p::f()
50    /// }
51    macro_reachable: FxHashSet<(LocalDefId, LocalDefId)>,
52    changed: bool,
53}
54
55impl Resolver<'_, '_> {
56    fn private_vis_decl(&self, decl: Decl<'_>) -> Visibility {
57        Visibility::Restricted(
58            decl.parent_module.map_or(CRATE_DEF_ID, |m| m.nearest_parent_mod().expect_local()),
59        )
60    }
61
62    fn private_vis_def(&self, def_id: LocalDefId) -> Visibility {
63        // For mod items `normal_mod_id` will be equal to `def_id`, but we actually need its parent.
64        let normal_mod_id = self
65            .get_nearest_non_block_module(def_id.to_def_id())
66            .nearest_parent_mod()
67            .expect_local();
68        if normal_mod_id == def_id {
69            Visibility::Restricted(self.tcx.local_parent(def_id))
70        } else {
71            Visibility::Restricted(normal_mod_id)
72        }
73    }
74}
75
76impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
77    /// Fills the `Resolver::effective_visibilities` table with public & exported items
78    /// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
79    /// need access to a TyCtxt for that. Returns the set of ambiguous re-exports.
80    pub(crate) fn compute_effective_visibilities<'c>(
81        r: &'a mut Resolver<'ra, 'tcx>,
82        krate: &'c Crate,
83    ) -> FxHashSet<Decl<'ra>> {
84        let mut visitor = EffectiveVisibilitiesVisitor {
85            r,
86            def_effective_visibilities: Default::default(),
87            import_effective_visibilities: Default::default(),
88            current_private_vis: Visibility::Restricted(CRATE_DEF_ID),
89            macro_reachable: Default::default(),
90            changed: true,
91        };
92
93        visitor.def_effective_visibilities.update_root();
94        visitor.set_bindings_effective_visibilities(CRATE_DEF_ID);
95
96        while visitor.changed {
97            visitor.changed = false;
98            visit::walk_crate(&mut visitor, krate);
99        }
100        visitor.r.effective_visibilities = visitor.def_effective_visibilities;
101
102        let mut exported_ambiguities = FxHashSet::default();
103
104        // Update visibilities for import def ids. These are not used during the
105        // `EffectiveVisibilitiesVisitor` pass, because we have more detailed declaration-based
106        // information, but are used by later passes. Effective visibility of an import def id
107        // is the maximum value among visibilities of declarations corresponding to that def id.
108        for (decl, eff_vis) in visitor.import_effective_visibilities.iter() {
109            let DeclKind::Import { import, .. } = decl.kind else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
110            if let Some(def_id) = import.def_id() {
111                r.effective_visibilities.update_eff_vis(def_id, eff_vis, r.tcx)
112            }
113            if decl.ambiguity.get().is_some() && eff_vis.is_public_at_level(Level::Reexported) {
114                exported_ambiguities.insert(*decl);
115            }
116        }
117
118        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/effective_visibilities.rs:118",
                        "rustc_resolve::effective_visibilities",
                        ::tracing::Level::INFO,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/effective_visibilities.rs"),
                        ::tracing_core::__macro_support::Option::Some(118u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::effective_visibilities"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::INFO <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("resolve::effective_visibilities: {0:#?}",
                                                    r.effective_visibilities) as &dyn Value))])
            });
    } else { ; }
};info!("resolve::effective_visibilities: {:#?}", r.effective_visibilities);
119
120        exported_ambiguities
121    }
122
123    /// Update effective visibilities of name declarations in the given module,
124    /// including their whole reexport chains.
125    fn set_bindings_effective_visibilities(&mut self, module_id: LocalDefId) {
126        let module = self.r.expect_module(module_id.to_def_id());
127        for (_, name_resolution) in self.r.resolutions(module).borrow().iter() {
128            let Some(mut decl) = name_resolution.borrow().best_decl() else {
129                continue;
130            };
131            // Set the given effective visibility level to `Level::Direct` and
132            // sets the rest of the `use` chain to `Level::Reexported` until
133            // we hit the actual exported item.
134            let priv_vis = |this: &Self, parent_id, decl| match parent_id {
135                ParentId::Def(_) => this.current_private_vis,
136                ParentId::Import(_) => this.r.private_vis_decl(decl),
137            };
138            let mut parent_id = ParentId::Def(module_id);
139            while let DeclKind::Import { source_decl, .. } = decl.kind {
140                self.update_import(decl, parent_id, priv_vis(self, parent_id, decl));
141                parent_id = ParentId::Import(decl);
142                decl = source_decl;
143            }
144            if let Some(def_id) = decl.res().opt_def_id().and_then(|id| id.as_local()) {
145                let priv_vis = priv_vis(self, parent_id, decl);
146                self.update_def(def_id, decl.vis().expect_local(), parent_id, priv_vis);
147            }
148        }
149    }
150
151    fn effective_vis_or_private(&mut self, parent_id: ParentId<'ra>) -> EffectiveVisibility {
152        // Private nodes are only added to the table for caching, they could be added or removed at
153        // any moment without consequences, so we don't set `changed` to true when adding them.
154        *match parent_id {
155            ParentId::Def(def_id) => self
156                .def_effective_visibilities
157                .effective_vis_or_private(def_id, || self.r.private_vis_def(def_id)),
158            ParentId::Import(binding) => self
159                .import_effective_visibilities
160                .effective_vis_or_private(binding, || self.r.private_vis_decl(binding)),
161        }
162    }
163
164    /// All effective visibilities for a node are larger or equal than private visibility
165    /// for that node (see `check_invariants` in middle/privacy.rs).
166    /// So if either parent or nominal visibility is the same as private visibility, then
167    /// `min(parent_vis, nominal_vis) <= priv_vis`, and the update logic is guaranteed
168    /// to not update anything and we can skip it.
169    fn may_update(
170        &self,
171        nominal_vis: Visibility,
172        parent_id: ParentId<'_>,
173        priv_vis: Visibility,
174    ) -> bool {
175        nominal_vis != priv_vis
176            && match parent_id {
177                ParentId::Def(def_id) => self.r.tcx.local_visibility(def_id),
178                ParentId::Import(decl) => decl.vis().expect_local(),
179            } != priv_vis
180    }
181
182    fn update_import(&mut self, decl: Decl<'ra>, parent_id: ParentId<'ra>, priv_vis: Visibility) {
183        let nominal_vis = decl.vis().expect_local();
184        if !self.may_update(nominal_vis, parent_id, priv_vis) {
185            return;
186        };
187        let inherited_eff_vis = self.effective_vis_or_private(parent_id);
188        let tcx = self.r.tcx;
189        self.changed |= self.import_effective_visibilities.update(
190            decl,
191            Some(nominal_vis),
192            priv_vis,
193            inherited_eff_vis,
194            parent_id.level(),
195            tcx,
196        );
197        if let Some(max_vis_decl) = decl.ambiguity_vis_max.get() {
198            // Avoid the most visible import in an ambiguous glob set being reported as unused.
199            self.update_import(max_vis_decl, parent_id, priv_vis);
200        }
201    }
202
203    fn update_def(
204        &mut self,
205        def_id: LocalDefId,
206        nominal_vis: Visibility,
207        parent_id: ParentId<'ra>,
208        priv_vis: Visibility,
209    ) {
210        if !self.may_update(nominal_vis, parent_id, priv_vis) {
211            return;
212        };
213        let inherited_eff_vis = self.effective_vis_or_private(parent_id);
214        let tcx = self.r.tcx;
215        self.changed |= self.def_effective_visibilities.update(
216            def_id,
217            Some(nominal_vis),
218            priv_vis,
219            inherited_eff_vis,
220            parent_id.level(),
221            tcx,
222        );
223    }
224
225    fn update_field(&mut self, def_id: LocalDefId, parent_id: LocalDefId) {
226        let nominal_vis = self.r.tcx.local_visibility(def_id);
227        self.update_def(def_id, nominal_vis, ParentId::Def(parent_id), self.current_private_vis);
228    }
229
230    fn update_macro(&mut self, def_id: LocalDefId, inherited_effective_vis: EffectiveVisibility) {
231        let max_vis = Some(self.r.tcx.local_visibility(def_id));
232        let priv_vis = if def_id == CRATE_DEF_ID {
233            Visibility::Restricted(CRATE_DEF_ID)
234        } else {
235            self.r.private_vis_def(def_id)
236        };
237        self.changed |= self.def_effective_visibilities.update(
238            def_id,
239            max_vis,
240            priv_vis,
241            inherited_effective_vis,
242            Level::Reachable,
243            self.r.tcx,
244        );
245    }
246
247    // We have to make sure that the items that macros might reference
248    // are reachable, since they might be exported transitively.
249    fn update_reachability_from_macro(
250        &mut self,
251        local_def_id: LocalDefId,
252        md: &ast::MacroDef,
253        attrs: &[Attribute],
254    ) {
255        // Non-opaque macros cannot make other items more accessible than they already are.
256        if rustc_ast::attr::find_by_name(attrs, sym::rustc_macro_transparency)
257            .map_or(md.macro_rules, |attr| attr.value_str() != Some(sym::opaque))
258        {
259            return;
260        }
261
262        let macro_module_def_id = self.r.tcx.local_parent(local_def_id);
263        if self.r.tcx.def_kind(macro_module_def_id) != DefKind::Mod {
264            // The macro's parent doesn't correspond to a `mod`, return early (#63164, #65252).
265            return;
266        }
267
268        let Some(macro_ev) = self
269            .def_effective_visibilities
270            .effective_vis(local_def_id)
271            .filter(|ev| ev.public_at_level().is_some())
272            .copied()
273        else {
274            return;
275        };
276
277        // Since we are starting from an externally visible module,
278        // all the parents in the loop below are also guaranteed to be modules.
279        let mut module_def_id = macro_module_def_id;
280        loop {
281            let changed_reachability =
282                self.update_macro_reachable(module_def_id, macro_module_def_id, macro_ev);
283            if changed_reachability || module_def_id == CRATE_DEF_ID {
284                break;
285            }
286            module_def_id = self.r.tcx.local_parent(module_def_id);
287        }
288    }
289
290    /// Updates the item as being reachable through a macro defined in the given
291    /// module. Returns `true` if the level has changed.
292    fn update_macro_reachable(
293        &mut self,
294        module_def_id: LocalDefId,
295        defining_mod: LocalDefId,
296        macro_ev: EffectiveVisibility,
297    ) -> bool {
298        if self.macro_reachable.insert((module_def_id, defining_mod)) {
299            let module = self.r.expect_module(module_def_id.to_def_id());
300            for (_, name_resolution) in self.r.resolutions(module).borrow().iter() {
301                let Some(decl) = name_resolution.borrow().best_decl() else {
302                    continue;
303                };
304
305                if let Res::Def(def_kind, def_id) = decl.res()
306                    && let Some(def_id) = def_id.as_local()
307                    // FIXME: defs should be checked with `EffectiveVisibilities::is_reachable`.
308                    && decl.vis().is_accessible_from(defining_mod, self.r.tcx)
309                {
310                    let vis = self.r.tcx.local_visibility(def_id);
311                    self.update_macro_reachable_def(def_id, def_kind, vis, defining_mod, macro_ev);
312                }
313            }
314            true
315        } else {
316            false
317        }
318    }
319
320    fn update_macro_reachable_def(
321        &mut self,
322        def_id: LocalDefId,
323        def_kind: DefKind,
324        vis: Visibility,
325        module: LocalDefId,
326        macro_ev: EffectiveVisibility,
327    ) {
328        self.update_macro(def_id, macro_ev);
329
330        match def_kind {
331            DefKind::Mod => {
332                if vis.is_accessible_from(module, self.r.tcx) {
333                    self.update_macro_reachable(def_id, module, macro_ev);
334                }
335            }
336            DefKind::Struct | DefKind::Union => {
337                self.r
338                    .macro_reachable_adts
339                    .entry(def_id)
340                    .or_insert_with(Default::default)
341                    .insert(module);
342            }
343            _ => {}
344        }
345    }
346}
347
348impl<'a, 'ra, 'tcx> Visitor<'a> for EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
349    fn visit_item(&mut self, item: &'a ast::Item) {
350        let def_id = self.r.owner_def_id(item.id);
351        // Update effective visibilities of nested items.
352        // If it's a mod, also make the visitor walk all of its items
353        match &item.kind {
354            // Resolved in rustc_privacy when types are available
355            ast::ItemKind::Impl(..) => return,
356
357            // Should be unreachable at this stage
358            ast::ItemKind::MacCall(..) | ast::ItemKind::DelegationMac(..) => {
    ::core::panicking::panic_fmt(format_args!("ast::ItemKind::MacCall encountered, this should not anymore appear at this stage"));
}panic!(
359                "ast::ItemKind::MacCall encountered, this should not anymore appear at this stage"
360            ),
361
362            ast::ItemKind::Mod(..) => {
363                let prev_private_vis =
364                    mem::replace(&mut self.current_private_vis, Visibility::Restricted(def_id));
365                self.set_bindings_effective_visibilities(def_id);
366                visit::walk_item(self, item);
367                self.current_private_vis = prev_private_vis;
368            }
369
370            ast::ItemKind::Enum(_, _, EnumDef { variants }) => {
371                self.set_bindings_effective_visibilities(def_id);
372                for variant in variants {
373                    let variant_def_id = self.r.child_def_id(item.id, variant.id);
374                    for field in variant.data.fields() {
375                        self.update_field(self.r.child_def_id(item.id, field.id), variant_def_id);
376                    }
377                }
378            }
379
380            ast::ItemKind::Struct(_, _, def) | ast::ItemKind::Union(_, _, def) => {
381                for field in def.fields() {
382                    self.update_field(self.r.child_def_id(item.id, field.id), def_id);
383                }
384            }
385
386            ast::ItemKind::Trait(..) => {
387                self.set_bindings_effective_visibilities(def_id);
388            }
389
390            ast::ItemKind::MacroDef(_, macro_def) => {
391                self.update_reachability_from_macro(def_id, macro_def, &item.attrs);
392            }
393
394            ast::ItemKind::ExternCrate(..)
395            | ast::ItemKind::Use(..)
396            | ast::ItemKind::Static(..)
397            | ast::ItemKind::Const(..)
398            | ast::ItemKind::ConstBlock(..)
399            | ast::ItemKind::GlobalAsm(..)
400            | ast::ItemKind::TyAlias(..)
401            | ast::ItemKind::TraitAlias(..)
402            | ast::ItemKind::ForeignMod(..)
403            | ast::ItemKind::Fn(..)
404            | ast::ItemKind::Delegation(..) => return,
405        }
406    }
407}