Skip to content

Commit ec8c2e1

Browse files
committed
Use a single CtxtInterners
1 parent ef064d2 commit ec8c2e1

File tree

1 file changed

+28
-136
lines changed

1 file changed

+28
-136
lines changed

src/librustc/ty/context.rs

+28-136
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// ignore-tidy-filelength
2-
31
//! Type context book-keeping.
42
53
use crate::arena::Arena;
@@ -67,7 +65,6 @@ use std::ops::{Deref, Bound};
6765
use std::iter;
6866
use std::sync::mpsc;
6967
use std::sync::Arc;
70-
use std::marker::PhantomData;
7168
use rustc_target::spec::abi;
7269
use rustc_macros::HashStable;
7370
use syntax::ast;
@@ -81,14 +78,12 @@ use crate::hir;
8178

8279
pub struct AllArenas {
8380
pub interner: SyncDroplessArena,
84-
pub local_interner: SyncDroplessArena,
8581
}
8682

8783
impl AllArenas {
8884
pub fn new() -> Self {
8985
AllArenas {
9086
interner: SyncDroplessArena::default(),
91-
local_interner: SyncDroplessArena::default(),
9287
}
9388
}
9489
}
@@ -136,57 +131,21 @@ impl<'tcx> CtxtInterners<'tcx> {
136131

137132
/// Intern a type
138133
#[inline(never)]
139-
fn intern_ty(
140-
local: &CtxtInterners<'tcx>,
141-
global: &CtxtInterners<'tcx>,
142-
st: TyKind<'tcx>,
134+
fn intern_ty(&self,
135+
st: TyKind<'tcx>
143136
) -> Ty<'tcx> {
144-
let flags = super::flags::FlagComputation::for_sty(&st);
145-
146-
// HACK(eddyb) Depend on flags being accurate to
147-
// determine that all contents are in the global tcx.
148-
// See comments on Lift for why we can't use that.
149-
if flags.flags.intersects(ty::TypeFlags::KEEP_IN_LOCAL_TCX) {
150-
local.type_.borrow_mut().intern(st, |st| {
151-
let ty_struct = TyS {
152-
sty: st,
153-
flags: flags.flags,
154-
outer_exclusive_binder: flags.outer_exclusive_binder,
155-
};
137+
self.type_.borrow_mut().intern(st, |st| {
138+
let flags = super::flags::FlagComputation::for_sty(&st);
156139

157-
// Make sure we don't end up with inference
158-
// types/regions in the global interner
159-
if ptr_eq(local, global) {
160-
bug!("Attempted to intern `{:?}` which contains \
161-
inference types/regions in the global type context",
162-
&ty_struct);
163-
}
164-
165-
// This is safe because all the types the ty_struct can point to
166-
// already is in the local arena or the global arena
167-
let ty_struct: TyS<'tcx> = unsafe {
168-
mem::transmute(ty_struct)
169-
};
170-
171-
Interned(local.arena.alloc(ty_struct))
172-
}).0
173-
} else {
174-
global.type_.borrow_mut().intern(st, |st| {
175-
let ty_struct = TyS {
176-
sty: st,
177-
flags: flags.flags,
178-
outer_exclusive_binder: flags.outer_exclusive_binder,
179-
};
140+
let ty_struct = TyS {
141+
sty: st,
142+
flags: flags.flags,
143+
outer_exclusive_binder: flags.outer_exclusive_binder,
144+
};
180145

181-
// This is safe because all the types the ty_struct can point to
182-
// already is in the global arena
183-
let ty_struct: TyS<'tcx> = unsafe {
184-
mem::transmute(ty_struct)
185-
};
186146

187-
Interned(global.arena.alloc(ty_struct))
188-
}).0
189-
}
147+
Interned(self.arena.alloc(ty_struct))
148+
}).0
190149
}
191150
}
192151

