Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e9ddc23

Browse files
authoredMar 8, 2024
Unrolled build for rust-lang#121089
Rollup merge of rust-lang#121089 - oli-obk:create_def_feed, r=petrochenkov Remove `feed_local_def_id` best reviewed commit by commit Basically I returned `TyCtxtFeed` from `create_def` and then preserved that in the local caches based on rust-lang#121084 r? ````@petrochenkov````
2 parents 9c3ad80 + ef00fae commit e9ddc23

File tree

15 files changed

+181
-86
lines changed

15 files changed

+181
-86
lines changed
 

‎compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
427427
tcx.ensure_with_value().early_lint_checks(());
428428
tcx.ensure_with_value().debugger_visualizers(LOCAL_CRATE);
429429
tcx.ensure_with_value().get_lang_items(());
430-
let (mut resolver, krate) = tcx.resolver_for_lowering(()).steal();
430+
let (mut resolver, krate) = tcx.resolver_for_lowering().steal();
431431

432432
let ast_index = index_crate(&resolver.node_id_to_def_id, &krate);
433433
let mut owners = IndexVec::from_fn_n(

‎compiler/rustc_driver_impl/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ fn run_compiler(
418418
}
419419

420420
// Make sure name resolution and macro expansion is run.
421-
queries.global_ctxt()?.enter(|tcx| tcx.resolver_for_lowering(()));
421+
queries.global_ctxt()?.enter(|tcx| tcx.resolver_for_lowering());
422422

423423
if callbacks.after_expansion(compiler, queries) == Compilation::Stop {
424424
return early_exit();

‎compiler/rustc_driver_impl/src/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<'tcx> PrintExtra<'tcx> {
229229
{
230230
match self {
231231
PrintExtra::AfterParsing { krate, .. } => f(krate),
232-
PrintExtra::NeedsAstMap { tcx } => f(&tcx.resolver_for_lowering(()).borrow().1),
232+
PrintExtra::NeedsAstMap { tcx } => f(&tcx.resolver_for_lowering().borrow().1),
233233
}
234234
}
235235

@@ -281,7 +281,7 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
281281
}
282282
AstTreeExpanded => {
283283
debug!("pretty-printing expanded AST");
284-
format!("{:#?}", ex.tcx().resolver_for_lowering(()).borrow().1)
284+
format!("{:#?}", ex.tcx().resolver_for_lowering().borrow().1)
285285
}
286286
Hir(s) => {
287287
debug!("pretty printing HIR {:?}", s);

‎compiler/rustc_interface/src/passes.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fn configure_and_expand(
280280

281281
fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
282282
let sess = tcx.sess;
283-
let (resolver, krate) = &*tcx.resolver_for_lowering(()).borrow();
283+
let (resolver, krate) = &*tcx.resolver_for_lowering().borrow();
284284
let mut lint_buffer = resolver.lint_buffer.steal();
285285

286286
if sess.opts.unstable_opts.input_stats {
@@ -531,10 +531,10 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
531531
}
532532
}
533533

534-
fn resolver_for_lowering<'tcx>(
534+
fn resolver_for_lowering_raw<'tcx>(
535535
tcx: TyCtxt<'tcx>,
536536
(): (),
537-
) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)> {
537+
) -> (&'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)>, &'tcx ty::ResolverGlobalCtxt) {
538538
let arenas = Resolver::arenas();
539539
let _ = tcx.registered_tools(()); // Uses `crate_for_resolver`.
540540
let (krate, pre_configured_attrs) = tcx.crate_for_resolver(()).steal();
@@ -549,16 +549,15 @@ fn resolver_for_lowering<'tcx>(
549549
ast_lowering: untracked_resolver_for_lowering,
550550
} = resolver.into_outputs();
551551

552-
let feed = tcx.feed_unit_query();
553-
feed.resolutions(tcx.arena.alloc(untracked_resolutions));
554-
tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Lrc::new(krate))))
552+
let resolutions = tcx.arena.alloc(untracked_resolutions);
553+
(tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Lrc::new(krate)))), resolutions)
555554
}
556555

