Skip to content

Commit 5e48418

Browse files
Rollup merge of rust-lang#155587 - oli-obk:feed-visibility-no-hash, r=petrochenkov
Immediately feed visibility on DefId creation This system was originally introduced in rust-lang#121089 This PR was enabled by refactorings in rust-lang#154945, because after that, the visibility feeding happens directly after the `DefId` creation, so we don't need to go through the intermediate hash table anymore Should unblock rust-lang#138995
2 parents dcde112 + 3070ce9 commit 5e48418

5 files changed

Lines changed: 138 additions & 128 deletions

File tree

compiler/rustc_middle/src/ty/context.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,23 @@ impl<'tcx> TyCtxt<'tcx> {
703703
self.dep_graph.assert_ignored();
704704
TyCtxtFeed { tcx: self, key }.delayed_owner(owner);
705705
}
706+
707+
// Trait impl item visibility is inherited from its trait when not specified
708+
// explicitly. In that case we cannot determine it in early resolve,
709+
// but instead are feeding it in late resolve, where we don't have access to the
710+
// `TyCtxtFeed` anymore.
711+
// To avoid having to hash the `LocalDefId` multiple times for inserting and removing the
712+
// `TyCtxtFeed` from a hash table, we add this hack to feed the visibility.
713+
// Do not use outside of the resolver query.
714+
pub fn feed_visibility_for_trait_impl_item(self, key: LocalDefId, vis: ty::Visibility) {
715+
if cfg!(debug_assertions) {
716+
match self.def_kind(self.local_parent(key)) {
717+
DefKind::Impl { of_trait: true } => {}
718+
other => bug!("{key:?} is not an assoc item of a trait impl: {other:?}"),
719+
}
720+
}
721+
TyCtxtFeed { tcx: self, key }.visibility(vis.to_def_id())
722+
}
706723
}
707724

708725
impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> {

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
2121
use rustc_index::bit_set::DenseBitSet;
2222
use rustc_metadata::creader::LoadedMacro;
2323
use rustc_middle::metadata::{ModChild, Reexport};
24-
use rustc_middle::ty::{Feed, Visibility};
24+
use rustc_middle::ty::{TyCtxtFeed, Visibility};
2525
use rustc_middle::{bug, span_bug};
2626
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
2727
use rustc_span::{Ident, Span, Symbol, kw, sym};
@@ -563,6 +563,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
563563
item: &Item,
564564
vis: Visibility,
565565
root_span: Span,
566+
feed: TyCtxtFeed<'tcx, LocalDefId>,
566567
) {
567568
debug!(
568569
"build_reduced_graph_for_use_tree(parent_prefix={:?}, use_tree={:?}, nested={})",
@@ -572,7 +573,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
572573
// Top level use tree reuses the item's id and list stems reuse their parent
573574
// use tree's ids, so in both cases their visibilities are already filled.
574575
if nested && !list_stem {
575-
self.r.feed_visibility(self.r.feed(id), vis);
576+
self.r.feed_visibility(feed, vis);
576577
}
577578

578579
let mut prefix_iter = parent_prefix
@@ -735,11 +736,11 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
735736
}
736737
ast::UseTreeKind::Nested { ref items, .. } => {
737738
for &(ref tree, id) in items {
738-
self.create_def(id, None, DefKind::Use, use_tree.span());
739+
let feed = self.create_def(id, None, DefKind::Use, use_tree.span());
739740
self.build_reduced_graph_for_use_tree(
740741
// This particular use tree
741742
tree, id, &prefix, true, false, // The whole `use` item
742-
item, vis, root_span,
743+
item, vis, root_span, feed,
743744
);
744745
}
745746

@@ -768,6 +769,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
768769
self.parent_scope.module.nearest_parent_mod().expect_local(),
769770
),
770771
root_span,
772+
feed,
771773
);
772774
}
773775
}
@@ -778,7 +780,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
778780
&mut self,
779781
fields: &[ast::FieldDef],
780782
ident: Ident,
781-
feed: Feed<'tcx, LocalDefId>,
783+
feed: TyCtxtFeed<'tcx, LocalDefId>,
782784
adt_res: Res,
783785
adt_vis: Visibility,
784786
adt_span: Span,
@@ -798,13 +800,12 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
798800
}
799801