@@ -933,7 +892,7 @@ EnumLiftImpl! {
933892

934893
impl<'tcx> CommonTypes<'tcx> {
935894
fn new(interners: &CtxtInterners<'tcx>) -> CommonTypes<'tcx> {
936-
let mk = |sty| CtxtInterners::intern_ty(interners, interners, sty);
895+
let mk = |sty| interners.intern_ty(sty);
937896

938897
CommonTypes {
939898
unit: mk(Tuple(List::empty())),
@@ -1015,8 +974,6 @@ pub struct FreeRegionInfo {
1015974
#[derive(Copy, Clone)]
1016975
pub struct TyCtxt<'tcx> {
1017976
gcx: &'tcx GlobalCtxt<'tcx>,
1018-
interners: &'tcx CtxtInterners<'tcx>,
1019-
dummy: PhantomData<&'tcx ()>,
1020977
}
1021978

1022979
impl<'tcx> Deref for TyCtxt<'tcx> {
@@ -1030,8 +987,7 @@ impl<'tcx> Deref for TyCtxt<'tcx> {
1030987
pub struct GlobalCtxt<'tcx> {
1031988
pub arena: WorkerLocal<Arena<'tcx>>,
1032989

1033-
global_interners: CtxtInterners<'tcx>,
1034-
local_interners: CtxtInterners<'tcx>,
990+
interners: CtxtInterners<'tcx>,
1035991

1036992
cstore: &'tcx CrateStoreDyn,
1037993

@@ -1122,8 +1078,6 @@ impl<'tcx> TyCtxt<'tcx> {
11221078
pub fn global_tcx(self) -> TyCtxt<'tcx> {
11231079
TyCtxt {
11241080
gcx: self.gcx,
1125-
interners: &self.gcx.global_interners,
1126-
dummy: PhantomData,
11271081
}
11281082
}
11291083

@@ -1203,11 +1157,6 @@ impl<'tcx> TyCtxt<'tcx> {
12031157
value.lift_to_tcx(self.global_tcx())
12041158
}
12051159

1206-
/// Returns `true` if self is the same as self.global_tcx().
1207-
fn is_global(self) -> bool {
1208-
ptr_eq(self.interners, &self.global_interners)
1209-
}
1210-
12111160
/// Creates a type context and call the closure with a `TyCtxt` reference
12121161
/// to the context. The closure enforces that the type context and any interned
12131162
/// value (types, substs, etc.) can only be used while `ty::tls` has a valid
@@ -1229,7 +1178,6 @@ impl<'tcx> TyCtxt<'tcx> {
12291178
s.fatal(&err);
12301179
});
12311180
let interners = CtxtInterners::new(&arenas.interner);
1232-
let local_interners = CtxtInterners::new(&arenas.local_interner);
12331181
let common = Common {
12341182
empty_predicates: ty::GenericPredicates {
12351183
parent: None,
@@ -1287,8 +1235,7 @@ impl<'tcx> TyCtxt<'tcx> {
12871235
sess: s,
12881236
cstore,
12891237
arena: WorkerLocal::new(|_| Arena::default()),
1290-
global_interners: interners,
1291-
local_interners: local_interners,
1238+
interners,
12921239
dep_graph,
12931240
common,
12941241
types: common_types,
@@ -1682,8 +1629,6 @@ impl<'tcx> GlobalCtxt<'tcx> {
16821629
{
16831630
let tcx = TyCtxt {
16841631
gcx: self,
1685-
interners: &self.local_interners,
1686-
dummy: PhantomData,
16871632
};
16881633
ty::tls::with_related_context(tcx.global_tcx(), |icx| {
16891634
let new_icx = ty::tls::ImplicitCtxt {
@@ -1729,11 +1674,7 @@ macro_rules! nop_lift {
17291674
type Lifted = $lifted;
17301675
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
17311676
if tcx.interners.arena.in_arena(*self as *const _) {
1732-
return Some(unsafe { mem::transmute(*self) });
1733-
}
1734-
// Also try in the global tcx if we're not that.
1735-
if !tcx.is_global() {
1736-
self.lift_to_tcx(tcx.global_tcx())
1677+
Some(unsafe { mem::transmute(*self) })
17371678
} else {
17381679
None
17391680
}
@@ -1751,11 +1692,7 @@ macro_rules! nop_list_lift {
17511692
return Some(List::empty());
17521693
}
17531694
if tcx.interners.arena.in_arena(*self as *const _) {
1754-
return Some(unsafe { mem::transmute(*self) });
1755-
}
1756-
// Also try in the global tcx if we're not that.
1757-
if !tcx.is_global() {
1758-
self.lift_to_tcx(tcx.global_tcx())
1695+
Some(unsafe { mem::transmute(*self) })
17591696
} else {
17601697
None
17611698
}
@@ -1785,7 +1722,6 @@ pub mod tls {
17851722

17861723
use std::fmt;
17871724
use std::mem;
1788-
use std::marker::PhantomData;
17891725
use syntax_pos;
17901726
use crate::ty::query;
17911727
use errors::{Diagnostic, TRACK_DIAGNOSTICS};
@@ -1949,8 +1885,6 @@ pub mod tls {
19491885

19501886
let tcx = TyCtxt {
19511887
gcx,
1952-
interners: &gcx.global_interners,
1953-
dummy: PhantomData,
19541888
};
19551889
let icx = ImplicitCtxt {
19561890
tcx,
@@ -1981,8 +1915,6 @@ pub mod tls {
19811915
let gcx = &*(gcx as *const GlobalCtxt<'_>);
19821916
let tcx = TyCtxt {
19831917
gcx,
1984-
interners: &gcx.global_interners,
1985-
dummy: PhantomData,
19861918
};
19871919
let icx = ImplicitCtxt {
19881920
query: None,
@@ -2041,26 +1973,6 @@ pub mod tls {
20411973
})
20421974
}
20431975

2044-
/// Allows access to the current ImplicitCtxt whose tcx field has the same global
2045-
/// interner and local interner as the tcx argument passed in. This means the closure
2046-
/// is given an ImplicitCtxt with the same 'tcx and 'tcx lifetimes as the TyCtxt passed in.
2047-
/// This will panic if you pass it a TyCtxt which has a different global interner or
2048-
/// a different local interner from the current ImplicitCtxt's tcx field.
2049-
#[inline]
2050-
pub fn with_fully_related_context<'tcx, F, R>(tcx: TyCtxt<'tcx>, f: F) -> R
2051-
where
2052-
F: for<'b> FnOnce(&ImplicitCtxt<'b, 'tcx>) -> R,
2053-
{
2054-
with_context(|context| {
2055-
unsafe {
2056-
assert!(ptr_eq(context.tcx.gcx, tcx.gcx));
2057-
assert!(ptr_eq(context.tcx.interners, tcx.interners));
2058-
let context: &ImplicitCtxt<'_, '_> = mem::transmute(context);
2059-
f(context)
2060-
}
2061-
})
2062-
}
2063-
20641976
/// Allows access to the TyCtxt in the current ImplicitCtxt.
20651977
/// Panics if there is no ImplicitCtxt available
20661978
#[inline]
@@ -2288,39 +2200,22 @@ impl<'tcx> Borrow<[Goal<'tcx>]> for Interned<'tcx, List<Goal<'tcx>>> {
22882200
macro_rules! intern_method {
22892201
($lt_tcx:tt, $name:ident: $method:ident($alloc:ty,
22902202
$alloc_method:expr,
2291-
$alloc_to_key:expr,
2292-
$keep_in_local_tcx:expr) -> $ty:ty) => {
2203+
$alloc_to_key:expr) -> $ty:ty) => {
22932204
impl<$lt_tcx> TyCtxt<$lt_tcx> {
22942205
pub fn $method(self, v: $alloc) -> &$lt_tcx $ty {
22952206
let key = ($alloc_to_key)(&v);
22962207

2297-
// HACK(eddyb) Depend on flags being accurate to
2298-
// determine that all contents are in the global tcx.
2299-
// See comments on Lift for why we can't use that.
2300-
if ($keep_in_local_tcx)(&v) {
2301-
self.interners.$name.borrow_mut().intern_ref(key, || {
2302-
// Make sure we don't end up with inference
2303-
// types/regions in the global tcx.
2304-
if self.is_global() {
2305-
bug!("Attempted to intern `{:?}` which contains \
2306-
inference types/regions in the global type context",
2307-
v);
2308-
}
2208+
self.interners.$name.borrow_mut().intern_ref(key, || {
2209+
Interned($alloc_method(&self.interners.arena, v))
23092210

2310-
Interned($alloc_method(&self.interners.arena, v))
2311-
}).0
2312-
} else {
2313-
self.global_interners.$name.borrow_mut().intern_ref(key, || {
2314-
Interned($alloc_method(&self.global_interners.arena, v))
2315-
}).0
2316-
}
2211+
}).0
23172212
}
23182213
}
23192214
}
23202215
}
23212216

23222217
macro_rules! direct_interners {
2323-
($lt_tcx:tt, $($name:ident: $method:ident($keep_in_local_tcx:expr) -> $ty:ty),+) => {
2218+
($lt_tcx:tt, $($name:ident: $method:ident($ty:ty)),+) => {
23242219
$(impl<$lt_tcx> PartialEq for Interned<$lt_tcx, $ty> {
23252220
fn eq(&self, other: &Self) -> bool {
23262221
self.0 == other.0
@@ -2339,8 +2234,7 @@ macro_rules! direct_interners {
23392234
$lt_tcx,
23402235
$name: $method($ty,
23412236
|a: &$lt_tcx SyncDroplessArena, v| -> &$lt_tcx $ty { a.alloc(v) },
2342-
|x| x,
2343-
$keep_in_local_tcx) -> $ty);)+
2237+
|x| x) -> $ty);)+
23442238
}
23452239
}
23462240

@@ -2349,18 +2243,17 @@ pub fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {
23492243
}
23502244

23512245
direct_interners!('tcx,
2352-
region: mk_region(|r: &RegionKind| r.keep_in_local_tcx()) -> RegionKind,
2353-
goal: mk_goal(|c: &GoalKind<'_>| keep_local(c)) -> GoalKind<'tcx>,
2354-
const_: mk_const(|c: &Const<'_>| keep_local(&c)) -> Const<'tcx>
2246+
region: mk_region(RegionKind),
2247+
goal: mk_goal(GoalKind<'tcx>),
2248+
const_: mk_const(Const<'tcx>)
23552249
);
23562250

23572251
macro_rules! slice_interners {
23582252
($($field:ident: $method:ident($ty:ty)),+) => (
23592253
$(intern_method!( 'tcx, $field: $method(
23602254
&[$ty],
23612255
|a, v| List::from_arena(a, v),
2362-
Deref::deref,
2363-
|xs: &[$ty]| xs.iter().any(keep_local)) -> List<$ty>);)+
2256+
Deref::deref) -> List<$ty>);)+
23642257
);
23652258
}
23662259

@@ -2384,8 +2277,7 @@ intern_method! {
23842277
canonical_var_infos: _intern_canonical_var_infos(
23852278
&[CanonicalVarInfo],
23862279
|a, v| List::from_arena(a, v),
2387-
Deref::deref,
2388-
|_xs: &[CanonicalVarInfo]| -> bool { false }
2280+
Deref::deref
23892281
) -> List<CanonicalVarInfo>
23902282
}
23912283

@@ -2431,7 +2323,7 @@ impl<'tcx> TyCtxt<'tcx> {
24312323

24322324
#[inline]
24332325
pub fn mk_ty(&self, st: TyKind<'tcx>) -> Ty<'tcx> {
2434-
CtxtInterners::intern_ty(&self.interners, &self.global_interners, st)
2326+
self.interners.intern_ty(st)
24352327
}
24362328

24372329
pub fn mk_mach_int(self, tm: ast::IntTy) -> Ty<'tcx> {

0 commit comments

Comments
 (0)