557556
pub(crate) fn write_dep_info(tcx: TyCtxt<'_>) {
558557
// Make sure name resolution and macro expansion is run for
559558
// the side-effect of providing a complete set of all
560559
// accessed files and env vars.
561-
let _ = tcx.resolver_for_lowering(());
560+
let _ = tcx.resolver_for_lowering();
562561

563562
let sess = tcx.sess;
564563
let _timer = sess.timer("write_dep_info");
@@ -607,7 +606,10 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
607606
let providers = &mut Providers::default();
608607
providers.analysis = analysis;
609608
providers.hir_crate = rustc_ast_lowering::lower_to_hir;
610-
providers.resolver_for_lowering = resolver_for_lowering;
609+
providers.resolver_for_lowering_raw = resolver_for_lowering_raw;
610+
providers.stripped_cfg_items =
611+
|tcx, _| tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal());
612+
providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1;
611613
providers.early_lint_checks = early_lint_checks;
612614
proc_macro_decls::provide(providers);
613615
rustc_const_eval::provide(providers);

‎compiler/rustc_interface/src/queries.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use rustc_codegen_ssa::CodegenResults;
88
use rustc_data_structures::steal::Steal;
99
use rustc_data_structures::svh::Svh;
1010
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, OnceLock, WorkerLocal};
11-
use rustc_hir::def::DefKind;
12-
use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
11+
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
1312
use rustc_hir::definitions::Definitions;
1413
use rustc_incremental::setup_dep_graph;
1514
use rustc_metadata::creader::CStore;
@@ -144,10 +143,8 @@ impl<'tcx> Queries<'tcx> {
144143
stable_crate_id,
145144
)) as _);
146145
let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
147-
let source_span = AppendOnlyIndexVec::new();
148-
let _id = source_span.push(krate.spans.inner_span);
149-
debug_assert_eq!(_id, CRATE_DEF_ID);
150-
let untracked = Untracked { cstore, source_span, definitions };
146+
let untracked =
147+
Untracked { cstore, source_span: AppendOnlyIndexVec::new(), definitions };
151148

152149
let qcx = passes::create_global_ctxt(
153150
self.compiler,
@@ -172,9 +169,6 @@ impl<'tcx> Queries<'tcx> {
172169
)));
173170
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
174171
feed.output_filenames(Arc::new(outputs));
175-
176-
let feed = tcx.feed_local_def_id(CRATE_DEF_ID);
177-
feed.def_kind(DefKind::Mod);
178172
});
179173
Ok(qcx)
180174
})

‎compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,11 @@ rustc_queries! {
125125
}
126126

127127
query resolutions(_: ()) -> &'tcx ty::ResolverGlobalCtxt {
128-
feedable
129128
no_hash
130129
desc { "getting the resolver outputs" }
131130
}
132131

