Skip to content

Commit 66a376e

Browse files
committed
Store CtxtInterners for local values in AllArenas
1 parent 3ade426 commit 66a376e

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

src/librustc/infer/mod.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ use crate::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
2121
use crate::ty::fold::{TypeFolder, TypeFoldable};
2222
use crate::ty::relate::RelateResult;
2323
use crate::ty::subst::{Kind, InternalSubsts, SubstsRef};
24-
use crate::ty::{self, GenericParamDefKind, Ty, TyCtxt, CtxtInterners, InferConst};
24+
use crate::ty::{self, GenericParamDefKind, Ty, TyCtxt, InferConst};
2525
use crate::ty::{FloatVid, IntVid, TyVid, ConstVid};
2626
use crate::util::nodemap::FxHashMap;
2727

28-
use arena::SyncDroplessArena;
2928
use errors::DiagnosticBuilder;
3029
use rustc_data_structures::unify as ut;
3130
use std::cell::{Cell, Ref, RefCell, RefMut};
@@ -468,17 +467,13 @@ impl<'tcx> fmt::Display for FixupError<'tcx> {
468467
/// `F: for<'b, 'tcx> where 'gcx: 'tcx FnOnce(InferCtxt<'b, 'gcx, 'tcx>)`.
469468
pub struct InferCtxtBuilder<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
470469
global_tcx: TyCtxt<'a, 'gcx, 'gcx>,
471-
arena: SyncDroplessArena,
472-
interners: Option<CtxtInterners<'tcx>>,
473470
fresh_tables: Option<RefCell<ty::TypeckTables<'tcx>>>,
474471
}
475472

