From 66f24393cc0a1167c50e174f59510f0fe5cf24c9 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 6 Dec 2023 16:21:16 +0300 Subject: [PATCH] Feed visibilities for import stems and opaque types too --- compiler/rustc_ast_lowering/src/lib.rs | 6 +++++- compiler/rustc_privacy/src/lib.rs | 21 ++++--------------- .../rustc_resolve/src/build_reduced_graph.rs | 8 ++++--- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index d435082e12190..c8d17e2ab41c4 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -62,7 +62,7 @@ use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName, TraitCandidate}; use rustc_index::{Idx, IndexSlice, IndexVec}; use rustc_middle::{ span_bug, - ty::{ResolverAstLowering, TyCtxt}, + ty::{ResolverAstLowering, TyCtxt, Visibility}, }; use rustc_session::parse::{add_feature_diagnostics, feature_err}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; @@ -1624,6 +1624,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ); debug!(?opaque_ty_def_id); + // Meaningless, but provided so that all items have visibilities. + let parent_mod = self.tcx.parent_module_from_def_id(opaque_ty_def_id).to_def_id(); + self.tcx.feed_local_def_id(opaque_ty_def_id).visibility(Visibility::Restricted(parent_mod)); + // Map from captured (old) lifetime to synthetic (new) lifetime. // Used to resolve lifetimes in the bounds of the opaque. let mut captured_to_synthesized_mapping = FxHashMap::default(); diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 12a6807b1d5ff..777d893ba3eb4 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -1774,25 +1774,12 @@ pub fn provide(providers: &mut Providers) { } fn visibility(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Visibility { - local_visibility(tcx, def_id).to_def_id() -} - -fn local_visibility(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Visibility { - let hir_id = tcx.local_def_id_to_hir_id(def_id); - match tcx.hir().get(hir_id) { + match tcx.hir().get_by_def_id(def_id) { // Unique types created for closures participate in type privacy checking. // They have visibilities inherited from the module they are defined in. - Node::Expr(hir::Expr { kind: hir::ExprKind::Closure{..}, .. }) - // - AST lowering creates dummy `use` items which don't - // get their entries in the resolver's visibility table. - // - AST lowering also creates opaque type items with inherited visibilities. - // Visibility on them should have no effect, but to avoid the visibility - // query failing on some items, we provide it for opaque types as well. - | Node::Item(hir::Item { - kind: hir::ItemKind::Use(_, hir::UseKind::ListStem) - | hir::ItemKind::OpaqueTy(..), - .. - }) => ty::Visibility::Restricted(tcx.parent_module(hir_id).to_local_def_id()), + Node::Expr(hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => { + ty::Visibility::Restricted(tcx.parent_module_from_def_id(def_id).to_def_id()) + } _ => span_bug!( tcx.def_span(def_id), "visibility table unexpectedly missing a def-id: {:?}", diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 9c09bb38a3f14..cc553d916806c 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -399,6 +399,10 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { parent_prefix, use_tree, nested ); + if nested { + self.r.feed_visibility(self.r.local_def_id(id), vis); + } + let mut prefix_iter = parent_prefix .iter() .cloned() @@ -437,8 +441,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { let mut source = module_path.pop().unwrap(); let mut type_ns_only = false; - self.r.feed_visibility(self.r.local_def_id(id), vis); - if nested { // Correctly handle `self` if source.ident.name == kw::SelfLower { @@ -552,7 +554,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { max_vis: Cell::new(None), id, }; - self.r.feed_visibility(self.r.local_def_id(id), vis); + self.add_import(prefix, kind, use_tree.span, item, root_span, item.id, vis); } ast::UseTreeKind::Nested(ref items) => {