133-
query resolver_for_lowering(_: ()) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)> {
132+
query resolver_for_lowering_raw(_: ()) -> (&'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)>, &'tcx ty::ResolverGlobalCtxt) {
134133
eval_always
135134
no_hash
136135
desc { "getting the resolver for lowering" }
@@ -2216,7 +2215,6 @@ rustc_queries! {
22162215
/// Should not be called for the local crate before the resolver outputs are created, as it
22172216
/// is only fed there.
22182217
query stripped_cfg_items(cnum: CrateNum) -> &'tcx [StrippedCfgItem] {
2219-
feedable
22202218
desc { "getting cfg-ed out item names" }
22212219
separate_provide_extern
22222220
}

‎compiler/rustc_middle/src/ty/context.rs

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use rustc_data_structures::profiling::SelfProfilerRef;
3939
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
4040
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4141
use rustc_data_structures::steal::Steal;
42-
use rustc_data_structures::sync::{self, FreezeReadGuard, Lock, WorkerLocal};
42+
use rustc_data_structures::sync::{self, FreezeReadGuard, Lock, Lrc, WorkerLocal};
4343
#[cfg(parallel_compiler)]
4444
use rustc_data_structures::sync::{DynSend, DynSync};
4545
use rustc_data_structures::unord::UnordSet;
@@ -60,7 +60,7 @@ use rustc_session::config::CrateType;
6060
use rustc_session::cstore::{CrateStoreDyn, Untracked};
6161
use rustc_session::lint::Lint;
6262
use rustc_session::{Limit, MetadataKind, Session};
63-
use rustc_span::def_id::{DefPathHash, StableCrateId};
63+
use rustc_span::def_id::{DefPathHash, StableCrateId, CRATE_DEF_ID};
6464
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6565
use rustc_span::{Span, DUMMY_SP};
6666
use rustc_target::abi::{FieldIdx, Layout, LayoutS, TargetDataLayout, VariantIdx};
@@ -74,6 +74,7 @@ use std::cmp::Ordering;
7474
use std::fmt;
7575
use std::hash::{Hash, Hasher};
7676
use std::iter;
77+
use std::marker::PhantomData;
7778
use std::mem;
7879
use std::ops::{Bound, Deref};
7980

@@ -498,14 +499,55 @@ pub struct TyCtxtFeed<'tcx, KEY: Copy> {
498499
key: KEY,
499500
}
500501

502+
/// Never return a `Feed` from a query. Only queries that create a `DefId` are
503+
/// allowed to feed queries for that `DefId`.
504+
impl<KEY: Copy, CTX> !HashStable<CTX> for TyCtxtFeed<'_, KEY> {}
505+
506+
/// The same as `TyCtxtFeed`, but does not contain a `TyCtxt`.
507+
/// Use this to pass around when you have a `TyCtxt` elsewhere.
508+
/// Just an optimization to save space and not store hundreds of
509+
/// `TyCtxtFeed` in the resolver.
510+
#[derive(Copy, Clone)]
511+
pub struct Feed<'tcx, KEY: Copy> {
512+
_tcx: PhantomData<TyCtxt<'tcx>>,
513+
// Do not allow direct access, as downstream code must not mutate this field.
514+
key: KEY,
515+
}
516+
517+
/// Never return a `Feed` from a query. Only queries that create a `DefId` are
518+
/// allowed to feed queries for that `DefId`.
519+
impl<KEY: Copy, CTX> !HashStable<CTX> for Feed<'_, KEY> {}
520+
521+
impl<T: fmt::Debug + Copy> fmt::Debug for Feed<'_, T> {
522+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
523+
self.key.fmt(f)
524+
}
525+
}
526+
527+
/// Some workarounds to use cases that cannot use `create_def`.
528+
/// Do not add new ways to create `TyCtxtFeed` without consulting
529+
/// with T-compiler and making an analysis about why your addition
530+
/// does not cause incremental compilation issues.
501531
impl<'tcx> TyCtxt<'tcx> {
532+
/// Can only be fed before queries are run, and is thus exempt from any
533+
/// incremental issues. Do not use except for the initial query feeding.
502534
pub fn feed_unit_query(self) -> TyCtxtFeed<'tcx, ()> {
535+
self.dep_graph.assert_ignored();
503536
TyCtxtFeed { tcx: self, key: () }
504537
}
538+
539+
/// Can only be fed before queries are run, and is thus exempt from any
540+
/// incremental issues. Do not use except for the initial query feeding.
505541
pub fn feed_local_crate(self) -> TyCtxtFeed<'tcx, CrateNum> {
542+
self.dep_graph.assert_ignored();
506543
TyCtxtFeed { tcx: self, key: LOCAL_CRATE }
507544
}
508-
pub fn feed_local_def_id(self, key: LocalDefId) -> TyCtxtFeed<'tcx, LocalDefId> {
545+
546+
/// Only used in the resolver to register the `CRATE_DEF_ID` `DefId` and feed
547+
/// some queries for it. It will panic if used twice.
548+
pub fn create_local_crate_def_id(self, span: Span) -> TyCtxtFeed<'tcx, LocalDefId> {
549+
let key = self.untracked().source_span.push(span);
550+
assert_eq!(key, CRATE_DEF_ID);
509551
TyCtxtFeed { tcx: self, key }
510552
}
511553

@@ -523,6 +565,23 @@ impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> {
523565
pub fn key(&self) -> KEY {
524566
self.key
525567
}
568+
569+
#[inline(always)]
570+
pub fn downgrade(self) -> Feed<'tcx, KEY> {
571+
Feed { _tcx: PhantomData, key: self.key }
572+
}
573+
}
574+
575+
impl<'tcx, KEY: Copy> Feed<'tcx, KEY> {
576+
#[inline(always)]
577+
pub fn key(&self) -> KEY {
578+
self.key
579+
}
580+
581+
#[inline(always)]
582+
pub fn upgrade(self, tcx: TyCtxt<'tcx>) -> TyCtxtFeed<'tcx, KEY> {
583+
TyCtxtFeed { tcx, key: self.key }
584+
}
526585
}
527586

528587
impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
@@ -1067,7 +1126,7 @@ impl<'tcx> TyCtxt<'tcx> {
10671126
// needs to be re-evaluated.
10681127
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
10691128

1070-
let feed = self.feed_local_def_id(def_id);
1129+
let feed = TyCtxtFeed { tcx: self, key: def_id };
10711130
feed.def_kind(def_kind);
10721131
// Unique types created for closures participate in type privacy checking.
10731132
// They have visibilities inherited from the module they are defined in.
@@ -2304,6 +2363,10 @@ impl<'tcx> TyCtxt<'tcx> {
23042363
self.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..])
23052364
}
23062365