800802
/// Constructs the reduced graph for one item.
801-
fn build_reduced_graph_for_item(&mut self, item: &'a Item) {
803+
fn build_reduced_graph_for_item(&mut self, item: &'a Item, feed: TyCtxtFeed<'tcx, LocalDefId>) {
802804
let parent_scope = &self.parent_scope;
803805
let parent = parent_scope.module.expect_local();
804806
let expansion = parent_scope.expansion;
805807
let sp = item.span;
806808
let vis = self.resolve_visibility(&item.vis);
807-
let feed = self.r.feed(item.id);
808809
let local_def_id = feed.key();
809810
let def_id = local_def_id.to_def_id();
810811
let def_kind = self.r.tcx.def_kind(def_id);
@@ -825,6 +826,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
825826
item,
826827
vis,
827828
use_tree.span(),
829+
feed,
828830
);
829831
}
830832

@@ -867,7 +869,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
867869

868870
// Functions introducing procedural macros reserve a slot
869871
// in the macro namespace as well (see #52225).
870-
self.define_macro(item);
872+
self.define_macro(item, feed);
871873
}
872874

873875
// These items live in the type namespace.
@@ -902,7 +904,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
902904

903905
// If this is a tuple or unit struct, define a name
904906
// in the value namespace as well.
905-
if let Some(ctor_node_id) = vdata.ctor_node_id() {
907+
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(vdata) {
906908
// If the structure is marked as non_exhaustive then lower the visibility
907909
// to within the crate.
908910
let mut ctor_vis = if vis.is_public()
@@ -927,7 +929,14 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
927929
}
928930
field_visibilities.push(field_vis.to_def_id());
929931
}
930-
let feed = self.r.feed(ctor_node_id);
932+
// If this is a unit or tuple-like struct, register the constructor.
933+
let feed = self.create_def(
934+
ctor_node_id,
935+
None,
936+
DefKind::Ctor(CtorOf::Struct, ctor_kind),
937+
item.span,
938+
);
939+
931940
let ctor_def_id = feed.key();
932941
let ctor_res = self.res(ctor_def_id);
933942
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
@@ -1062,8 +1071,8 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
10621071
&mut self,
10631072
item: &ForeignItem,
10641073
ident: Ident,
1074+
feed: TyCtxtFeed<'tcx, LocalDefId>,
10651075
) {
1066-
let feed = self.r.feed(item.id);
10671076
let local_def_id = feed.key();
10681077
let def_id = local_def_id.to_def_id();
10691078
let ns = match item.kind {
@@ -1259,10 +1268,13 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
12591268
}
12601269
}
12611270

