Skip to content

Commit 56ef997

Browse files
committed
Keep TyCtxtFeed around longer in the resolver
1 parent 937cff1 commit 56ef997

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ use std::cmp::Ordering;
7676
use std::fmt;
7777
use std::hash::{Hash, Hasher};
7878
use std::iter;
79+
use std::marker::PhantomData;
7980
use std::mem;
8081
use std::ops::{Bound, Deref};
8182

@@ -515,6 +516,23 @@ pub struct TyCtxtFeed<'tcx, KEY: Copy> {
515516
key: KEY,
516517
}
517518

519+
/// The same as `TyCtxtFeed`, but does not contain a `TyCtxt`.
520+
/// Use this to pass around when you have a `TyCtxt` elsewhere.
521+
/// Just an optimization to save space and not store hundreds of
522+
/// `TyCtxtFeed` in the resolver.
523+
#[derive(Copy, Clone)]
524+
pub struct Feed<'tcx, KEY: Copy> {
525+
_tcx: PhantomData<TyCtxt<'tcx>>,
526+
// Do not allow direct access, as downstream code must not mutate this field.
527+
key: KEY,
528+
}
529+
530+
impl<T: fmt::Debug + Copy> fmt::Debug for Feed<'_, T> {
531+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
532+
self.key.fmt(f)
533+
}
534+
}
535+
518536
impl<'tcx> TyCtxt<'tcx> {
519537
pub fn feed_unit_query(self) -> TyCtxtFeed<'tcx, ()> {
520538
TyCtxtFeed { tcx: self, key: () }
@@ -540,6 +558,23 @@ impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> {
540558
pub fn key(&self) -> KEY {
541559
self.key
542560
}
561+
562+
#[inline(always)]
563+
pub fn downgrade(self) -> Feed<'tcx, KEY> {
564+
Feed { _tcx: PhantomData, key: self.key }
565+
}
566+
}
567+
568+
impl<'tcx, KEY: Copy> Feed<'tcx, KEY> {
569+
#[inline(always)]
570+
pub fn key(&self) -> KEY {
571+
self.key
572+
}
573+
574+
#[inline(always)]
575+
pub fn upgrade(self, tcx: TyCtxt<'tcx>) -> TyCtxtFeed<'tcx, KEY> {
576+
TyCtxtFeed { tcx, key: self.key }
577+
}
543578
}
544579

545580
impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ pub use self::consts::{
8484
Const, ConstData, ConstInt, ConstKind, Expr, ScalarInt, UnevaluatedConst, ValTree,
8585
};
8686
pub use self::context::{
87-
tls, CtxtInterners, DeducedParamAttrs, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt, TyCtxtFeed,
87+
tls, CtxtInterners, DeducedParamAttrs, Feed, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt,
88+
TyCtxtFeed,
8889
};
8990
pub use self::instance::{Instance, InstanceDef, ShortInstance, UnusedGenericParams};
9091
pub use self::list::List;

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_ast::*;
44
use rustc_expand::expand::AstFragment;
55
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
66
use rustc_hir::def_id::LocalDefId;
7+
use rustc_middle::ty::Feed;
78
use rustc_span::hygiene::LocalExpnId;
89
use rustc_span::symbol::{kw, sym, Symbol};
910
use rustc_span::Span;
@@ -20,7 +21,7 @@ pub(crate) fn collect_definitions(
2021
/// Creates `DefId`s for nodes in the AST.
2122
struct DefCollector<'a, 'b, 'tcx> {
2223
resolver: &'a mut Resolver<'b, 'tcx>,
23-
parent_def: LocalDefId,
24+
parent_def: Feed<'tcx, LocalDefId>,
2425
impl_trait_context: ImplTraitContext,
2526
expansion: LocalExpnId,
2627
}
@@ -32,8 +33,8 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
3233
name: Symbol,
3334
def_kind: DefKind,
3435
span: Span,
35-
) -> LocalDefId {
36-
let parent_def = self.parent_def;
36+
) -> Feed<'tcx, LocalDefId> {
37+
let parent_def = self.parent_def.key();
3738
debug!(
3839
"create_def(node_id={:?}, def_kind={:?}, parent_def={:?})",
3940
node_id, def_kind, parent_def
@@ -47,10 +48,10 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
4748
self.expansion.to_expn_id(),
4849
span.with_parent(None),
4950
)
50-
.def_id()
51+
.downgrade()
5152
}
5253

53-
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {
54+
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: Feed<'tcx, LocalDefId>, f: F) {
5455
let orig_parent_def = std::mem::replace(&mut self.parent_def, parent_def);
5556
f(self);
5657
self.parent_def = orig_parent_def;
@@ -148,7 +149,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
148149
let def_id = self.create_def(i.id, i.ident.name, def_kind, i.span);
149150

150151
if let Some(macro_data) = opt_macro_data {
151-
self.resolver.macro_map.insert(def_id.to_def_id(), macro_data);
152+
self.resolver.macro_map.insert(def_id.key().to_def_id(), macro_data);
152153
}
153154

154155
self.with_parent(def_id, |this| {

compiler/rustc_resolve/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ 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, TyCtxtFeed};
55+
use rustc_middle::ty::{self, Feed, MainDefinition, RegisteredTools, TyCtxt, TyCtxtFeed};
5656
use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs};
5757
use rustc_query_system::ich::StableHashingContext;
5858
use rustc_session::lint::builtin::PRIVATE_MACRO_USE;
@@ -1106,7 +1106,7 @@ pub struct Resolver<'a, 'tcx> {
11061106
/// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId`
11071107
/// we know what parent node that fragment should be attached to thanks to this table,
11081108
/// and how the `impl Trait` fragments were introduced.
1109-
invocation_parents: FxHashMap<LocalExpnId, (LocalDefId, ImplTraitContext)>,
1109+
invocation_parents: FxHashMap<LocalExpnId, (Feed<'tcx, LocalDefId>, ImplTraitContext)>,
11101110

11111111
/// Some way to know that we are in a *trait* impl in `visit_assoc_item`.
11121112
/// FIXME: Replace with a more general AST map (together with some other fields).
@@ -1318,7 +1318,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13181318
node_id_to_def_id.insert(CRATE_NODE_ID, CRATE_DEF_ID);
13191319

13201320
let mut invocation_parents = FxHashMap::default();
1321-
invocation_parents.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, ImplTraitContext::Existential));
1321+
invocation_parents.insert(
1322+
LocalExpnId::ROOT,
1323+
(tcx.feed_local_def_id(CRATE_DEF_ID).downgrade(), ImplTraitContext::Existential),
1324+
);
13221325

13231326
let mut extern_prelude: FxHashMap<Ident, ExternPreludeEntry<'_>> = tcx
13241327
.sess

compiler/rustc_resolve/src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
174174
}
175175

176176
fn invocation_parent(&self, id: LocalExpnId) -> LocalDefId {
177-
self.invocation_parents[&id].0
177+
self.invocation_parents[&id].0.key()
178178
}
179179

180180
fn resolve_dollar_crates(&mut self) {
@@ -804,7 +804,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
804804
let node_id = self
805805
.invocation_parents
806806
.get(&parent_scope.expansion)
807-
.map_or(ast::CRATE_NODE_ID, |id| self.def_id_to_node_id[id.0]);
807+
.map_or(ast::CRATE_NODE_ID, |id| self.def_id_to_node_id[id.0.key()]);
808808
self.lint_buffer.buffer_lint_with_diagnostic(
809809
LEGACY_DERIVE_HELPERS,
810810
node_id,

0 commit comments

Comments
 (0)