Skip to content

Commit 9861bc8

Browse files
committed
Compute has_pub_restricted in the resolver.
1 parent 88de3e5 commit 9861bc8

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

compiler/rustc_middle/src/ty/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ pub struct ResolverOutputs {
131131
pub definitions: rustc_hir::definitions::Definitions,
132132
pub cstore: Box<CrateStoreDyn>,
133133
pub visibilities: FxHashMap<LocalDefId, Visibility>,
134+
/// This field is used to decide whether we should make `PRIVATE_IN_PUBLIC` a hard error.
135+
pub has_pub_restricted: bool,
134136
pub access_levels: AccessLevels,
135137
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
136138
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,

compiler/rustc_privacy/src/lib.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,6 @@ struct SearchInterfaceForPrivateItemsVisitor<'tcx> {
16611661
item_def_id: LocalDefId,
16621662
/// The visitor checks that each component type is at least this visible.
16631663
required_visibility: ty::Visibility,
1664-
has_pub_restricted: bool,
16651664
has_old_errors: bool,
16661665
in_assoc_ty: bool,
16671666
}
@@ -1750,7 +1749,10 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17501749
};
17511750
let make_msg = || format!("{} {} `{}` in public interface", vis_descr, kind, descr);
17521751
let span = self.tcx.def_span(self.item_def_id.to_def_id());
1753-
if self.has_pub_restricted || self.has_old_errors || self.in_assoc_ty {
1752+
if self.has_old_errors
1753+
|| self.in_assoc_ty
1754+
|| self.tcx.resolutions(()).has_pub_restricted
1755+
{
17541756
let mut err = if kind == "trait" {
17551757
struct_span_err!(self.tcx.sess, span, E0445, "{}", make_msg())
17561758
} else {
@@ -1809,7 +1811,6 @@ impl<'tcx> DefIdVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'tcx> {
18091811

18101812
struct PrivateItemsInPublicInterfacesVisitor<'tcx> {
18111813
tcx: TyCtxt<'tcx>,
1812-
has_pub_restricted: bool,
18131814
old_error_set_ancestry: LocalDefIdSet,
18141815
}
18151816

@@ -1823,7 +1824,6 @@ impl<'tcx> PrivateItemsInPublicInterfacesVisitor<'tcx> {
18231824
tcx: self.tcx,
18241825
item_def_id: def_id,
18251826
required_visibility,
1826-
has_pub_restricted: self.has_pub_restricted,
18271827
has_old_errors: self.old_error_set_ancestry.contains(&def_id),
18281828
in_assoc_ty: false,
18291829
}
@@ -2061,13 +2061,6 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) {
20612061
};
20622062
tcx.hir().walk_toplevel_module(&mut visitor);
20632063

2064-
let has_pub_restricted = tcx.resolutions(()).visibilities.iter().any(|(&def_id, &v)| match v {
2065-
ty::Visibility::Public | ty::Visibility::Invisible => false,
2066-
ty::Visibility::Restricted(module) => {
2067-
module != tcx.parent_module_from_def_id(def_id).to_def_id()
2068-
}
2069-
});
2070-
20712064
let mut old_error_set_ancestry = HirIdSet::default();
20722065
for mut id in visitor.old_error_set.iter().copied() {
20732066
loop {
@@ -2085,7 +2078,6 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) {
20852078
// Check for private types and traits in public interfaces.
20862079
let mut visitor = PrivateItemsInPublicInterfacesVisitor {
20872080
tcx,
2088-
has_pub_restricted,
20892081
// Only definition IDs are ever searched in `old_error_set_ancestry`,
20902082
// so we can filter away all non-definition IDs at this point.
20912083
old_error_set_ancestry: old_error_set_ancestry

compiler/rustc_resolve/src/build_reduced_graph.rs

+2
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
265265
})
266266
}
267267
ast::VisibilityKind::Restricted { ref path, id, .. } => {
268+
// Make `PRIVATE_IN_PUBLIC` lint a hard error.
269+
self.r.has_pub_restricted = true;
268270
// For visibilities we are not ready to provide correct implementation of "uniform
269271
// paths" right now, so on 2018 edition we only allow module-relative paths for now.
270272
// On 2015 edition visibilities are resolved as crate-relative by default,

compiler/rustc_resolve/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,7 @@ pub struct Resolver<'a> {
934934
glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
935935
/// Visibilities in "lowered" form, for all entities that have them.
936936
visibilities: FxHashMap<LocalDefId, ty::Visibility>,
937+
has_pub_restricted: bool,
937938
used_imports: FxHashSet<NodeId>,
938939
maybe_unused_trait_imports: FxHashSet<LocalDefId>,
939940
maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
@@ -1313,6 +1314,7 @@ impl<'a> Resolver<'a> {
13131314

13141315
glob_map: Default::default(),
13151316
visibilities,
1317+
has_pub_restricted: false,
13161318
used_imports: FxHashSet::default(),
13171319
maybe_unused_trait_imports: Default::default(),
13181320
maybe_unused_extern_crates: Vec::new(),
@@ -1423,6 +1425,7 @@ impl<'a> Resolver<'a> {
14231425
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
14241426
let definitions = self.definitions;
14251427
let visibilities = self.visibilities;
1428+
let has_pub_restricted = self.has_pub_restricted;
14261429
let extern_crate_map = self.extern_crate_map;
14271430
let reexport_map = self.reexport_map;
14281431
let maybe_unused_trait_imports = self.maybe_unused_trait_imports;
@@ -1435,6 +1438,7 @@ impl<'a> Resolver<'a> {
14351438
definitions,
14361439
cstore: Box::new(self.crate_loader.into_cstore()),
14371440
visibilities,
1441+
has_pub_restricted,
14381442
access_levels,
14391443
extern_crate_map,
14401444
reexport_map,
@@ -1461,6 +1465,7 @@ impl<'a> Resolver<'a> {
14611465
access_levels: self.access_levels.clone(),
14621466
cstore: Box::new(self.cstore().clone()),
14631467
visibilities: self.visibilities.clone(),
1468+
has_pub_restricted: self.has_pub_restricted,
14641469
extern_crate_map: self.extern_crate_map.clone(),
14651470
reexport_map: self.reexport_map.clone(),
14661471
glob_map: self.glob_map.clone(),

0 commit comments

Comments
 (0)