Skip to content

Commit 58ed74c

Browse files
authored
Rollup merge of #101098 - petrochenkov:noinvis, r=TaKO8Ki
rustc_middle: Remove `Visibility::Invisible` It had a different meaning in the past, but now it's only used as an implementation detail of import resolution.
2 parents 0dc6ab6 + fc3f3c3 commit 58ed74c

File tree

10 files changed

+43
-53
lines changed

10 files changed

+43
-53
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -1500,24 +1500,18 @@ fn vcall_visibility_metadata<'ll, 'tcx>(
15001500
// If there is not LTO and the visibility in public, we have to assume that the vtable can
15011501
// be seen from anywhere. With multiple CGUs, the vtable is quasi-public.
15021502
(Lto::No | Lto::ThinLocal, Visibility::Public, _)
1503-
| (Lto::No, Visibility::Restricted(_) | Visibility::Invisible, false) => {
1504-
VCallVisibility::Public
1505-
}
1503+
| (Lto::No, Visibility::Restricted(_), false) => VCallVisibility::Public,
15061504
// With LTO and a quasi-public visibility, the usages of the functions of the vtable are
15071505
// all known by the `LinkageUnit`.
15081506
// FIXME: LLVM only supports this optimization for `Lto::Fat` currently. Once it also
15091507
// supports `Lto::Thin` the `VCallVisibility` may have to be adjusted for those.
15101508
(Lto::Fat | Lto::Thin, Visibility::Public, _)
1511-
| (
1512-
Lto::ThinLocal | Lto::Thin | Lto::Fat,
1513-
Visibility::Restricted(_) | Visibility::Invisible,
1514-
false,
1515-
) => VCallVisibility::LinkageUnit,
1509+
| (Lto::ThinLocal | Lto::Thin | Lto::Fat, Visibility::Restricted(_), false) => {
1510+
VCallVisibility::LinkageUnit
1511+
}
15161512
// If there is only one CGU, private vtables can only be seen by that CGU/translation unit
15171513
// and therefore we know of all usages of functions in the vtable.
1518-
(_, Visibility::Restricted(_) | Visibility::Invisible, true) => {
1519-
VCallVisibility::TranslationUnit
1520-
}
1514+
(_, Visibility::Restricted(_), true) => VCallVisibility::TranslationUnit,
15211515
};
15221516

15231517
let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);

compiler/rustc_middle/src/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ fn skip_stability_check_due_to_privacy(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
293293

294294
// These are not visible outside crate; therefore
295295
// stability markers are irrelevant, if even present.
296-
ty::Visibility::Restricted(..) | ty::Visibility::Invisible => true,
296+
ty::Visibility::Restricted(..) => true,
297297
}
298298
}
299299

compiler/rustc_middle/src/ty/inhabitedness/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,10 @@ impl<'tcx> FieldDef {
169169
param_env: ty::ParamEnv<'tcx>,
170170
) -> DefIdForest<'tcx> {
171171
let data_uninhabitedness = move || self.ty(tcx, substs).uninhabited_from(tcx, param_env);
172-
// FIXME(canndrew): Currently enum fields are (incorrectly) stored with
173-
// `Visibility::Invisible` so we need to override `self.vis` if we're
174-
// dealing with an enum.
175172
if is_enum {
176173
data_uninhabitedness()
177174
} else {
178175
match self.vis {
179-
Visibility::Invisible => DefIdForest::empty(),
180176
Visibility::Restricted(from) => {
181177
let forest = DefIdForest::from_id(from);
182178
let iter = Some(forest).into_iter().chain(Some(data_uninhabitedness()));

compiler/rustc_middle/src/ty/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ pub enum Visibility {
268268
Public,
269269
/// Visible only in the given crate-local module.
270270
Restricted(DefId),
271-
/// Not visible anywhere in the local crate. This is the visibility of private external items.
272-
Invisible,
273271
}
274272

275273
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
@@ -366,8 +364,6 @@ impl Visibility {
366364
let restriction = match self {
367365
// Public items are visible everywhere.
368366
Visibility::Public => return true,
369-
// Private items from other crates are visible nowhere.
370-
Visibility::Invisible => return false,
371367
// Restricted items are visible in an arbitrary local module.
372368
Visibility::Restricted(other) if other.krate != module.krate => return false,
373369
Visibility::Restricted(module) => module,
@@ -380,7 +376,6 @@ impl Visibility {
380376
pub fn is_at_least<T: DefIdTree>(self, vis: Visibility, tree: T) -> bool {
381377
let vis_restriction = match vis {
382378
Visibility::Public => return self == Visibility::Public,
383-
Visibility::Invisible => return true,
384379
Visibility::Restricted(module) => module,
385380
};
386381

@@ -392,7 +387,6 @@ impl Visibility {
392387
match self {
393388
Visibility::Public => true,
394389
Visibility::Restricted(def_id) => def_id.is_local(),
395-
Visibility::Invisible => false,
396390
}
397391
}
398392

compiler/rustc_privacy/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,6 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17311731
if !vis.is_at_least(self.required_visibility, self.tcx) {
17321732
let vis_descr = match vis {
17331733
ty::Visibility::Public => "public",
1734-
ty::Visibility::Invisible => "private",
17351734
ty::Visibility::Restricted(vis_def_id) => {
17361735
if vis_def_id == self.tcx.parent_module(hir_id).to_def_id() {
17371736
"private"

compiler/rustc_resolve/src/build_reduced_graph.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
380380
has_attributes: !item.attrs.is_empty(),
381381
root_span,
382382
root_id,
383-
vis: Cell::new(vis),
383+
vis: Cell::new(Some(vis)),
384384
used: Cell::new(false),
385385
});
386386

@@ -588,7 +588,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
588588
ast::UseTreeKind::Glob => {
589589
let kind = ImportKind::Glob {
590590
is_prelude: self.r.session.contains_name(&item.attrs, sym::prelude_import),
591-
max_vis: Cell::new(ty::Visibility::Invisible),
591+
max_vis: Cell::new(None),
592592
};
593593
self.r.visibilities.insert(self.r.local_def_id(id), vis);
594594
self.add_import(prefix, kind, use_tree.span, id, item, root_span, item.id, vis);
@@ -650,7 +650,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
650650
true,
651651
// The whole `use` item
652652
item,
653-
ty::Visibility::Invisible,
653+
ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod()),
654654
root_span,
655655
);
656656
}
@@ -885,7 +885,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
885885
root_span: item.span,
886886
span: item.span,
887887
module_path: Vec::new(),
888-
vis: Cell::new(vis),
888+
vis: Cell::new(Some(vis)),
889889
used: Cell::new(used),
890890
});
891891
self.r.potentially_unused_imports.push(import);
@@ -1118,7 +1118,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11181118
root_span: span,
11191119
span,
11201120
module_path: Vec::new(),
1121-
vis: Cell::new(ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id())),
1121+
vis: Cell::new(Some(ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id()))),
11221122
used: Cell::new(false),
11231123
})
11241124
};