476473
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'gcx> {
477474
pub fn infer_ctxt(self) -> InferCtxtBuilder<'a, 'gcx, 'tcx> {
478475
InferCtxtBuilder {
479476
global_tcx: self,
480-
arena: SyncDroplessArena::default(),
481-
interners: None,
482477
fresh_tables: None,
483478
}
484479
}
@@ -518,14 +513,10 @@ impl<'a, 'gcx, 'tcx> InferCtxtBuilder<'a, 'gcx, 'tcx> {
518513
pub fn enter<R>(&'tcx mut self, f: impl for<'b> FnOnce(InferCtxt<'b, 'gcx, 'tcx>) -> R) -> R {
519514
let InferCtxtBuilder {
520515
global_tcx,
521-
ref arena,
522-
ref mut interners,
523516
ref fresh_tables,
524517
} = *self;
525518
let in_progress_tables = fresh_tables.as_ref();
526-
// Check that we haven't entered before
527-
assert!(interners.is_none());
528-
global_tcx.enter_local(arena, interners, |tcx| {
519+
global_tcx.enter_local(|tcx| {
529520
f(InferCtxt {
530521
tcx,
531522
in_progress_tables,

src/librustc/ty/context.rs

+28-16
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ use crate::hir;
8282
pub struct AllArenas<'tcx> {
8383
pub global: WorkerLocal<GlobalArenas<'tcx>>,
8484
pub interner: SyncDroplessArena,
85+
pub local_interner: SyncDroplessArena,
8586
}
8687

8788
impl<'tcx> AllArenas<'tcx> {
8889
pub fn new() -> Self {
8990
AllArenas {
9091
global: WorkerLocal::new(|_| GlobalArenas::default()),
9192
interner: SyncDroplessArena::default(),
93+
local_interner: SyncDroplessArena::default(),
9294
}
9395
}
9496
}
@@ -154,7 +156,7 @@ impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
154156
/// Intern a type
155157
#[inline(never)]
156158
fn intern_ty(
157-
local: &CtxtInterners<'tcx>,
159+
local: &CtxtInterners<'gcx>,
158160
global: &CtxtInterners<'gcx>,
159161
st: TyKind<'tcx>
160162
) -> Ty<'tcx> {
@@ -179,6 +181,12 @@ impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
179181
&ty_struct);
180182
}
181183

184+
// This is safe because all the types the ty_struct can point to
185+
// already is in the local arena or the global arena
186+
let ty_struct: TyS<'gcx> = unsafe {
187+
mem::transmute(ty_struct)
188+
};
189+
182190
Interned(local.arena.alloc(ty_struct))
183191
}).0
184192
} else {
@@ -1029,8 +1037,8 @@ pub struct FreeRegionInfo {
10291037
#[derive(Copy, Clone)]
10301038
pub struct TyCtxt<'a, 'gcx: 'tcx, 'tcx: 'a> {
10311039
gcx: &'gcx GlobalCtxt<'gcx>,
1032-
interners: &'tcx CtxtInterners<'tcx>,
1033-
dummy: PhantomData<&'a ()>,
1040+
interners: &'gcx CtxtInterners<'gcx>,
1041+
dummy: PhantomData<(&'a (), &'tcx ())>,
10341042
}
10351043

10361044
impl<'gcx> Deref for TyCtxt<'_, 'gcx, '_> {
@@ -1045,6 +1053,7 @@ pub struct GlobalCtxt<'tcx> {
10451053
pub arena: WorkerLocal<Arena<'tcx>>,
10461054
global_arenas: &'tcx WorkerLocal<GlobalArenas<'tcx>>,
10471055
global_interners: CtxtInterners<'tcx>,
1056+
local_interners: CtxtInterners<'tcx>,
10481057

10491058
cstore: &'tcx CrateStoreDyn,
10501059

@@ -1262,6 +1271,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12621271
s.fatal(&err);
12631272
});
12641273
let interners = CtxtInterners::new(&arenas.interner);
1274+
let local_interners = CtxtInterners::new(&arenas.local_interner);
12651275
let common = Common {
12661276
empty_predicates: ty::GenericPredicates {
12671277
parent: None,
@@ -1321,6 +1331,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13211331
arena: WorkerLocal::new(|_| Arena::default()),
13221332
global_arenas: &arenas.global,
13231333
global_interners: interners,
1334+
local_interners: local_interners,
13241335
dep_graph,
13251336
common,
13261337
types: common_types,
@@ -1716,18 +1727,15 @@ impl<'gcx> GlobalCtxt<'gcx> {
17161727
/// with the same lifetime as `arena`.
17171728
pub fn enter_local<'tcx, F, R>(
17181729
&'gcx self,
1719-
arena: &'tcx SyncDroplessArena,
1720-
interners: &'tcx mut Option<CtxtInterners<'tcx>>,
17211730
f: F
17221731
) -> R
17231732
where
17241733
F: FnOnce(TyCtxt<'tcx, 'gcx, 'tcx>) -> R,
17251734
'gcx: 'tcx,
17261735
{
1727-
*interners = Some(CtxtInterners::new(&arena));
17281736
let tcx = TyCtxt {
17291737
gcx: self,
1730-
interners: interners.as_ref().unwrap(),
1738+
interners: &self.local_interners,
17311739
dummy: PhantomData,
17321740
};
17331741
ty::tls::with_related_context(tcx.global_tcx(), |icx| {
@@ -2333,6 +2341,17 @@ macro_rules! intern_method {
23332341
pub fn $method(self, v: $alloc) -> &$lt_tcx $ty {
23342342
let key = ($alloc_to_key)(&v);
23352343

2344+
let alloc = |v, interners: &'gcx CtxtInterners<'gcx>| {
2345+
// This transmutes $alloc<'tcx> to $alloc<'gcx>
2346+
let v = unsafe {
2347+
mem::transmute(v)
2348+
};
2349+
let i: &$lt_tcx $ty = $alloc_method(&interners.arena, v);
2350+
// Cast to 'gcx
2351+
let i = unsafe { mem::transmute(i) };
2352+
Interned(i)
2353+
};
2354+
23362355
// HACK(eddyb) Depend on flags being accurate to
23372356
// determine that all contents are in the global tcx.
23382357
// See comments on Lift for why we can't use that.
@@ -2346,18 +2365,11 @@ macro_rules! intern_method {
23462365
v);
23472366
}
23482367

2349-
Interned($alloc_method(&self.interners.arena, v))
2368+
alloc(v, &self.interners)
23502369
}).0
23512370
} else {
23522371
self.global_interners.$name.borrow_mut().intern_ref(key, || {
2353-
// This transmutes $alloc<'tcx> to $alloc<'gcx>
2354-
let v = unsafe {
2355-
mem::transmute(v)
2356-
};
2357-
let i: &$lt_tcx $ty = $alloc_method(&self.global_interners.arena, v);
2358-
// Cast to 'gcx
2359-
let i = unsafe { mem::transmute(i) };
2360-
Interned(i)
2372+
alloc(v, &self.global_interners)
23612373
}).0
23622374
}
23632375
}

0 commit comments

Comments
 (0)