Skip to content

Commit aad9995

Browse files
committed
Keep TyCtxtFeed around longer in the resolver
1 parent d07f6c5 commit aad9995

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

compiler/rustc_middle/src/ty/context.rs

+35
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

@@ -522,6 +523,23 @@ pub struct TyCtxtFeed<'tcx, KEY: Copy> {
522523
key: KEY,
523524
}
524525

526+
/// The same as `TyCtxtFeed`, but does not contain a `TyCtxt`.
527+
/// Use this to pass around when you have a `TyCtxt` elsewhere.
528+
/// Just an optimization to save space and not store hundreds of
529+
/// `TyCtxtFeed` in the resolver.
530+
#[derive(Copy, Clone)]
531+
pub struct Feed<'tcx, KEY: Copy> {
532+
_tcx: PhantomData<TyCtxt<'tcx>>,
533+
// Do not allow direct access, as downstream code must not mutate this field.
534+
key: KEY,
535+
}
536+
537+
impl<T: fmt::Debug + Copy> fmt::Debug for Feed<'_, T> {
538+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
539+
self.key.fmt(f)
540+
}
541+
}
542+
525543
impl<'tcx> TyCtxt<'tcx> {
526544
pub fn feed_unit_query(self) -> TyCtxtFeed<'tcx, ()> {
527545
TyCtxtFeed { tcx: self, key: () }
@@ -547,6 +565,23 @@ impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> {
547565
pub fn key(&self) -> KEY {
548566
self.key
549567
}
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+
}
550585
}
551586

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

compiler/rustc_middle/src/ty/mod.rs

+2-1
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

+14-3
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;
@@ -26,13 +27,13 @@ struct DefCollector<'a, 'b, 'tcx> {
2627
}
2728

2829
impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
29-
fn create_def(
30+
fn create_def_with_feed(
3031
&mut self,
3132
node_id: NodeId,
3233
name: Symbol,
3334
def_kind: DefKind,
3435
span: Span,
35-
) -> LocalDefId {
36+
) -> Feed<'tcx, LocalDefId> {
3637
let parent_def = self.parent_def;
3738
debug!(
3839
"create_def(node_id={:?}, def_kind={:?}, parent_def={:?})",
@@ -47,7 +48,17 @@ 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()
52+
}
53+
54+
fn create_def(
55+
&mut self,
56+
node_id: NodeId,
57+
name: Symbol,
58+
def_kind: DefKind,
59+
span: Span,
60+
) -> LocalDefId {
61+
self.create_def_with_feed(node_id, name, def_kind, span).key()
5162
}
5263

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

compiler/rustc_resolve/src/lib.rs

+1-1
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;

0 commit comments

Comments
 (0)