Skip to content

Commit b99be88

Browse files
committed
Change how hashing works
1 parent 00f6c4d commit b99be88

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,10 @@ impl<'tcx> Const<'tcx> {
268268
}
269269
}
270270

271-
impl Hash for Const<'tcx> {
271+
impl Hash for &'tcx Const<'tcx> {
272272
fn hash<H: Hasher>(&self, state: &mut H) {
273-
(self as *const Const<'tcx>).hash(state)
273+
let c: &'tcx Const<'tcx> = *self;
274+
(c as *const Const<'tcx>).hash(state)
274275
}
275276
}
276277

compiler/rustc_middle/src/ty/context.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<'tcx> CtxtInterners<'tcx> {
177177

178178
#[inline(never)]
179179
fn intern_const(&self, ty: Ty<'tcx>, val: ty::ConstKind<'tcx>) -> &'tcx Const<'tcx> {
180-
self.const_.intern(Const { ty, val }, |c| Interned(self.arena.alloc(c))).0
180+
self.const_.intern(CstHash(Const { ty, val }), |c| Interned(self.arena.alloc(c.0))).0
181181
}
182182
}
183183

@@ -934,13 +934,10 @@ impl<'tcx> CommonLifetimes<'tcx> {
934934

935935
impl<'tcx> CommonConsts<'tcx> {
936936
fn new(interners: &CtxtInterners<'tcx>, types: &CommonTypes<'tcx>) -> CommonConsts<'tcx> {
937-
let mk_const = |c| interners.const_.intern(c, |c| Interned(interners.arena.alloc(c))).0;
937+
let mk_const = |ty, val| interners.intern_const(ty, val);
938938

939939
CommonConsts {
940-
unit: mk_const(ty::Const {
941-
val: ty::ConstKind::Value(ConstValue::Scalar(Scalar::ZST)),
942-
ty: types.unit,
943-
}),
940+
unit: mk_const(types.unit, ty::ConstKind::Value(ConstValue::Scalar(Scalar::ZST))),
944941
}
945942
}
946943
}
@@ -2060,22 +2057,45 @@ impl<'tcx, T> Borrow<[T]> for Interned<'tcx, List<T>> {
20602057

20612058
impl<'tcx> PartialEq for Interned<'tcx, Const<'tcx>> {
20622059
fn eq(&self, other: &Self) -> bool {
2063-
self.0 == other.0
2060+
self.0.ty == other.0.ty && self.0.val == other.0.val
20642061
}
20652062
}
20662063

20672064
impl<'tcx> Eq for Interned<'tcx, Const<'tcx>> {}
20682065

2066+
#[repr(transparent)]
2067+
#[derive(Clone, Copy)]
2068+
struct CstHash<'tcx>(Const<'tcx>);
2069+
2070+
impl<'tcx> Hash for CstHash<'tcx> {
2071+
fn hash<H: Hasher>(&self, state: &mut H) {
2072+
self.0.ty.hash(state);
2073+
self.0.val.hash(state);
2074+
}
2075+
}
2076+
2077+
impl<'tcx> PartialEq for CstHash<'tcx> {
2078+
fn eq(&self, other: &Self) -> bool {
2079+
self.0.ty == other.0.ty && self.0.val == other.0.val
2080+
}
2081+
}
2082+
2083+
impl Eq for CstHash<'tcx> {}
2084+
20692085
impl<'tcx> Hash for Interned<'tcx, Const<'tcx>> {
20702086
fn hash<H: Hasher>(&self, state: &mut H) {
20712087
self.0.ty.hash(state);
20722088
self.0.val.hash(state);
20732089
}
20742090
}
20752091

2076-
impl<'tcx> Borrow<Const<'tcx>> for Interned<'tcx, Const<'tcx>> {
2077-
fn borrow(&self) -> &Const<'tcx> {
2078-
&self.0
2092+
impl<'tcx> Borrow<CstHash<'tcx>> for Interned<'tcx, Const<'tcx>> {
2093+
fn borrow(&self) -> &CstHash<'tcx> {
2094+
let c: &Const<'tcx> = &self.0;
2095+
unsafe {
2096+
// SAFETY: safe because CstHash is repr(transparent) over Const
2097+
&*(c as *const Const<'tcx> as *const CstHash<'tcx>)
2098+
}
20792099
}
20802100
}
20812101

0 commit comments

Comments
 (0)