Skip to content

Commit fcefa95

Browse files
committed
Auto merge of #155587 - oli-obk:feed-visibility-no-hash, r=<try>
Immediately feed visibility on DefId creation
2 parents 84c1190 + 73120b8 commit fcefa95

5 files changed

Lines changed: 133 additions & 127 deletions

File tree

compiler/rustc_middle/src/ty/context.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,20 @@ impl<'tcx> TyCtxt<'tcx> {
702702
pub fn feed_delayed_owner(self, key: LocalDefId, owner: MaybeOwner<'tcx>) {
703703
TyCtxtFeed { tcx: self, key }.delayed_owner(owner);
704704
}
705+
706+
// Trait impl item visibility is inherited from its trait when not specified
707+
// explicitly. In that case we cannot determine it here in early resolve,
708+
// so we leave a hole in the visibility table to be filled later.
709+
// To avoid having to hash the
710+
pub fn feed_visibility_for_trait_impl_item(self, key: LocalDefId, vis: ty::Visibility) {
711+
if cfg!(debug_assertions) {
712+
match self.def_kind(self.local_parent(key)) {
713+
DefKind::Impl { of_trait: true } => {}
714+
other => bug!("{key:?} is not an assoc item of a trait impl: {other:?}"),
715+
}
716+
}
717+
TyCtxtFeed { tcx: self, key }.visibility(vis.to_def_id())
718+
}
705719
}
706720

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

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 49 additions & 25 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.
@@ -927,7 +929,15 @@ 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 (ctor_kind, _) = CtorKind::from_ast(vdata).unwrap();
934+
let feed = self.create_def(
935+
ctor_node_id,
936+
None,
937+
DefKind::Ctor(CtorOf::Struct, ctor_kind),
938+
item.span,
939+
);
940+
931941
let ctor_def_id = feed.key();
932942
let ctor_res = self.res(ctor_def_id);
933943
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
@@ -1062,8 +1072,8 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
10621072
&mut self,
10631073
item: &ForeignItem,
10641074
ident: Ident,
1075+
feed: TyCtxtFeed<'tcx, LocalDefId>,
10651076
) {
1066-
let feed = self.r.feed(item.id);
10671077
let local_def_id = feed.key();
10681078
let def_id = local_def_id.to_def_id();
10691079
let ns = match item.kind {
@@ -1259,10 +1269,13 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
12591269
}
12601270
}
12611271

1262-
fn define_macro(&mut self, item: &ast::Item) -> MacroRulesScopeRef<'ra> {
1272+
fn define_macro(
1273+
&mut self,
1274+
item: &ast::Item,
1275+
feed: TyCtxtFeed<'tcx, LocalDefId>,
1276+
) -> MacroRulesScopeRef<'ra> {
12631277
let parent_scope = self.parent_scope;
12641278
let expansion = parent_scope.expansion;
1265-
let feed = self.r.feed(item.id);
12661279
let def_id = feed.key();
12671280
let (res, orig_ident, span, macro_rules) = match &item.kind {
12681281
ItemKind::MacroDef(ident, def) => {
@@ -1361,18 +1374,17 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
13611374
}
13621375

13631376
impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
1364-
pub(crate) fn brg_visit_item(&mut self, item: &'a Item) {
1377+
pub(crate) fn brg_visit_item(&mut self, item: &'a Item, feed: TyCtxtFeed<'tcx, LocalDefId>) {
13651378
let orig_module_scope = self.parent_scope.module;
13661379
self.parent_scope.macro_rules = match item.kind {
13671380
ItemKind::MacroDef(..) => {
1368-
let macro_rules_scope = self.define_macro(item);
1381+
let macro_rules_scope = self.define_macro(item, feed);
13691382
visit::walk_item(self, item);
13701383
macro_rules_scope
13711384
}
1372-
ItemKind::MacCall(..) => self.visit_invoc_in_module(item.id),
13731385
_ => {
13741386
let orig_macro_rules_scope = self.parent_scope.macro_rules;
1375-
self.build_reduced_graph_for_item(item);
1387+
self.build_reduced_graph_for_item(item, feed);
13761388
match item.kind {
13771389
ItemKind::Mod(..) => {
13781390
// Visit attributes after items for backward compatibility.
@@ -1394,8 +1406,8 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
13941406
self.parent_scope.module = orig_module_scope;
13951407
}
13961408

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);
1409+
pub(crate) fn brg_visit_mac_call(&mut self, id: NodeId) {
1410+
self.parent_scope.macro_rules = self.visit_invoc_in_module(id);
13991411
}
14001412

14011413
pub(crate) fn brg_visit_block(&mut self, block: &'a Block) {
@@ -1413,9 +1425,9 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
14131425
ctxt: AssocCtxt,
14141426
ident: Ident,
14151427
ns: Namespace,
1428+
feed: TyCtxtFeed<'tcx, LocalDefId>,
14161429
) {
14171430
let vis = self.resolve_visibility(&item.vis);
1418-
let feed = self.r.feed(item.id);
14191431
let local_def_id = feed.key();
14201432
let def_id = local_def_id.to_def_id();
14211433

@@ -1467,21 +1479,28 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
14671479
}
14681480
}
14691481

1470-
pub(crate) fn brg_visit_field_def(&mut self, sf: &'a ast::FieldDef) {
1482+
pub(crate) fn brg_visit_field_def(
1483+
&mut self,
1484+
sf: &'a ast::FieldDef,
1485+
feed: TyCtxtFeed<'tcx, LocalDefId>,
1486+
) {
14711487
let vis = self.resolve_visibility(&sf.vis);
1472-
self.r.feed_visibility(self.r.feed(sf.id), vis);
1488+
self.r.feed_visibility(feed, vis);
14731489
visit::walk_field_def(self, sf);
14741490
}
14751491

14761492
// Constructs the reduced graph for one variant. Variants exist in the
14771493
// type and value namespaces.
1478-
pub(crate) fn brg_visit_variant(&mut self, variant: &'a ast::Variant) {
1494+
pub(crate) fn brg_visit_variant(
1495+
&mut self,
1496+
variant: &'a ast::Variant,
1497+
feed: TyCtxtFeed<'tcx, LocalDefId>,
1498+
) {
14791499
let parent = self.parent_scope.module.expect_local();
14801500
let expn_id = self.parent_scope.expansion;
14811501
let ident = variant.ident;
14821502

14831503
// Define a name in the type namespace.
1484-
let feed = self.r.feed(variant.id);
14851504
let def_id = feed.key();
14861505
let vis = self.resolve_visibility(&variant.vis);
14871506
self.r.define_local(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
@@ -1496,8 +1515,13 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
14961515
};
14971516

14981517
// 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);
1518+
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&variant.data) {
1519+
let feed = self.create_def(
1520+
ctor_node_id,
1521+
None,
1522+
DefKind::Ctor(CtorOf::Variant, ctor_kind),
1523+
variant.span,
1524+
);
15011525
let ctor_def_id = feed.key();
15021526
let ctor_res = self.res(ctor_def_id);
15031527
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id);

0 commit comments

Comments
 (0)