Skip to content

Commit 8f52664

Browse files
committed
Lift using interners instead of in_arena
1 parent 490d5ac commit 8f52664

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

src/librustc/ty/context.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use crate::util::nodemap::{FxHashMap, FxHashSet};
5252
use arena::SyncDroplessArena;
5353
use errors::DiagnosticBuilder;
5454
use rustc_data_structures::profiling::SelfProfilerRef;
55-
use rustc_data_structures::sharded::ShardedHashMap;
55+
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
5656
use rustc_data_structures::stable_hasher::{
5757
hash_stable_hashmap, HashStable, StableHasher, StableVec,
5858
};
@@ -1557,11 +1557,11 @@ pub trait Lift<'tcx>: fmt::Debug {
15571557
}
15581558

15591559
macro_rules! nop_lift {
1560-
($ty:ty => $lifted:ty) => {
1560+
($set:ident; $ty:ty => $lifted:ty) => {
15611561
impl<'a, 'tcx> Lift<'tcx> for $ty {
15621562
type Lifted = $lifted;
15631563
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
1564-
if tcx.interners.arena.in_arena(*self as *const _) {
1564+
if tcx.interners.$set.contains_pointer_to(&Interned(*self)) {
15651565
Some(unsafe { mem::transmute(*self) })
15661566
} else {
15671567
None
@@ -1572,14 +1572,14 @@ macro_rules! nop_lift {
15721572
}
15731573

15741574
macro_rules! nop_list_lift {
1575-
($ty:ty => $lifted:ty) => {
1575+
($set:ident; $ty:ty => $lifted:ty) => {
15761576
impl<'a, 'tcx> Lift<'tcx> for &'a List<$ty> {
15771577
type Lifted = &'tcx List<$lifted>;
15781578
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
15791579
if self.is_empty() {
15801580
return Some(List::empty());
15811581
}
1582-
if tcx.interners.arena.in_arena(*self as *const _) {
1582+
if tcx.interners.$set.contains_pointer_to(&Interned(*self)) {
15831583
Some(unsafe { mem::transmute(*self) })
15841584
} else {
15851585
None
@@ -1589,21 +1589,21 @@ macro_rules! nop_list_lift {
15891589
};
15901590
}
15911591

1592-
nop_lift! {Ty<'a> => Ty<'tcx>}
1593-
nop_lift! {Region<'a> => Region<'tcx>}
1594-
nop_lift! {Goal<'a> => Goal<'tcx>}
1595-
nop_lift! {&'a Const<'a> => &'tcx Const<'tcx>}
1592+
nop_lift! {type_; Ty<'a> => Ty<'tcx>}
1593+
nop_lift! {region; Region<'a> => Region<'tcx>}
1594+
nop_lift! {goal; Goal<'a> => Goal<'tcx>}
1595+
nop_lift! {const_; &'a Const<'a> => &'tcx Const<'tcx>}
15961596

1597-
nop_list_lift! {Goal<'a> => Goal<'tcx>}
1598-
nop_list_lift! {Clause<'a> => Clause<'tcx>}
1599-
nop_list_lift! {Ty<'a> => Ty<'tcx>}
1600-
nop_list_lift! {ExistentialPredicate<'a> => ExistentialPredicate<'tcx>}
1601-
nop_list_lift! {Predicate<'a> => Predicate<'tcx>}
1602-
nop_list_lift! {CanonicalVarInfo => CanonicalVarInfo}
1603-
nop_list_lift! {ProjectionKind => ProjectionKind}
1597+
nop_list_lift! {goal_list; Goal<'a> => Goal<'tcx>}
1598+
nop_list_lift! {clauses; Clause<'a> => Clause<'tcx>}
1599+
nop_list_lift! {type_list; Ty<'a> => Ty<'tcx>}
1600+
nop_list_lift! {existential_predicates; ExistentialPredicate<'a> => ExistentialPredicate<'tcx>}
1601+
nop_list_lift! {predicates; Predicate<'a> => Predicate<'tcx>}
1602+
nop_list_lift! {canonical_var_infos; CanonicalVarInfo => CanonicalVarInfo}
1603+
nop_list_lift! {projs; ProjectionKind => ProjectionKind}
16041604

16051605
// This is the impl for `&'a InternalSubsts<'a>`.
1606-
nop_list_lift! {GenericArg<'a> => GenericArg<'tcx>}
1606+
nop_list_lift! {substs; GenericArg<'a> => GenericArg<'tcx>}
16071607

16081608
pub mod tls {
16091609
use super::{ptr_eq, GlobalCtxt, TyCtxt};
@@ -1927,6 +1927,11 @@ impl<'tcx, T: 'tcx + ?Sized> Clone for Interned<'tcx, T> {
19271927
}
19281928
impl<'tcx, T: 'tcx + ?Sized> Copy for Interned<'tcx, T> {}
19291929

1930+
unsafe impl<'tcx, T: 'tcx + ?Sized> IntoPointer for Interned<'tcx, T> {
1931+
fn into_pointer(&self) -> *const () {
1932+
self.0 as *const _ as *const ()
1933+
}
1934+
}
19301935
// N.B., an `Interned<Ty>` compares and hashes as a `TyKind`.
19311936
impl<'tcx> PartialEq for Interned<'tcx, TyS<'tcx>> {
19321937
fn eq(&self, other: &Interned<'tcx, TyS<'tcx>>) -> bool {

src/librustc_data_structures/sharded.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ impl<K: Eq + Hash + Copy> ShardedHashMap<K, ()> {
137137
}
138138
}
139139

140+
pub unsafe trait IntoPointer {
141+
/// Returns a pointer which outlives `self`.
142+
fn into_pointer(&self) -> *const ();
143+
}
144+
145+
impl<K: Eq + Hash + Copy + IntoPointer> ShardedHashMap<K, ()> {
146+
pub fn contains_pointer_to<T: Hash + IntoPointer>(&self, value: &T) -> bool {
147+
let hash = make_hash(&value);
148+
let shard = self.get_shard_by_hash(hash).lock();
149+
let value = value.into_pointer();
150+
shard.raw_entry().from_hash(hash, |entry| entry.into_pointer() == value).is_some()
151+
}
152+
}
153+
140154
#[inline]
141155
fn make_hash<K: Hash + ?Sized>(val: &K) -> u64 {
142156
let mut state = FxHasher::default();

0 commit comments

Comments
 (0)