2366+
pub fn resolver_for_lowering(self) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)> {
2367+
self.resolver_for_lowering_raw(()).0
2368+
}
2369+
23072370
/// Given an `impl_id`, return the trait it implements.
23082371
/// Return `None` if this is an inherent impl.
23092372
pub fn impl_trait_ref(

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub use generic_args::*;
3232
pub use generics::*;
3333
pub use intrinsic::IntrinsicDef;
3434
use rustc_ast as ast;
35+
use rustc_ast::expand::StrippedCfgItem;
3536
use rustc_ast::node_id::NodeMap;
3637
pub use rustc_ast_ir::{try_visit, Movability, Mutability};
3738
use rustc_attr as attr;
@@ -85,7 +86,8 @@ pub use self::consts::{
8586
Const, ConstData, ConstInt, ConstKind, Expr, ScalarInt, UnevaluatedConst, ValTree,
8687
};
8788
pub use self::context::{
88-
tls, CtxtInterners, DeducedParamAttrs, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt, TyCtxtFeed,
89+
tls, CtxtInterners, DeducedParamAttrs, Feed, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt,
90+
TyCtxtFeed,
8991
};
9092
pub use self::instance::{Instance, InstanceDef, ShortInstance, UnusedGenericParams};
9193
pub use self::list::List;
@@ -189,6 +191,7 @@ pub struct ResolverGlobalCtxt {
189191
pub doc_link_resolutions: FxHashMap<LocalDefId, DocLinkResMap>,
190192
pub doc_link_traits_in_scope: FxHashMap<LocalDefId, Vec<DefId>>,
191193
pub all_macro_rules: FxHashMap<Symbol, Res<ast::NodeId>>,
194+
pub stripped_cfg_items: Steal<Vec<StrippedCfgItem>>,
192195
}
193196

194197
/// Resolutions that should only be used for lowering.

‎compiler/rustc_passes/src/debugger_visualizer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'ast> rustc_ast::visit::Visitor<'ast> for DebuggerVisualizerCollector<'_> {
8787

8888
/// Traverses and collects the debugger visualizers for a specific crate.
8989
fn debugger_visualizers(tcx: TyCtxt<'_>, _: LocalCrate) -> Vec<DebuggerVisualizerFile> {
90-
let resolver_and_krate = tcx.resolver_for_lowering(()).borrow();
90+
let resolver_and_krate = tcx.resolver_for_lowering().borrow();
9191
let krate = &*resolver_and_krate.1;
9292

9393
let mut visitor = DebuggerVisualizerCollector { sess: tcx.sess, visualizers: Vec::new() };

‎compiler/rustc_passes/src/lang_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> {
243243

244244
/// Traverses and collects all the lang items in all crates.
245245
fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
246-
let resolver = tcx.resolver_for_lowering(()).borrow();
246+
let resolver = tcx.resolver_for_lowering().borrow();
247247
let (resolver, krate) = &*resolver;
248248

249249
// Initialize the collector.

‎compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_hir::def::{self, *};
2525
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
2626
use rustc_metadata::creader::LoadedMacro;
2727
use rustc_middle::metadata::ModChild;
28+
use rustc_middle::ty::Feed;
2829
use rustc_middle::{bug, ty};
2930
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
3031
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -407,7 +408,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
407408
// Top level use tree reuses the item's id and list stems reuse their parent
408409
// use tree's ids, so in both cases their visibilities are already filled.
409410
if nested && !list_stem {
410-
self.r.feed_visibility(self.r.local_def_id(id), vis);
411+
self.r.feed_visibility(self.r.feed(id), vis);
411412
}
412413

413414
let mut prefix_iter = parent_prefix
@@ -632,7 +633,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
632633
&mut self,
633634
fields: &[ast::FieldDef],
634635
ident: Ident,
635-
def_id: LocalDefId,
636+
feed: Feed<'tcx, LocalDefId>,
636637
adt_res: Res,
637638
adt_vis: ty::Visibility,
638639
adt_span: Span,
@@ -643,7 +644,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
643644

644645
// Define a name in the type namespace if it is not anonymous.
645646
self.r.define(parent, ident, TypeNS, (adt_res, adt_vis, adt_span, expansion));
646-
self.r.feed_visibility(def_id, adt_vis);
647+
self.r.feed_visibility(feed, adt_vis);
648+
let def_id = feed.key();
647649

648650
// Record field names for error reporting.
649651
self.insert_field_def_ids(def_id, fields);
@@ -653,14 +655,15 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
653655
match &field.ty.kind {
654656
ast::TyKind::AnonStruct(id, nested_fields)
655657
| ast::TyKind::AnonUnion(id, nested_fields) => {
656-
let local_def_id = self.r.local_def_id(*id);
658+
let feed = self.r.feed(*id);
659+
let local_def_id = feed.key();
657660
let def_id = local_def_id.to_def_id();
658661
let def_kind = self.r.tcx.def_kind(local_def_id);
659662
let res = Res::Def(def_kind, def_id);
660663
self.build_reduced_graph_for_struct_variant(
661664
&nested_fields,
662665
Ident::empty(),
663-
local_def_id,
666+
feed,
664667
res,
665668
// Anonymous adts inherit visibility from their parent adts.
666669
adt_vis,
@@ -680,12 +683,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
680683
let ident = item.ident;
681684
let sp = item.span;
682685
let vis = self.resolve_visibility(&item.vis);
683-
let local_def_id = self.r.local_def_id(item.id);
686+
let feed = self.r.feed(item.id);
687+
let local_def_id = feed.key();
684688
let def_id = local_def_id.to_def_id();
685689
let def_kind = self.r.tcx.def_kind(def_id);
686690
let res = Res::Def(def_kind, def_id);
687691

688-
self.r.feed_visibility(local_def_id, vis);
692+
self.r.feed_visibility(feed, vis);
689693

690694
match item.kind {
691695
ItemKind::Use(ref use_tree) => {
@@ -762,7 +766,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
762766
self.build_reduced_graph_for_struct_variant(
763767
vdata.fields(),
764768
ident,
765-
local_def_id,
769+
feed,
766770
res,
767771
vis,
768772
sp,
@@ -795,10 +799,11 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
795799
}
796800
ret_fields.push(field_vis.to_def_id());
797801
}
798-
let ctor_def_id = self.r.local_def_id(ctor_node_id);
802+
let feed = self.r.feed(ctor_node_id);
803+
let ctor_def_id = feed.key();
799804
let ctor_res = self.res(ctor_def_id);
800805
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
801-
self.r.feed_visibility(ctor_def_id, ctor_vis);
806+
self.r.feed_visibility(feed, ctor_vis);
802807
// We need the field visibility spans also for the constructor for E0603.
803808
self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata.fields());
804809

@@ -812,7 +817,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
812817
self.build_reduced_graph_for_struct_variant(
813818
vdata.fields(),
814819
ident,
815-
local_def_id,
820+
feed,
816821
res,
817822
vis,
818823
sp,
@@ -919,7 +924,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
919924

920925
/// Constructs the reduced graph for one foreign item.
921926
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
922-
let local_def_id = self.r.local_def_id(item.id);
927+
let feed = self.r.feed(item.id);
928+
let local_def_id = feed.key();
923929
let def_id = local_def_id.to_def_id();
924930
let ns = match item.kind {
925931
ForeignItemKind::Fn(..) => ValueNS,
@@ -931,7 +937,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
931937
let expansion = self.parent_scope.expansion;
932938
let vis = self.resolve_visibility(&item.vis);
933939
self.r.define(parent, item.ident, ns, (self.res(def_id), vis, item.span, expansion));
934-
self.r.feed_visibility(local_def_id, vis);
940+
self.r.feed_visibility(feed, vis);
935941
}
936942

937943
fn build_reduced_graph_for_block(&mut self, block: &Block) {
@@ -1218,7 +1224,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
12181224
fn define_macro(&mut self, item: &ast::Item) -> MacroRulesScopeRef<'a> {
12191225
let parent_scope = self.parent_scope;
12201226
let expansion = parent_scope.expansion;
1221-
let def_id = self.r.local_def_id(item.id);
1227+
let feed = self.r.feed(item.id);
1228+
let def_id = feed.key();
12221229
let (res, ident, span, macro_rules) = match &item.kind {
12231230
ItemKind::MacroDef(def) => (self.res(def_id), item.ident, item.span, def.macro_rules),
12241231
ItemKind::Fn(..) => match self.proc_macro_stub(item) {
@@ -1269,7 +1276,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
12691276
self.r.check_reserved_macro_name(ident, res);
12701277
self.insert_unused_macro(ident, def_id, item.id);
12711278
}
1272-
self.r.feed_visibility(def_id, vis);
1279+
self.r.feed_visibility(feed, vis);
12731280
let scope = self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Binding(
12741281
self.r.arenas.alloc_macro_rules_binding(MacroRulesBinding {
12751282
parent_macro_rules_scope: parent_scope.macro_rules,
@@ -1293,7 +1300,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
12931300
self.insert_unused_macro(ident, def_id, item.id);
12941301
}
12951302
self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
1296-
self.r.feed_visibility(def_id, vis);
1303+
self.r.feed_visibility(feed, vis);
12971304
self.parent_scope.macro_rules
12981305
}
12991306
}
@@ -1385,7 +1392,8 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
13851392
}
13861393

13871394
let vis = self.resolve_visibility(&item.vis);
1388-
let local_def_id = self.r.local_def_id(item.id);
1395+
let feed = self.r.feed(item.id);
1396+
let local_def_id = feed.key();
13891397
let def_id = local_def_id.to_def_id();
13901398

13911399
if !(ctxt == AssocCtxt::Impl
@@ -1395,7 +1403,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
13951403
// Trait impl item visibility is inherited from its trait when not specified
13961404
// explicitly. In that case we cannot determine it here in early resolve,
13971405
// so we leave a hole in the visibility table to be filled later.
1398-
self.r.feed_visibility(local_def_id, vis);
1406+
self.r.feed_visibility(feed, vis);
13991407
}
14001408

14011409
if ctxt == AssocCtxt::Trait {
@@ -1469,7 +1477,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
14691477
self.visit_invoc(sf.id);
14701478
} else {
14711479
let vis = self.resolve_visibility(&sf.vis);
1472-
self.r.feed_visibility(self.r.local_def_id(sf.id), vis);
1480+
self.r.feed_visibility(self.r.feed(sf.id), vis);
14731481
visit::walk_field_def(self, sf);
14741482
}
14751483
}
@@ -1487,10 +1495,11 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
14871495
let ident = variant.ident;
14881496

14891497
// Define a name in the type namespace.
1490-
let def_id = self.r.local_def_id(variant.id);
1498+
let feed = self.r.feed(variant.id);
1499+
let def_id = feed.key();
14911500
let vis = self.resolve_visibility(&variant.vis);
14921501
self.r.define(parent, ident, TypeNS, (self.res(def_id), vis, variant.span, expn_id));
1493-
self.r.feed_visibility(def_id, vis);
1502+
self.r.feed_visibility(feed, vis);
14941503

14951504
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
14961505
let ctor_vis =
@@ -1502,10 +1511,11 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
15021511

15031512
// Define a constructor name in the value namespace.
15041513
if let Some(ctor_node_id) = variant.data.ctor_node_id() {
1505-
let ctor_def_id = self.r.local_def_id(ctor_node_id);
1514+
let feed = self.r.feed(ctor_node_id);
1515+
let ctor_def_id = feed.key();
15061516
let ctor_res = self.res(ctor_def_id);
15071517
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));
1508-
self.r.feed_visibility(ctor_def_id, ctor_vis);
1518+
self.r.feed_visibility(feed, ctor_vis);
15091519
}
15101520

15111521
// Record field names for error reporting.

‎compiler/rustc_resolve/src/def_collector.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
3838
"create_def(node_id={:?}, def_kind={:?}, parent_def={:?})",
3939
node_id, def_kind, parent_def
4040
);
41-
self.resolver.create_def(
42-
parent_def,
43-
node_id,
44-
name,
45-
def_kind,
46-
self.expansion.to_expn_id(),
47-
span.with_parent(None),
48-
)
41+
self.resolver
42+
.create_def(
43+
parent_def,
44+
node_id,
45+
name,
46+
def_kind,
47+
self.expansion.to_expn_id(),
48+
span.with_parent(None),
49+
)
50+
.def_id()
4951
}
5052

5153
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {

‎compiler/rustc_resolve/src/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3121,7 +3121,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
31213121
);
31223122
rustc_middle::ty::Visibility::Public
31233123
};
3124-
this.r.feed_visibility(this.r.local_def_id(id), vis);
3124+
this.r.feed_visibility(this.r.feed(id), vis);
31253125
};
31263126

31273127
let Some(binding) = binding else {

‎compiler/rustc_resolve/src/lib.rs

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ use rustc_middle::metadata::ModChild;
5252
use rustc_middle::middle::privacy::EffectiveVisibilities;
5353
use rustc_middle::query::Providers;
5454
use rustc_middle::span_bug;
55-
use rustc_middle::ty::{self, MainDefinition, RegisteredTools, TyCtxt};
56-
use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs};
55+
use rustc_middle::ty::{self, MainDefinition, RegisteredTools, TyCtxt, TyCtxtFeed};
56+
use rustc_middle::ty::{Feed, ResolverGlobalCtxt, ResolverOutputs};
5757
use rustc_query_system::ich::StableHashingContext;
5858
use rustc_session::lint::builtin::PRIVATE_MACRO_USE;
5959
use rustc_session::lint::LintBuffer;
@@ -1117,7 +1117,7 @@ pub struct Resolver<'a, 'tcx> {
11171117

11181118
next_node_id: NodeId,
11191119

1120-
node_id_to_def_id: NodeMap<LocalDefId>,
1120+
node_id_to_def_id: NodeMap<Feed<'tcx, LocalDefId>>,
11211121
def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
11221122

11231123
/// Indices of unnamed struct or variant fields with unresolved attributes.
@@ -1233,11 +1233,19 @@ impl<'a, 'tcx> AsMut<Resolver<'a, 'tcx>> for Resolver<'a, 'tcx> {
12331233

12341234
impl<'tcx> Resolver<'_, 'tcx> {
12351235
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
1236-
self.node_id_to_def_id.get(&node).copied()
1236+
self.opt_feed(node).map(|f| f.key())
12371237
}
12381238

12391239
fn local_def_id(&self, node: NodeId) -> LocalDefId {
1240-
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{node:?}`"))
1240+
self.feed(node).key()
1241+
}
1242+
1243+
fn opt_feed(&self, node: NodeId) -> Option<Feed<'tcx, LocalDefId>> {
1244+
self.node_id_to_def_id.get(&node).copied()
1245+
}
1246+
1247+
fn feed(&self, node: NodeId) -> Feed<'tcx, LocalDefId> {
1248+
self.opt_feed(node).unwrap_or_else(|| panic!("no entry for node id: `{node:?}`"))
12411249
}
12421250

12431251
fn local_def_kind(&self, node: NodeId) -> DefKind {
@@ -1253,18 +1261,19 @@ impl<'tcx> Resolver<'_, 'tcx> {
12531261
def_kind: DefKind,
12541262
expn_id: ExpnId,
12551263
span: Span,
1256-
) -> LocalDefId {
1264+
) -> TyCtxtFeed<'tcx, LocalDefId> {
12571265
let data = def_kind.def_path_data(name);
12581266
assert!(
12591267
!self.node_id_to_def_id.contains_key(&node_id),
12601268
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
12611269
node_id,
12621270
data,
1263-
self.tcx.definitions_untracked().def_key(self.node_id_to_def_id[&node_id]),
1271+
self.tcx.definitions_untracked().def_key(self.node_id_to_def_id[&node_id].key()),
12641272
);
12651273

12661274
// FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()`
1267-
let def_id = self.tcx.create_def(parent, name, def_kind).def_id();
1275+
let feed = self.tcx.create_def(parent, name, def_kind);
1276+
let def_id = feed.def_id();
12681277

12691278
// Create the definition.
12701279
if expn_id != ExpnId::root() {
@@ -1281,11 +1290,11 @@ impl<'tcx> Resolver<'_, 'tcx> {
12811290
// we don't need a mapping from `NodeId` to `LocalDefId`.
12821291
if node_id != ast::DUMMY_NODE_ID {
12831292
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
1284-
self.node_id_to_def_id.insert(node_id, def_id);
1293+
self.node_id_to_def_id.insert(node_id, feed.downgrade());
12851294
}
12861295
assert_eq!(self.def_id_to_node_id.push(node_id), def_id);
12871296

1288-
def_id
1297+
feed
12891298
}
12901299

12911300
fn item_generics_num_lifetimes(&self, def_id: DefId) -> usize {
@@ -1333,7 +1342,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13331342
let mut def_id_to_node_id = IndexVec::default();
13341343
assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), CRATE_DEF_ID);
13351344
let mut node_id_to_def_id = NodeMap::default();
1336-
node_id_to_def_id.insert(CRATE_NODE_ID, CRATE_DEF_ID);
1345+
let crate_feed = tcx.create_local_crate_def_id(crate_span);
1346+
1347+
crate_feed.def_kind(DefKind::Mod);
1348+
let crate_feed = crate_feed.downgrade();
1349+
node_id_to_def_id.insert(CRATE_NODE_ID, crate_feed);
13371350

13381351
let mut invocation_parents = FxHashMap::default();
13391352
invocation_parents.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, ImplTraitContext::Existential));
@@ -1484,7 +1497,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14841497

