Skip to content

Commit 9838411

Browse files
committed
Auto merge of #155257 - petrochenkov:visatleast, r=adwinwhite
privacy: Assert that compared visibilities are (usually) ordered And make "greater than" (`>`) the new primary operation for comparing visibilities instead of "is at least" (`>=`).
2 parents fb76025 + 714df2b commit 9838411

6 files changed

Lines changed: 77 additions & 44 deletions

File tree

compiler/rustc_hir_analysis/src/coherence/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn visit_implementation_of_const_param_ty(checker: &Checker<'_>) -> Result<(), E
190190
let struct_vis = tcx.visibility(adt.did());
191191
for variant in adt.variants() {
192192
for field in &variant.fields {
193-
if !field.vis.is_at_least(struct_vis, tcx) {
193+
if struct_vis.greater_than(field.vis, tcx) {
194194
let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
195195
return Err(tcx
196196
.dcx()

compiler/rustc_middle/src/middle/privacy.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! outside their scopes. This pass will also generate a set of exported items
33
//! which are available for use externally when compiled as a library.
44
5+
use std::cmp::Ordering;
56
use std::hash::Hash;
67

78
use rustc_data_structures::fx::{FxIndexMap, IndexEntry};
@@ -82,7 +83,9 @@ impl EffectiveVisibility {
8283
for l in Level::all_levels() {
8384
let rhs_vis = self.at_level_mut(l);
8485
let lhs_vis = *lhs.at_level(l);
85-
if rhs_vis.is_at_least(lhs_vis, tcx) {
86+
// FIXME: figure out why unordered visibilities occur here,
87+
// and what the behavior for them should be.
88+
if rhs_vis.partial_cmp(lhs_vis, tcx) == Some(Ordering::Greater) {
8689
*rhs_vis = lhs_vis;
8790
};
8891
}
@@ -139,9 +142,7 @@ impl EffectiveVisibilities {
139142
for l in Level::all_levels() {
140143
let vis_at_level = eff_vis.at_level(l);
141144
let old_vis_at_level = old_eff_vis.at_level_mut(l);
142-
if vis_at_level != old_vis_at_level
143-
&& vis_at_level.is_at_least(*old_vis_at_level, tcx)
144-
{
145+
if vis_at_level.greater_than(*old_vis_at_level, tcx) {
145146
*old_vis_at_level = *vis_at_level
146147
}
147148
}
@@ -160,16 +161,16 @@ impl EffectiveVisibilities {
160161
// and all effective visibilities are larger or equal than private visibility.
161162
let private_vis = Visibility::Restricted(tcx.parent_module_from_def_id(def_id));
162163
let span = tcx.def_span(def_id.to_def_id());
163-
if !ev.direct.is_at_least(private_vis, tcx) {
164+
if private_vis.greater_than(ev.direct, tcx) {
164165
span_bug!(span, "private {:?} > direct {:?}", private_vis, ev.direct);
165166
}
166-
if !ev.reexported.is_at_least(ev.direct, tcx) {
167+
if ev.direct.greater_than(ev.reexported, tcx) {
167168
span_bug!(span, "direct {:?} > reexported {:?}", ev.direct, ev.reexported);
168169
}
169-
if !ev.reachable.is_at_least(ev.reexported, tcx) {
170+
if ev.reexported.greater_than(ev.reachable, tcx) {
170171
span_bug!(span, "reexported {:?} > reachable {:?}", ev.reexported, ev.reachable);
171172
}
172-
if !ev.reachable_through_impl_trait.is_at_least(ev.reachable, tcx) {
173+
if ev.reachable.greater_than(ev.reachable_through_impl_trait, tcx) {
173174
span_bug!(
174175
span,
175176
"reachable {:?} > reachable_through_impl_trait {:?}",
@@ -183,7 +184,7 @@ impl EffectiveVisibilities {
183184
let is_impl = matches!(tcx.def_kind(def_id), DefKind::Impl { .. });
184185
if !is_impl && tcx.trait_impl_of_assoc(def_id.to_def_id()).is_none() {
185186
let nominal_vis = tcx.visibility(def_id);
186-
if !nominal_vis.is_at_least(ev.reachable, tcx) {
187+
if ev.reachable.greater_than(nominal_vis, tcx) {
187188
span_bug!(
188189
span,
189190
"{:?}: reachable {:?} > nominal {:?}",
@@ -242,8 +243,11 @@ impl<Id: Eq + Hash> EffectiveVisibilities<Id> {
242243
if !(inherited_effective_vis_at_prev_level == inherited_effective_vis_at_level
243244
&& level != l)
244245
{
246+
// FIXME: figure out why unordered visibilities occur here,
247+
// and what the behavior for them should be.
245248
calculated_effective_vis = if let Some(max_vis) = max_vis
246-
&& !max_vis.is_at_least(inherited_effective_vis_at_level, tcx)
249+
&& inherited_effective_vis_at_level.partial_cmp(max_vis, tcx)
250+
== Some(Ordering::Greater)
247251
{
248252
max_vis
249253
} else {
@@ -252,8 +256,10 @@ impl<Id: Eq + Hash> EffectiveVisibilities<Id> {
252256
}
253257
// effective visibility can't be decreased at next update call for the
254258
// same id
255-
if *current_effective_vis_at_level != calculated_effective_vis
256-
&& calculated_effective_vis.is_at_least(*current_effective_vis_at_level, tcx)
259+
// FIXME: figure out why unordered visibilities occur here,
260+
// and what the behavior for them should be.
261+
if calculated_effective_vis.partial_cmp(*current_effective_vis_at_level, tcx)
262+
== Some(Ordering::Greater)
257263
{
258264
changed = true;
259265
*current_effective_vis_at_level = calculated_effective_vis;

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
#![allow(rustc::usage_of_ty_tykind)]
1313

14+
use std::cmp::Ordering;
1415
use std::fmt::Debug;
1516
use std::hash::{Hash, Hasher};
1617
use std::marker::PhantomData;
@@ -379,18 +380,46 @@ impl<Id: Into<DefId>> Visibility<Id> {
379380
}
380381
}
381382

382-
/// Returns `true` if this visibility is at least as accessible as the given visibility
383-
pub fn is_at_least(self, vis: Visibility<impl Into<DefId>>, tcx: TyCtxt<'_>) -> bool {
384-
match vis {
385-
Visibility::Public => self.is_public(),
386-
Visibility::Restricted(id) => self.is_accessible_from(id, tcx),
383+
pub fn partial_cmp(
384+
self,
385+
vis: Visibility<impl Into<DefId>>,
386+
tcx: TyCtxt<'_>,
387+
) -> Option<Ordering> {
388+
match (self, vis) {
389+
(Visibility::Public, Visibility::Public) => Some(Ordering::Equal),
390+
(Visibility::Public, Visibility::Restricted(_)) => Some(Ordering::Greater),
391+
(Visibility::Restricted(_), Visibility::Public) => Some(Ordering::Less),
392+
(Visibility::Restricted(lhs_id), Visibility::Restricted(rhs_id)) => {
393+
let (lhs_id, rhs_id) = (lhs_id.into(), rhs_id.into());
394+
if lhs_id == rhs_id {
395+
Some(Ordering::Equal)
396+
} else if tcx.is_descendant_of(rhs_id, lhs_id) {
397+
Some(Ordering::Greater)
398+
} else if tcx.is_descendant_of(lhs_id, rhs_id) {
399+
Some(Ordering::Less)
400+
} else {
401+
None
402+
}
403+
}
387404
}
388405
}
389406
}
390407

391-
impl<Id: Into<DefId> + Copy> Visibility<Id> {
392-
pub fn min(self, vis: Visibility<Id>, tcx: TyCtxt<'_>) -> Visibility<Id> {
393-
if self.is_at_least(vis, tcx) { vis } else { self }
408+
impl<Id: Into<DefId> + Debug + Copy> Visibility<Id> {
409+
/// Returns `true` if this visibility is strictly larger than the given visibility.
410+
#[track_caller]
411+
pub fn greater_than(
412+
self,
413+
vis: Visibility<impl Into<DefId> + Debug + Copy>,
414+
tcx: TyCtxt<'_>,
415+
) -> bool {
416+
match self.partial_cmp(vis, tcx) {
417+
Some(ord) => ord.is_gt(),
418+
None => {
419+
tcx.dcx().delayed_bug(format!("unordered visibilities: {self:?} and {vis:?}"));
420+
false
421+
}
422+
}
394423
}
395424
}
396425

compiler/rustc_privacy/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ fn assoc_has_type_of(tcx: TyCtxt<'_>, item: &ty::AssocItem) -> bool {
337337
}
338338

339339
fn min(vis1: ty::Visibility, vis2: ty::Visibility, tcx: TyCtxt<'_>) -> ty::Visibility {
340-
if vis1.is_at_least(vis2, tcx) { vis2 } else { vis1 }
340+
if vis1.greater_than(vis2, tcx) { vis2 } else { vis1 }
341341
}
342342

343343
/// Visitor used to determine impl visibility and reachability.
@@ -1465,7 +1465,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
14651465
};
14661466

14671467
let vis = self.tcx.local_visibility(local_def_id);
1468-
if self.hard_error && !vis.is_at_least(self.required_visibility, self.tcx) {
1468+
if self.hard_error && self.required_visibility.greater_than(vis, self.tcx) {
14691469
let vis_descr = match vis {
14701470
ty::Visibility::Public => "public",
14711471
ty::Visibility::Restricted(vis_def_id) => {
@@ -1499,7 +1499,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
14991499

15001500
let reachable_at_vis = *effective_vis.at_level(Level::Reachable);
15011501

1502-
if !vis.is_at_least(reachable_at_vis, self.tcx) {
1502+
if reachable_at_vis.greater_than(vis, self.tcx) {
15031503
let lint = if self.in_primary_interface {
15041504
lint::builtin::PRIVATE_INTERFACES
15051505
} else {

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
924924
let field_vis = self
925925
.try_resolve_visibility(&field.vis, false)
926926
.unwrap_or(Visibility::Public);
927-
if ctor_vis.is_at_least(field_vis, self.r.tcx) {
927+
if ctor_vis.greater_than(field_vis, self.r.tcx) {
928928
ctor_vis = field_vis;
929929
}
930930
field_visibilities.push(field_vis.to_def_id());

compiler/rustc_resolve/src/imports.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! A bunch of methods and structures more or less related to resolving imports.
22
3+
use std::cmp::Ordering;
34
use std::mem;
45

56
use itertools::Itertools;
@@ -374,24 +375,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
374375
pub(crate) fn import_decl_vis(&self, decl: Decl<'ra>, import: ImportSummary) -> Visibility {
375376
assert!(import.vis.is_accessible_from(import.nearest_parent_mod, self.tcx));
376377
let decl_vis = decl.vis();
377-
if decl_vis.is_at_least(import.vis, self.tcx) {
378-
// Ordered, import is less visible than the imported declaration, or the same,
379-
// use the import's visibility.
380-
import.vis
381-
} else if decl_vis.is_accessible_from(import.nearest_parent_mod, self.tcx) {
382-
// Ordered, imported declaration is less visible than the import, but is still visible
378+
if decl_vis.partial_cmp(import.vis, self.tcx) == Some(Ordering::Less)
379+
&& decl_vis.is_accessible_from(import.nearest_parent_mod, self.tcx)
380+
&& pub_use_of_private_extern_crate_hack(import, decl).is_none()
381+
{
382+
// Imported declaration is less visible than the import, but is still visible
383383
// from the current module, use the declaration's visibility.
384-
assert!(import.vis.is_at_least(decl_vis, self.tcx));
385-
if pub_use_of_private_extern_crate_hack(import, decl).is_some() {
386-
import.vis
387-
} else {
388-
decl_vis.expect_local()
389-
}
384+
decl_vis.expect_local()
390385
} else {
391-
// Ordered or not, the imported declaration is too private for the current module.
386+
// Good case - imported declaration is more visible than the import, or the same,
387+
// use the import's visibility.
388+
// Bad case - imported declaration is too private for the current module.
392389
// It doesn't matter what visibility we choose here (except in the `PRIVATE_MACRO_USE`
393-
// case), because either some error will be reported, or the import declaration
394-
// will be thrown away (unfortunately cannot use delayed bug here for this reason).
390+
// and `PUB_USE_OF_PRIVATE_EXTERN_CRATE` cases), because either some error will be
391+
// reported, or the import declaration will be thrown away (unfortunately cannot use
392+
// delayed bug here for this reason).
395393
// Use import visibility to keep the all declaration visibilities in a module ordered.
396394
import.vis
397395
}
@@ -404,7 +402,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
404402

405403
if let ImportKind::Glob { ref max_vis, .. } = import.kind
406404
&& (vis == import.vis
407-
|| max_vis.get().is_none_or(|max_vis| vis.is_at_least(max_vis, self.tcx)))
405+
|| max_vis.get().is_none_or(|max_vis| vis.greater_than(max_vis, self.tcx)))
408406
{
409407
max_vis.set_unchecked(Some(vis))
410408
}
@@ -475,7 +473,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
475473
// FIXME: remove this when `warn_ambiguity` is removed (#149195).
476474
self.arenas.alloc_decl((*old_glob_decl).clone())
477475
}
478-
} else if !old_glob_decl.vis().is_at_least(glob_decl.vis(), self.tcx) {
476+
} else if glob_decl.vis().greater_than(old_glob_decl.vis(), self.tcx) {
479477
// We are glob-importing the same item but with greater visibility.
480478
// All visibilities here are ordered because all of them are ancestors of `module`.
481479
// FIXME: Update visibility in place, but without regressions
@@ -1251,7 +1249,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12511249
});
12521250
}
12531251
if let Some(max_vis) = max_vis.get()
1254-
&& !max_vis.is_at_least(import.vis, self.tcx)
1252+
&& import.vis.greater_than(max_vis, self.tcx)
12551253
{
12561254
let def_id = self.local_def_id(id);
12571255
self.lint_buffer.buffer_lint(
@@ -1500,7 +1498,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15001498
return;
15011499
};
15021500

1503-
if !binding.vis().is_at_least(import.vis, this.tcx) {
1501+
if import.vis.greater_than(binding.vis(), this.tcx) {
15041502
reexport_error = Some((ns, binding));
15051503
if let Visibility::Restricted(binding_def_id) = binding.vis()
15061504
&& binding_def_id.is_top_level_module()

0 commit comments

Comments
 (0)