From 9861bc8d5266288a7f09d6c64545a3d1c0bc7b89 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 18 Apr 2022 15:35:40 +0200 Subject: [PATCH] Compute has_pub_restricted in the resolver. --- compiler/rustc_middle/src/ty/mod.rs | 2 ++ compiler/rustc_privacy/src/lib.rs | 16 ++++------------ .../rustc_resolve/src/build_reduced_graph.rs | 2 ++ compiler/rustc_resolve/src/lib.rs | 5 +++++ 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 94e1e6e5987..ec416722c21 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -131,6 +131,8 @@ pub struct ResolverOutputs { pub definitions: rustc_hir::definitions::Definitions, pub cstore: Box, pub visibilities: FxHashMap, + /// This field is used to decide whether we should make `PRIVATE_IN_PUBLIC` a hard error. + pub has_pub_restricted: bool, pub access_levels: AccessLevels, pub extern_crate_map: FxHashMap, pub maybe_unused_trait_imports: FxHashSet, diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 27b42a8ac15..5f9a0357557 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -1661,7 +1661,6 @@ struct SearchInterfaceForPrivateItemsVisitor<'tcx> { item_def_id: LocalDefId, /// The visitor checks that each component type is at least this visible. required_visibility: ty::Visibility, - has_pub_restricted: bool, has_old_errors: bool, in_assoc_ty: bool, } @@ -1750,7 +1749,10 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> { }; let make_msg = || format!("{} {} `{}` in public interface", vis_descr, kind, descr); let span = self.tcx.def_span(self.item_def_id.to_def_id()); - if self.has_pub_restricted || self.has_old_errors || self.in_assoc_ty { + if self.has_old_errors + || self.in_assoc_ty + || self.tcx.resolutions(()).has_pub_restricted + { let mut err = if kind == "trait" { struct_span_err!(self.tcx.sess, span, E0445, "{}", make_msg()) } else { @@ -1809,7 +1811,6 @@ impl<'tcx> DefIdVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'tcx> { struct PrivateItemsInPublicInterfacesVisitor<'tcx> { tcx: TyCtxt<'tcx>, - has_pub_restricted: bool, old_error_set_ancestry: LocalDefIdSet, } @@ -1823,7 +1824,6 @@ impl<'tcx> PrivateItemsInPublicInterfacesVisitor<'tcx> { tcx: self.tcx, item_def_id: def_id, required_visibility, - has_pub_restricted: self.has_pub_restricted, has_old_errors: self.old_error_set_ancestry.contains(&def_id), in_assoc_ty: false, } @@ -2061,13 +2061,6 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) { }; tcx.hir().walk_toplevel_module(&mut visitor); - let has_pub_restricted = tcx.resolutions(()).visibilities.iter().any(|(&def_id, &v)| match v { - ty::Visibility::Public | ty::Visibility::Invisible => false, - ty::Visibility::Restricted(module) => { - module != tcx.parent_module_from_def_id(def_id).to_def_id() - } - }); - let mut old_error_set_ancestry = HirIdSet::default(); for mut id in visitor.old_error_set.iter().copied() { loop { @@ -2085,7 +2078,6 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) { // Check for private types and traits in public interfaces. let mut visitor = PrivateItemsInPublicInterfacesVisitor { tcx, - has_pub_restricted, // Only definition IDs are ever searched in `old_error_set_ancestry`, // so we can filter away all non-definition IDs at this point. old_error_set_ancestry: old_error_set_ancestry diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 3cb38d94009..291b6645d9a 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -265,6 +265,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { }) } ast::VisibilityKind::Restricted { ref path, id, .. } => { + // Make `PRIVATE_IN_PUBLIC` lint a hard error. + self.r.has_pub_restricted = true; // For visibilities we are not ready to provide correct implementation of "uniform // paths" right now, so on 2018 edition we only allow module-relative paths for now. // On 2015 edition visibilities are resolved as crate-relative by default, diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 4dfb7aef86f..cca1f102586 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -934,6 +934,7 @@ pub struct Resolver<'a> { glob_map: FxHashMap>, /// Visibilities in "lowered" form, for all entities that have them. visibilities: FxHashMap, + has_pub_restricted: bool, used_imports: FxHashSet, maybe_unused_trait_imports: FxHashSet, maybe_unused_extern_crates: Vec<(LocalDefId, Span)>, @@ -1313,6 +1314,7 @@ impl<'a> Resolver<'a> { glob_map: Default::default(), visibilities, + has_pub_restricted: false, used_imports: FxHashSet::default(), maybe_unused_trait_imports: Default::default(), maybe_unused_extern_crates: Vec::new(), @@ -1423,6 +1425,7 @@ impl<'a> Resolver<'a> { let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect(); let definitions = self.definitions; let visibilities = self.visibilities; + let has_pub_restricted = self.has_pub_restricted; let extern_crate_map = self.extern_crate_map; let reexport_map = self.reexport_map; let maybe_unused_trait_imports = self.maybe_unused_trait_imports; @@ -1435,6 +1438,7 @@ impl<'a> Resolver<'a> { definitions, cstore: Box::new(self.crate_loader.into_cstore()), visibilities, + has_pub_restricted, access_levels, extern_crate_map, reexport_map, @@ -1461,6 +1465,7 @@ impl<'a> Resolver<'a> { access_levels: self.access_levels.clone(), cstore: Box::new(self.cstore().clone()), visibilities: self.visibilities.clone(), + has_pub_restricted: self.has_pub_restricted, extern_crate_map: self.extern_crate_map.clone(), reexport_map: self.reexport_map.clone(), glob_map: self.glob_map.clone(),