14851498
let root_parent_scope = ParentScope::module(graph_root, &resolver);
14861499
resolver.invocation_parent_scopes.insert(LocalExpnId::ROOT, root_parent_scope);
1487-
resolver.feed_visibility(CRATE_DEF_ID, ty::Visibility::Public);
1500+
resolver.feed_visibility(crate_feed, ty::Visibility::Public);
14881501

14891502
resolver
14901503
}
@@ -1532,9 +1545,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15321545
Default::default()
15331546
}
15341547

1535-
fn feed_visibility(&mut self, def_id: LocalDefId, vis: ty::Visibility) {
1536-
self.tcx.feed_local_def_id(def_id).visibility(vis.to_def_id());
1537-
self.visibilities_for_hashing.push((def_id, vis));
1548+
fn feed_visibility(&mut self, feed: Feed<'tcx, LocalDefId>, vis: ty::Visibility) {
1549+
let feed = feed.upgrade(self.tcx);
1550+
feed.visibility(vis.to_def_id());
1551+
self.visibilities_for_hashing.push((feed.def_id(), vis));
15381552
}
15391553

15401554
pub fn into_outputs(self) -> ResolverOutputs {
@@ -1547,12 +1561,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15471561
let confused_type_with_std_module = self.confused_type_with_std_module;
15481562
let effective_visibilities = self.effective_visibilities;
15491563

1550-
self.tcx.feed_local_crate().stripped_cfg_items(self.tcx.arena.alloc_from_iter(
1551-
self.stripped_cfg_items.into_iter().filter_map(|item| {
1552-
let parent_module = self.node_id_to_def_id.get(&item.parent_module)?.to_def_id();
1553-
Some(StrippedCfgItem { parent_module, name: item.name, cfg: item.cfg })
1554-
}),
1555-
));
1564+
let stripped_cfg_items = Steal::new(
1565+
self.stripped_cfg_items
1566+
.into_iter()
1567+
.filter_map(|item| {
1568+
let parent_module =
1569+
self.node_id_to_def_id.get(&item.parent_module)?.key().to_def_id();
1570+
Some(StrippedCfgItem { parent_module, name: item.name, cfg: item.cfg })
1571+
})
1572+
.collect(),
1573+
);
15561574

15571575
let global_ctxt = ResolverGlobalCtxt {
15581576
expn_that_defined,
@@ -1569,6 +1587,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15691587
doc_link_resolutions: self.doc_link_resolutions,
15701588
doc_link_traits_in_scope: self.doc_link_traits_in_scope,
15711589
all_macro_rules: self.all_macro_rules,
1590+
stripped_cfg_items,
15721591
};
15731592
let ast_lowering = ty::ResolverAstLowering {
15741593
legacy_const_generic_args: self.legacy_const_generic_args,
@@ -1578,7 +1597,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15781597
lifetimes_res_map: self.lifetimes_res_map,
15791598
extra_lifetime_params_map: self.extra_lifetime_params_map,
15801599
next_node_id: self.next_node_id,
1581-
node_id_to_def_id: self.node_id_to_def_id,
1600+
node_id_to_def_id: self
1601+
.node_id_to_def_id
1602+
.into_items()
1603+
.map(|(k, f)| (k, f.key()))
1604+
.collect(),
15821605
def_id_to_node_id: self.def_id_to_node_id,
15831606
trait_map: self.trait_map,
15841607
lifetime_elision_allowed: self.lifetime_elision_allowed,

‎tests/ui/panics/default-backtrace-ice.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ error: the compiler unexpectedly panicked. this is a bug.
2121

2222

2323
query stack during panic:
24-
#0 [resolver_for_lowering] getting the resolver for lowering
24+
#0 [resolver_for_lowering_raw] getting the resolver for lowering
2525
end of query stack

0 commit comments

Comments
 (0)
This repository has been archived.