compiler/rustc_resolve/src/check_unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl Resolver<'_> {
227227
for import in self.potentially_unused_imports.iter() {
228228
match import.kind {
229229
_ if import.used.get()
230-
|| import.vis.get().is_public()
230+
|| import.expect_vis().is_public()
231231
|| import.span.is_dummy() =>
232232
{
233233
if let ImportKind::MacroUse = import.kind {

compiler/rustc_resolve/src/ident.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,10 @@ impl<'a> Resolver<'a> {
953953
// Check if one of single imports can still define the name,
954954
// if it can then our result is not determined and can be invalidated.
955955
for single_import in &resolution.single_imports {
956-
if !self.is_accessible_from(single_import.vis.get(), parent_scope.module) {
956+
let Some(import_vis) = single_import.vis.get() else {
957+
continue;
958+
};
959+
if !self.is_accessible_from(import_vis, parent_scope.module) {
957960
continue;
958961
}
959962
let Some(module) = single_import.imported_module.get() else {
@@ -1018,7 +1021,10 @@ impl<'a> Resolver<'a> {
10181021
// Check if one of glob imports can still define the name,
10191022
// if it can then our "no resolution" result is not determined and can be invalidated.
10201023
for glob_import in module.globs.borrow().iter() {
1021-
if !self.is_accessible_from(glob_import.vis.get(), parent_scope.module) {
1024+
let Some(import_vis) = glob_import.vis.get() else {
1025+
continue;
1026+
};
1027+
if !self.is_accessible_from(import_vis, parent_scope.module) {
10221028
continue;
10231029
}
10241030
let module = match glob_import.imported_module.get() {

compiler/rustc_resolve/src/imports.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ pub enum ImportKind<'a> {
5252
},
5353
Glob {
5454
is_prelude: bool,
55-
max_vis: Cell<ty::Visibility>, // The visibility of the greatest re-export.
56-
// n.b. `max_vis` is only used in `finalize_import` to check for re-export errors.
55+
max_vis: Cell<Option<ty::Visibility>>, // The visibility of the greatest re-export.
56+
// n.b. `max_vis` is only used in `finalize_import` to check for re-export errors.
5757
},
5858
ExternCrate {
5959
source: Option<Symbol>,
@@ -144,7 +144,7 @@ pub(crate) struct Import<'a> {
144144
pub module_path: Vec<Segment>,
145145
/// The resolution of `module_path`.
146146
pub imported_module: Cell<Option<ModuleOrUniformRoot<'a>>>,
147-
pub vis: Cell<ty::Visibility>,
147+
pub vis: Cell<Option<ty::Visibility>>,
148148
pub used: Cell<bool>,
149149
}
150150

@@ -159,6 +159,10 @@ impl<'a> Import<'a> {
159159
_ => false,
160160
}
161161
}
162+
163+
pub(crate) fn expect_vis(&self) -> ty::Visibility {
164+
self.vis.get().expect("encountered cleared import visibility")
165+
}
162166
}
163167

164168
/// Records information about the resolution of a name in a namespace of a module.
@@ -199,7 +203,7 @@ fn pub_use_of_private_extern_crate_hack(import: &Import<'_>, binding: &NameBindi
199203
import: Import { kind: ImportKind::ExternCrate { .. }, .. },
200204
..
201205
},
202-
) => import.vis.get().is_public(),
206+
) => import.expect_vis().is_public(),
203207
_ => false,
204208
}
205209
}
@@ -212,17 +216,20 @@ impl<'a> Resolver<'a> {
212216
binding: &'a NameBinding<'a>,
213217
import: &'a Import<'a>,
214218
) -> &'a NameBinding<'a> {
215-
let vis = if binding.vis.is_at_least(import.vis.get(), self)
219+
let import_vis = import.expect_vis();
220+
let vis = if binding.vis.is_at_least(import_vis, self)
216221
|| pub_use_of_private_extern_crate_hack(import, binding)
217222
{
218-
import.vis.get()
223+
import_vis
219224
} else {
220225
binding.vis
221226
};
222227

223228
if let ImportKind::Glob { ref max_vis, .. } = import.kind {
224-
if vis == import.vis.get() || vis.is_at_least(max_vis.get(), self) {
225-
max_vis.set(vis)
229+
if vis == import_vis
230+
|| max_vis.get().map_or(true, |max_vis| vis.is_at_least(max_vis, self))
231+
{
232+
max_vis.set(Some(vis))
226233
}
227234
}
228235

@@ -536,7 +543,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
536543
} else {
537544
// For better failure detection, pretend that the import will
538545
// not define any names while resolving its module path.
539-
let orig_vis = import.vis.replace(ty::Visibility::Invisible);
546+
let orig_vis = import.vis.take();
540547
let path_res =
541548
self.r.maybe_resolve_path(&import.module_path, None, &import.parent_scope);
542549
import.vis.set(orig_vis);
@@ -571,7 +578,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
571578
if let Err(Undetermined) = source_bindings[ns].get() {
572579
// For better failure detection, pretend that the import will
573580
// not define any names while resolving its module path.
574-
let orig_vis = import.vis.replace(ty::Visibility::Invisible);
581+
let orig_vis = import.vis.take();
575582
let binding = this.resolve_ident_in_module(
576583
module,
577584
source,
@@ -620,7 +627,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
620627
/// Optionally returns an unresolved import error. This error is buffered and used to
621628
/// consolidate multiple unresolved import errors into a single diagnostic.
622629
fn finalize_import(&mut self, import: &'b Import<'b>) -> Option<UnresolvedImportError> {
623-
let orig_vis = import.vis.replace(ty::Visibility::Invisible);
630+
let orig_vis = import.vis.take();
624631
let ignore_binding = match &import.kind {
625632
ImportKind::Single { target_bindings, .. } => target_bindings[TypeNS].get(),
626633
_ => None,
@@ -727,9 +734,9 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
727734
});
728735
}
729736
}
730-
if !is_prelude &&
731-
max_vis.get() != ty::Visibility::Invisible && // Allow empty globs.
732-
!max_vis.get().is_at_least(import.vis.get(), &*self.r)
737+
if !is_prelude
738+
&& let Some(max_vis) = max_vis.get()
739+
&& !max_vis.is_at_least(import.expect_vis(), &*self.r)
733740
{
734741
let msg = "glob import doesn't reexport anything because no candidate is public enough";
735742
self.r.lint_buffer.buffer_lint(UNUSED_IMPORTS, import.id, import.span, msg);
@@ -742,7 +749,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
742749
let mut all_ns_err = true;
743750
self.r.per_ns(|this, ns| {
744751
if !type_ns_only || ns == TypeNS {
745-
let orig_vis = import.vis.replace(ty::Visibility::Invisible);
752+
let orig_vis = import.vis.take();
746753
let binding = this.resolve_ident_in_module(
747754
module,
748755
ident,
@@ -906,8 +913,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
906913
let mut crate_private_reexport = false;
907914
self.r.per_ns(|this, ns| {
908915
if let Ok(binding) = source_bindings[ns].get() {
909-
let vis = import.vis.get();
910-
if !binding.vis.is_at_least(vis, &*this) {
916+
if !binding.vis.is_at_least(import.expect_vis(), &*this) {
911917
reexport_error = Some((ns, binding));
912918
if let ty::Visibility::Restricted(binding_def_id) = binding.vis {
913919
if binding_def_id.is_top_level_module() {

src/librustdoc/clean/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1776,11 +1776,6 @@ fn is_field_vis_inherited(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
17761776
pub(crate) fn clean_visibility(vis: ty::Visibility) -> Visibility {
17771777
match vis {
17781778
ty::Visibility::Public => Visibility::Public,
1779-
// NOTE: this is not quite right: `ty` uses `Invisible` to mean 'private',
1780-
// while rustdoc really does mean inherited. That means that for enum variants, such as
1781-
// `pub enum E { V }`, `V` will be marked as `Public` by `ty`, but as `Inherited` by rustdoc.
1782-
// Various parts of clean override `tcx.visibility` explicitly to make sure this distinction is captured.
1783-
ty::Visibility::Invisible => Visibility::Inherited,
17841779
ty::Visibility::Restricted(module) => Visibility::Restricted(module),
17851780
}
17861781
}

0 commit comments

Comments
 (0)