Skip to content

Commit 18d2bd2

Browse files
committed
resolve: Replace visibility table in resolver outputs with query feeding
Also feed missing visibilities for import stems and trait impl items, which were previously evaluated lazily.
1 parent dd6126e commit 18d2bd2

File tree

8 files changed

+40
-77
lines changed

8 files changed

+40
-77
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
6060
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
6161
use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName, TraitCandidate};
6262
use rustc_index::{Idx, IndexSlice, IndexVec};
63-
use rustc_middle::{
64-
span_bug,
65-
ty::{ResolverAstLowering, TyCtxt},
66-
};
63+
use rustc_middle::span_bug;
64+
use rustc_middle::ty::{ResolverAstLowering, TyCtxt, Visibility};
6765
use rustc_session::parse::{add_feature_diagnostics, feature_err};
6866
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6967
use rustc_span::{DesugaringKind, Span, DUMMY_SP};
@@ -1624,6 +1622,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16241622
);
16251623
debug!(?opaque_ty_def_id);
16261624

1625+
// Meaningless, but provided so that all items have visibilities.
1626+
let parent_mod = self.tcx.parent_module_from_def_id(opaque_ty_def_id).to_def_id();
1627+
self.tcx.feed_local_def_id(opaque_ty_def_id).visibility(Visibility::Restricted(parent_mod));
1628+
16271629
// Map from captured (old) lifetime to synthetic (new) lifetime.
16281630
// Used to resolve lifetimes in the bounds of the opaque.
16291631
let mut captured_to_synthesized_mapping = FxHashMap::default();

compiler/rustc_middle/src/hir/map/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1077,8 +1077,6 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
10771077

10781078
let upstream_crates = upstream_crates(tcx);
10791079

1080-
let resolutions = tcx.resolutions(());
1081-
10821080
// We hash the final, remapped names of all local source files so we
10831081
// don't have to include the path prefix remapping commandline args.
10841082
// If we included the full mapping in the SVH, we could only have
@@ -1133,8 +1131,6 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
11331131
}
11341132
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
11351133
tcx.stable_crate_id(LOCAL_CRATE).hash_stable(&mut hcx, &mut stable_hasher);
1136-
// Hash visibility information since it does not appear in HIR.
1137-
resolutions.visibilities.hash_stable(&mut hcx, &mut stable_hasher);
11381134
stable_hasher.finish()
11391135
});
11401136

