Skip to content

Commit bd09b85

Browse files
committed
privacy: Make "greater than" the new base operation for comparing visibilities
1 parent 39982c1 commit bd09b85

3 files changed

Lines changed: 14 additions & 17 deletions

File tree

compiler/rustc_middle/src/middle/privacy.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl EffectiveVisibility {
8484
let lhs_vis = *lhs.at_level(l);
8585
// FIXME: figure out why unordered visibilities occur here,
8686
// and what the behavior for them should be.
87-
if rhs_vis.is_at_least_ext(lhs_vis, tcx, false) {
87+
if rhs_vis.greater_than_ext(lhs_vis, tcx, false) {
8888
*rhs_vis = lhs_vis;
8989
};
9090
}
@@ -254,13 +254,11 @@ impl<Id: Eq + Hash> EffectiveVisibilities<Id> {
254254
// same id
255255
// FIXME: figure out why unordered visibilities occur here,
256256
// and what the behavior for them should be.
257-
if *current_effective_vis_at_level != calculated_effective_vis
258-
&& calculated_effective_vis.is_at_least_ext(
259-
*current_effective_vis_at_level,
260-
tcx,
261-
false,
262-
)
263-
{
257+
if calculated_effective_vis.greater_than_ext(
258+
*current_effective_vis_at_level,
259+
tcx,
260+
false,
261+
) {
264262
changed = true;
265263
*current_effective_vis_at_level = calculated_effective_vis;
266264
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,33 +375,32 @@ impl<Id: Into<DefId>> Visibility<Id> {
375375
}
376376
}
377377

378-
/// Returns `true` if this visibility is at least as accessible as the given visibility
378+
/// Returns `true` if this visibility is strictly larger than the given visibility.
379379
#[track_caller]
380-
pub fn is_at_least_ext(
380+
pub fn greater_than_ext(
381381
self,
382382
vis: Visibility<impl Into<DefId>>,
383383
tcx: TyCtxt<'_>,
384384
assert_ordered: bool,
385385
) -> bool {
386386
match (self, vis) {
387-
(Visibility::Public, _) => true,
388387
(_, Visibility::Public) => false,
388+
(Visibility::Public, _) => true,
389389
(Visibility::Restricted(lhs_id), Visibility::Restricted(rhs_id)) => {
390390
let lhs_id = lhs_id.into();
391391
let rhs_id = rhs_id.into();
392-
let ge = tcx.is_descendant_of(rhs_id, lhs_id);
393-
if !ge && assert_ordered && !tcx.is_descendant_of(lhs_id, rhs_id) {
392+
let gt = lhs_id != rhs_id && tcx.is_descendant_of(rhs_id, lhs_id);
393+
if !gt && assert_ordered && !tcx.is_descendant_of(lhs_id, rhs_id) {
394394
bug!("unordered visibilities: {lhs_id:?} and {rhs_id:?}");
395395
}
396-
ge
396+
gt
397397
}
398398
}
399399
}
400400

401401
#[track_caller]
402402
pub fn greater_than(self, vis: Visibility<impl Into<DefId>>, tcx: TyCtxt<'_>) -> bool {
403-
let (lhs, rhs) = (self.to_def_id(), vis.to_def_id());
404-
lhs != rhs && lhs.is_at_least_ext(rhs, tcx, true)
403+
self.greater_than_ext(vis, tcx, true)
405404
}
406405
}
407406

compiler/rustc_resolve/src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
369369
let import_vis = import.vis.to_def_id();
370370
// FIXME: figure out why unordered visibilities occur here,
371371
// and what the behavior for them should be.
372-
let vis = if decl.vis().is_at_least_ext(import_vis, self.tcx, false)
372+
let vis = if decl.vis().greater_than_ext(import_vis, self.tcx, false)
373373
|| pub_use_of_private_extern_crate_hack(import, decl).is_some()
374374
{
375375
import_vis

0 commit comments

Comments
 (0)