1262-
fn define_macro(&mut self, item: &ast::Item) -> MacroRulesScopeRef<'ra> {
1271+
fn define_macro(
1272+
&mut self,
1273+
item: &ast::Item,
1274+
feed: TyCtxtFeed<'tcx, LocalDefId>,
1275+
) -> MacroRulesScopeRef<'ra> {
12631276
let parent_scope = self.parent_scope;
12641277
let expansion = parent_scope.expansion;
1265-
let feed = self.r.feed(item.id);
12661278
let def_id = feed.key();
12671279
let (res, orig_ident, span, macro_rules) = match &item.kind {
12681280
ItemKind::MacroDef(ident, def) => {
@@ -1361,18 +1373,17 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
13611373
}
13621374

13631375
impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
1364-
pub(crate) fn brg_visit_item(&mut self, item: &'a Item) {
1376+
pub(crate) fn brg_visit_item(&mut self, item: &'a Item, feed: TyCtxtFeed<'tcx, LocalDefId>) {
13651377
let orig_module_scope = self.parent_scope.module;
13661378
self.parent_scope.macro_rules = match item.kind {
13671379
ItemKind::MacroDef(..) => {
1368-
let macro_rules_scope = self.define_macro(item);
1380+
let macro_rules_scope = self.define_macro(item, feed);
13691381
visit::walk_item(self, item);
13701382
macro_rules_scope
13711383
}
1372-
ItemKind::MacCall(..) => self.visit_invoc_in_module(item.id),
13731384
_ => {
13741385
let orig_macro_rules_scope = self.parent_scope.macro_rules;
1375-
self.build_reduced_graph_for_item(item);
1386+
self.build_reduced_graph_for_item(item, feed);
13761387
match item.kind {
13771388
ItemKind::Mod(..) => {
13781389
// Visit attributes after items for backward compatibility.
@@ -1394,8 +1405,10 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
13941405
self.parent_scope.module = orig_module_scope;
13951406
}
13961407

1397-
pub(crate) fn brg_visit_stmt_mac_call(&mut self, stmt: &'a ast::Stmt) {
1398-
self.parent_scope.macro_rules = self.visit_invoc_in_module(stmt.id);
1408+
/// Handle a macro call that itself can produce new `macro_rules` items
1409+
/// in the current module.
1410+
pub(crate) fn brg_visit_mac_call_in_module(&mut self, id: NodeId) {
1411+
self.parent_scope.macro_rules = self.visit_invoc_in_module(id);
13991412
}
14001413

14011414
pub(crate) fn brg_visit_block(&mut self, block: &'a Block) {
@@ -1413,9 +1426,9 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
14131426
ctxt: AssocCtxt,
14141427
ident: Ident,
14151428
ns: Namespace,
1429+
feed: TyCtxtFeed<'tcx, LocalDefId>,
14161430
) {
14171431
let vis = self.resolve_visibility(&item.vis);
1418-
let feed = self.r.feed(item.id);
14191432
let local_def_id = feed.key();
14201433
let def_id = local_def_id.to_def_id();
14211434

@@ -1467,21 +1480,28 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
14671480
}
14681481
}
14691482

1470-
pub(crate) fn brg_visit_field_def(&mut self, sf: &'a ast::FieldDef) {
1483+
pub(crate) fn brg_visit_field_def(
1484+
&mut self,
1485+
sf: &'a ast::FieldDef,
1486+
feed: TyCtxtFeed<'tcx, LocalDefId>,
1487+
) {
14711488
let vis = self.resolve_visibility(&sf.vis);
1472-
self.r.feed_visibility(self.r.feed(sf.id), vis);
1489+
self.r.feed_visibility(feed, vis);
14731490
visit::walk_field_def(self, sf);
14741491
}
14751492

14761493
// Constructs the reduced graph for one variant. Variants exist in the
14771494
// type and value namespaces.
1478-
pub(crate) fn brg_visit_variant(&mut self, variant: &'a ast::Variant) {
1495+
pub(crate) fn brg_visit_variant(
1496+
&mut self,
1497+
variant: &'a ast::Variant,
1498+
feed: TyCtxtFeed<'tcx, LocalDefId>,
1499+
) {
14791500
let parent = self.parent_scope.module.expect_local();
14801501
let expn_id = self.parent_scope.expansion;
14811502
let ident = variant.ident;
14821503

14831504
// Define a name in the type namespace.
1484-
let feed = self.r.feed(variant.id);
14851505
let def_id = feed.key();
14861506
let vis = self.resolve_visibility(&variant.vis);
14871507
self.r.define_local(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
@@ -1496,8 +1516,13 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
14961516
};
14971517

14981518
// Define a constructor name in the value namespace.
1499-
if let Some(ctor_node_id) = variant.data.ctor_node_id() {
1500-
let feed = self.r.feed(ctor_node_id);
1519+
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&variant.data) {
1520+
let feed = self.create_def(
1521+
ctor_node_id,
1522+
None,
1523+
DefKind::Ctor(CtorOf::Variant, ctor_kind),
1524+
variant.span,
1525+
);
15011526
let ctor_def_id = feed.key();
15021527
let ctor_res = self.res(ctor_def_id);
15031528
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id);

0 commit comments

Comments
 (0)