compiler/rustc_middle/src/ty/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ pub struct ResolverOutputs {
158158

159159
#[derive(Debug)]
160160
pub struct ResolverGlobalCtxt {
161-
pub visibilities: FxHashMap<LocalDefId, Visibility>,
162161
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
163162
pub expn_that_defined: FxHashMap<LocalDefId, ExpnId>,
164163
pub effective_visibilities: EffectiveVisibilities,

compiler/rustc_privacy/src/lib.rs

+10-45
Original file line numberDiff line numberDiff line change
@@ -1797,52 +1797,17 @@ pub fn provide(providers: &mut Providers) {
17971797
}
17981798

17991799
fn visibility(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Visibility<DefId> {
1800-
local_visibility(tcx, def_id).to_def_id()
1801-
}
1802-
1803-
fn local_visibility(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Visibility {
1804-
match tcx.resolutions(()).visibilities.get(&def_id) {
1805-
Some(vis) => *vis,
1806-
None => {
1807-
let hir_id = tcx.local_def_id_to_hir_id(def_id);
1808-
match tcx.hir().get(hir_id) {
1809-
// Unique types created for closures participate in type privacy checking.
1810-
// They have visibilities inherited from the module they are defined in.
1811-
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure{..}, .. })
1812-
// - AST lowering creates dummy `use` items which don't
1813-
// get their entries in the resolver's visibility table.
1814-
// - AST lowering also creates opaque type items with inherited visibilities.
1815-
// Visibility on them should have no effect, but to avoid the visibility
1816-
// query failing on some items, we provide it for opaque types as well.
1817-
| Node::Item(hir::Item {
1818-
kind: hir::ItemKind::Use(_, hir::UseKind::ListStem)
1819-
| hir::ItemKind::OpaqueTy(..),
1820-
..
1821-
}) => ty::Visibility::Restricted(tcx.parent_module(hir_id).to_local_def_id()),
1822-
// Visibilities of trait impl items are inherited from their traits
1823-
// and are not filled in resolve.
1824-
Node::ImplItem(impl_item) => {
1825-
match tcx.hir().get_by_def_id(tcx.hir().get_parent_item(hir_id).def_id) {
1826-
Node::Item(hir::Item {
1827-
kind: hir::ItemKind::Impl(hir::Impl { of_trait: Some(tr), .. }),
1828-
..
1829-
}) => tr.path.res.opt_def_id().map_or_else(
1830-
|| {
1831-
tcx.sess.span_delayed_bug(tr.path.span, "trait without a def-id");
1832-
ty::Visibility::Public
1833-
},
1834-
|def_id| tcx.visibility(def_id).expect_local(),
1835-
),
1836-
_ => span_bug!(impl_item.span, "the parent is not a trait impl"),
1837-
}
1838-
}
1839-
_ => span_bug!(
1840-
tcx.def_span(def_id),
1841-
"visibility table unexpectedly missing a def-id: {:?}",
1842-
def_id,
1843-
),
1844-
}
1800+
match tcx.hir().get_by_def_id(def_id) {
1801+
// Unique types created for closures participate in type privacy checking.
1802+
// They have visibilities inherited from the module they are defined in.
1803+
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
1804+
ty::Visibility::Restricted(tcx.parent_module_from_def_id(def_id).to_def_id())
18451805
}
1806+
_ => span_bug!(
1807+
tcx.def_span(def_id),
1808+
"visibility query unexpectedly called on a def-id: {:?}",
1809+
def_id,
1810+
),
18461811
}
18471812
}
18481813

compiler/rustc_resolve/src/build_reduced_graph.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
231231
// (i.e. variants, fields, and trait items) inherits from the visibility
232232
// of the enum or trait.
233233
ModuleKind::Def(DefKind::Enum | DefKind::Trait, def_id, _) => {
234-
self.r.visibilities[&def_id.expect_local()]
234+
self.r.tcx.visibility(def_id).expect_local()
235235
}
236236
// Otherwise, the visibility is restricted to the nearest parent `mod` item.
237237
_ => ty::Visibility::Restricted(
@@ -399,6 +399,10 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
399399
parent_prefix, use_tree, nested
400400
);
401401

402+
if nested {
403+
self.r.feed_visibility(self.r.local_def_id(id), vis);
404+
}
405+
402406
let mut prefix_iter = parent_prefix
403407
.iter()
404408
.cloned()
@@ -437,8 +441,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
437441
let mut source = module_path.pop().unwrap();
438442
let mut type_ns_only = false;
439443

440-
self.r.visibilities.insert(self.r.local_def_id(id), vis);
441-
442444
if nested {
443445
// Correctly handle `self`
444446
if source.ident.name == kw::SelfLower {
@@ -552,7 +554,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
552554
max_vis: Cell::new(None),
553555
id,
554556
};
555-
self.r.visibilities.insert(self.r.local_def_id(id), vis);
557+
556558
self.add_import(prefix, kind, use_tree.span, item, root_span, item.id, vis);
557559
}
558560
ast::UseTreeKind::Nested(ref items) => {
@@ -629,7 +631,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
629631
let local_def_id = self.r.local_def_id(item.id);
630632
let def_id = local_def_id.to_def_id();
631633

632-
self.r.visibilities.insert(local_def_id, vis);
634+
self.r.feed_visibility(local_def_id, vis);
633635

634636
match item.kind {
635637
ItemKind::Use(ref use_tree) => {
@@ -753,7 +755,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
753755
let ctor_res =
754756
Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id.to_def_id());
755757
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
756-
self.r.visibilities.insert(ctor_def_id, ctor_vis);
758+
self.r.feed_visibility(ctor_def_id, ctor_vis);
757759
// We need the field visibility spans also for the constructor for E0603.
758760
self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata);
759761

@@ -899,7 +901,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
899901
let vis = self.resolve_visibility(&item.vis);
900902
let res = Res::Def(def_kind, def_id);
901903
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
902-
self.r.visibilities.insert(local_def_id, vis);
904+
self.r.feed_visibility(local_def_id, vis);
903905
}
904906

905907
fn build_reduced_graph_for_block(&mut self, block: &Block) {
@@ -1233,7 +1235,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
12331235
self.r.check_reserved_macro_name(ident, res);
12341236
self.insert_unused_macro(ident, def_id, item.id);
12351237
}
1236-
self.r.visibilities.insert(def_id, vis);
1238+
self.r.feed_visibility(def_id, vis);
12371239
let scope = self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Binding(
12381240
self.r.arenas.alloc_macro_rules_binding(MacroRulesBinding {
12391241
parent_macro_rules_scope: parent_scope.macro_rules,
@@ -1257,7 +1259,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
12571259
self.insert_unused_macro(ident, def_id, item.id);
12581260
}
12591261
self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
1260-
self.r.visibilities.insert(def_id, vis);
1262+
self.r.feed_visibility(def_id, vis);
12611263
self.parent_scope.macro_rules
12621264
}
12631265
}
@@ -1359,7 +1361,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
13591361
// Trait impl item visibility is inherited from its trait when not specified
13601362
// explicitly. In that case we cannot determine it here in early resolve,
13611363
// so we leave a hole in the visibility table to be filled later.
1362-
self.r.visibilities.insert(local_def_id, vis);
1364+
self.r.feed_visibility(local_def_id, vis);
13631365
}
13641366

13651367
if ctxt == AssocCtxt::Trait {
@@ -1438,7 +1440,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
14381440
self.visit_invoc(sf.id);
14391441
} else {
14401442
let vis = self.resolve_visibility(&sf.vis);
1441-
self.r.visibilities.insert(self.r.local_def_id(sf.id), vis);
1443+
self.r.feed_visibility(self.r.local_def_id(sf.id), vis);
14421444
visit::walk_field_def(self, sf);
14431445
}
14441446
}
@@ -1460,7 +1462,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
14601462
let res = Res::Def(DefKind::Variant, def_id.to_def_id());
14611463
let vis = self.resolve_visibility(&variant.vis);
14621464
self.r.define(parent, ident, TypeNS, (res, vis, variant.span, expn_id));
1463-
self.r.visibilities.insert(def_id, vis);
1465+
self.r.feed_visibility(def_id, vis);
14641466

14651467
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
14661468
let ctor_vis =
@@ -1476,7 +1478,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
14761478
let ctor_res =
14771479
Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id.to_def_id());
14781480
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));
1479-
self.r.visibilities.insert(ctor_def_id, ctor_vis);
1481+
self.r.feed_visibility(ctor_def_id, ctor_vis);
14801482
}
14811483

