Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ad6ab11

Browse files
committedMay 14, 2023
Auto merge of rust-lang#111425 - Bryanskiy:privacy_ef, r=petrochenkov
Populate effective visibilities in `rustc_privacy` (take 2) Same as rust-lang#110907 + regressions fixes. Fixes rust-lang#111359. r? `@petrochenkov`
2 parents 2e18605 + 670f5b1 commit ad6ab11

File tree

7 files changed

+243
-147
lines changed

7 files changed

+243
-147
lines changed
 

‎compiler/rustc_lint/src/builtin.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,9 @@ declare_lint_pass!(MissingCopyImplementations => [MISSING_COPY_IMPLEMENTATIONS])
647647

648648
impl<'tcx> LateLintPass<'tcx> for MissingCopyImplementations {
649649
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
650-
if !cx.effective_visibilities.is_reachable(item.owner_id.def_id) {
650+
if !(cx.effective_visibilities.is_reachable(item.owner_id.def_id)
651+
&& cx.tcx.local_visibility(item.owner_id.def_id).is_public())
652+
{
651653
return;
652654
}
653655
let (def, ty) = match item.kind {
@@ -766,7 +768,9 @@ impl_lint_pass!(MissingDebugImplementations => [MISSING_DEBUG_IMPLEMENTATIONS]);
766768

767769
impl<'tcx> LateLintPass<'tcx> for MissingDebugImplementations {
768770
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
769-
if !cx.effective_visibilities.is_reachable(item.owner_id.def_id) {
771+
if !(cx.effective_visibilities.is_reachable(item.owner_id.def_id)
772+
&& cx.tcx.local_visibility(item.owner_id.def_id).is_public())
773+
{
770774
return;
771775
}
772776

‎compiler/rustc_middle/src/middle/privacy.rs

+19-26
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,26 @@ impl EffectiveVisibility {
6464
self.at_level(level).is_public()
6565
}
6666

67-
pub fn from_vis(vis: Visibility) -> EffectiveVisibility {
67+
pub const fn from_vis(vis: Visibility) -> EffectiveVisibility {
6868
EffectiveVisibility {
6969
direct: vis,
7070
reexported: vis,
7171
reachable: vis,
7272
reachable_through_impl_trait: vis,
7373
}
7474
}
75+
76+
#[must_use]
77+
pub fn min(mut self, lhs: EffectiveVisibility, tcx: TyCtxt<'_>) -> Self {
78+
for l in Level::all_levels() {
79+
let rhs_vis = self.at_level_mut(l);
80+
let lhs_vis = *lhs.at_level(l);
81+
if rhs_vis.is_at_least(lhs_vis, tcx) {
82+
*rhs_vis = lhs_vis;
83+
};
84+
}
85+
self
86+
}
7587
}
7688

7789
/// Holds a map of effective visibilities for reachable HIR nodes.
@@ -137,24 +149,6 @@ impl EffectiveVisibilities {
137149
};
138150
}
139151

140-
pub fn set_public_at_level(
141-
&mut self,
142-
id: LocalDefId,
143-
lazy_private_vis: impl FnOnce() -> Visibility,
144-
level: Level,
145-
) {
146-
let mut effective_vis = self
147-
.effective_vis(id)
148-
.copied()
149-
.unwrap_or_else(|| EffectiveVisibility::from_vis(lazy_private_vis()));
150-
for l in Level::all_levels() {
151-
if l <= level {
152-
*effective_vis.at_level_mut(l) = Visibility::Public;
153-
}
154-
}
155-
self.map.insert(id, effective_vis);
156-
}
157-
158152
pub fn check_invariants(&self, tcx: TyCtxt<'_>, early: bool) {
159153
if !cfg!(debug_assertions) {
160154
return;
@@ -219,7 +213,7 @@ impl<Id: Eq + Hash> EffectiveVisibilities<Id> {
219213
pub fn update(
220214
&mut self,
221215
id: Id,
222-
nominal_vis: Visibility,
216+
nominal_vis: Option<Visibility>,
223217
lazy_private_vis: impl FnOnce() -> Visibility,
224218
inherited_effective_vis: EffectiveVisibility,
225219
level: Level,
@@ -243,12 +237,11 @@ impl<Id: Eq + Hash> EffectiveVisibilities<Id> {
243237
if !(inherited_effective_vis_at_prev_level == inherited_effective_vis_at_level
244238
&& level != l)
245239
{
246-
calculated_effective_vis =
247-
if nominal_vis.is_at_least(inherited_effective_vis_at_level, tcx) {
248-
inherited_effective_vis_at_level
249-
} else {
250-
nominal_vis
251-
};
240+
calculated_effective_vis = if let Some(nominal_vis) = nominal_vis && !nominal_vis.is_at_least(inherited_effective_vis_at_level, tcx) {
241+
nominal_vis
242+
} else {
243+
inherited_effective_vis_at_level
244+
}
252245
}
253246
// effective visibility can't be decreased at next update call for the
254247
// same id

0 commit comments

Comments
 (0)
Please sign in to comment.