14821484
// Record field names for error reporting.

compiler/rustc_resolve/src/effective_visibilities.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
186186
) -> Option<Option<Visibility>> {
187187
match parent_id {
188188
ParentId::Def(def_id) => (nominal_vis != self.current_private_vis
189-
&& self.r.visibilities[&def_id] != self.current_private_vis)
189+
&& self.r.tcx.local_visibility(def_id) != self.current_private_vis)
190190
.then_some(Some(self.current_private_vis)),
191191
ParentId::Import(_) => Some(None),
192192
}
@@ -222,7 +222,7 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
222222
}
223223

224224
fn update_field(&mut self, def_id: LocalDefId, parent_id: LocalDefId) {
225-
self.update_def(def_id, self.r.visibilities[&def_id], ParentId::Def(parent_id));
225+
self.update_def(def_id, self.r.tcx.local_visibility(def_id), ParentId::Def(parent_id));
226226
}
227227
}
228228

compiler/rustc_resolve/src/late.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3108,6 +3108,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
31083108
| (DefKind::AssocFn, AssocItemKind::Fn(..))
31093109
| (DefKind::AssocConst, AssocItemKind::Const(..)) => {
31103110
self.r.record_partial_res(id, PartialRes::new(res));
3111+
let vis = self.r.tcx.visibility(id_in_trait).expect_local();
3112+
self.r.feed_visibility(self.r.local_def_id(id), vis);
31113113
return;
31123114
}
31133115
_ => {}

compiler/rustc_resolve/src/lib.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1007,8 +1007,6 @@ pub struct Resolver<'a, 'tcx> {
10071007

10081008
/// Maps glob imports to the names of items actually imported.
10091009
glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
1010-
/// Visibilities in "lowered" form, for all entities that have them.
1011-
visibilities: FxHashMap<LocalDefId, ty::Visibility>,
10121010
used_imports: FxHashSet<NodeId>,
10131011
maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
10141012

@@ -1295,9 +1293,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12951293
&mut FxHashMap::default(),
12961294
);
12971295

1298-
let mut visibilities = FxHashMap::default();
1299-
visibilities.insert(CRATE_DEF_ID, ty::Visibility::Public);
1300-
13011296
let mut def_id_to_node_id = IndexVec::default();
13021297
assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), CRATE_DEF_ID);
13031298
let mut node_id_to_def_id = FxHashMap::default();
@@ -1363,7 +1358,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13631358
ast_transform_scopes: FxHashMap::default(),
13641359

13651360
glob_map: Default::default(),
1366-
visibilities,
13671361
used_imports: FxHashSet::default(),
13681362
maybe_unused_trait_imports: Default::default(),
13691363

@@ -1450,6 +1444,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14501444

14511445
let root_parent_scope = ParentScope::module(graph_root, &resolver);
14521446
resolver.invocation_parent_scopes.insert(LocalExpnId::ROOT, root_parent_scope);
1447+
resolver.feed_visibility(CRATE_DEF_ID, ty::Visibility::Public);
14531448

14541449
resolver
14551450
}
@@ -1497,10 +1492,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14971492
Default::default()
14981493
}
14991494

1495+
fn feed_visibility(&self, def_id: LocalDefId, vis: ty::Visibility) {
1496+
self.tcx.feed_local_def_id(def_id).visibility(vis.to_def_id());
1497+
}
1498+
15001499
pub fn into_outputs(self) -> ResolverOutputs {
15011500
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
15021501
let expn_that_defined = self.expn_that_defined;
1503-
let visibilities = self.visibilities;
15041502
let extern_crate_map = self.extern_crate_map;
15051503
let maybe_unused_trait_imports = self.maybe_unused_trait_imports;
15061504
let glob_map = self.glob_map;
@@ -1517,7 +1515,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15171515

15181516
let global_ctxt = ResolverGlobalCtxt {
15191517
expn_that_defined,
1520-
visibilities,
15211518
effective_visibilities,
15221519
extern_crate_map,
15231520
module_children: self.module_children,

0 commit comments

Comments
 (0)