Skip to content

Commit c1ea62a

Browse files
committed
fix for QueryKey
1 parent e5bd0b9 commit c1ea62a

3 files changed

Lines changed: 83 additions & 21 deletions

File tree

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -722,13 +722,10 @@ impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx> {
722722
}
723723
}
724724

725-
impl<'tcx, C> TyAbiInterface<'tcx, C> for Ty<'tcx>
726-
where
727-
C: HasTyCtxt<'tcx> + HasTypingEnv<'tcx>,
728-
{
725+
impl<'tcx> TyAbiInterface<'tcx, LayoutCx<'tcx>> for Ty<'tcx> {
729726
fn ty_and_layout_for_variant(
730727
this: TyAndLayout<'tcx>,
731-
cx: &C,
728+
cx: &LayoutCx<'tcx>,
732729
variant_index: VariantIdx,
733730
) -> TyAndLayout<'tcx> {
734731
let layout = match this.variants {
@@ -770,7 +767,11 @@ where
770767
TyAndLayout { ty: this.ty, layout }
771768
}
772769

773-
fn ty_and_layout_field(this: TyAndLayout<'tcx>, cx: &C, i: usize) -> TyAndLayout<'tcx> {
770+
fn ty_and_layout_field(
771+
this: TyAndLayout<'tcx>,
772+
cx: &LayoutCx<'tcx>,
773+
i: usize,
774+
) -> TyAndLayout<'tcx> {
774775
enum TyMaybeWithLayout<'tcx> {
775776
Ty(Ty<'tcx>),
776777
TyAndLayout(TyAndLayout<'tcx>),
@@ -968,7 +969,7 @@ where
968969
/// This will recurse into fields of ADTs to find the inner pointer.
969970
fn ty_and_layout_pointee_info_at(
970971
this: TyAndLayout<'tcx>,
971-
cx: &C,
972+
cx: &LayoutCx<'tcx>,
972973
offset: Size,
973974
) -> Option<PointeeInfo> {
974975
let tcx = cx.tcx();

compiler/rustc_type_ir/src/binder.rs

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::ops::{ControlFlow, Deref};
55

66
use derive_where::derive_where;
77
#[cfg(feature = "nightly")]
8+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
9+
#[cfg(feature = "nightly")]
810
use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
911
use rustc_type_ir_macros::{
1012
GenericTypeVisitable, Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic,
@@ -1010,10 +1012,7 @@ where
10101012

10111013
#[derive_where(Clone, Copy, PartialEq, Eq, Hash; I: Interner)]
10121014
#[derive(Lift_Generic)]
1013-
#[cfg_attr(
1014-
feature = "nightly",
1015-
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
1016-
)]
1015+
#[cfg_attr(feature = "nightly", derive(Encodable_NoContext, Decodable_NoContext))]
10171016
pub enum BoundRegionKind<I: Interner> {
10181017
/// An anonymous region parameter for a given fn (&T)
10191018
Anon,
@@ -1072,21 +1071,15 @@ impl<I: Interner> BoundRegionKind<I> {
10721071

10731072
#[derive_where(Clone, Copy, PartialEq, Eq, Debug, Hash; I: Interner)]
10741073
#[derive(Lift_Generic)]
1075-
#[cfg_attr(
1076-
feature = "nightly",
1077-
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
1078-
)]
1074+
#[cfg_attr(feature = "nightly", derive(Encodable_NoContext, Decodable_NoContext))]
10791075
pub enum BoundTyKind<I: Interner> {
10801076
Anon,
10811077
Param(I::DefId),
10821078
}
10831079

10841080
#[derive_where(Clone, Copy, PartialEq, Eq, Debug, Hash; I: Interner)]
10851081
#[derive(Lift_Generic)]
1086-
#[cfg_attr(
1087-
feature = "nightly",
1088-
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
1089-
)]
1082+
#[cfg_attr(feature = "nightly", derive(Encodable_NoContext, Decodable_NoContext))]
10901083
pub enum BoundVariableKind<I: Interner> {
10911084
Ty(BoundTyKind<I>),
10921085
Region(BoundRegionKind<I>),
@@ -1116,6 +1109,58 @@ impl<I: Interner> BoundVariableKind<I> {
11161109
}
11171110
}
11181111

1112+
#[cfg(feature = "nightly")]
1113+
// This is not a derived impl because a derive would require `I: HashStable`.
1114+
impl<CTX, I: Interner> HashStable<CTX> for BoundRegionKind<I>
1115+
where
1116+
I::DefId: HashStable<CTX>,
1117+
I::Symbol: HashStable<CTX>,
1118+
{
1119+
#[inline]
1120+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
1121+
std::mem::discriminant(self).hash_stable(hcx, hasher);
1122+
match self {
1123+
BoundRegionKind::Anon | BoundRegionKind::ClosureEnv => {}
1124+
BoundRegionKind::NamedForPrinting(sym) => sym.hash_stable(hcx, hasher),
1125+
BoundRegionKind::Named(def_id) => def_id.hash_stable(hcx, hasher),
1126+
}
1127+
}
1128+
}
1129+
1130+
#[cfg(feature = "nightly")]
1131+
// This is not a derived impl because a derive would require `I: HashStable`.
1132+
impl<CTX, I: Interner> HashStable<CTX> for BoundTyKind<I>
1133+
where
1134+
I::DefId: HashStable<CTX>,
1135+
{
1136+
#[inline]
1137+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
1138+
std::mem::discriminant(self).hash_stable(hcx, hasher);
1139+
match self {
1140+
BoundTyKind::Anon => {}
1141+
BoundTyKind::Param(def_id) => def_id.hash_stable(hcx, hasher),
1142+
}
1143+
}
1144+
}
1145+
1146+
#[cfg(feature = "nightly")]
1147+
// This is not a derived impl because a derive would require `I: HashStable`.
1148+
impl<CTX, I: Interner> HashStable<CTX> for BoundVariableKind<I>
1149+
where
1150+
BoundRegionKind<I>: HashStable<CTX>,
1151+
BoundTyKind<I>: HashStable<CTX>,
1152+
{
1153+
#[inline]
1154+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
1155+
std::mem::discriminant(self).hash_stable(hcx, hasher);
1156+
match self {
1157+
BoundVariableKind::Ty(ty) => ty.hash_stable(hcx, hasher),
1158+
BoundVariableKind::Region(region) => region.hash_stable(hcx, hasher),
1159+
BoundVariableKind::Const => {}
1160+
}
1161+
}
1162+
}
1163+
11191164
#[derive_where(Clone, Copy, PartialEq, Eq, Hash; I: Interner)]
11201165
#[cfg_attr(
11211166
feature = "nightly",

compiler/rustc_type_ir/src/sty/mod.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use std::ops::{ControlFlow, Deref, Range};
55
use derive_where::derive_where;
66
use rustc_abi::{FieldIdx, VariantIdx};
77
#[cfg(feature = "nightly")]
8-
use rustc_macros::HashStable_NoContext;
8+
use rustc_data_structures::fingerprint::Fingerprint;
9+
#[cfg(feature = "nightly")]
10+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
911
use rustc_span::sym;
1012
use tracing::instrument;
1113

@@ -25,7 +27,6 @@ use crate::{
2527
/// Use this rather than `TyKind`, whenever possible.
2628
#[derive_where(Copy; I: Interner, I::Interned<WithCachedTypeInfo<TyKind<I>>>: Copy)]
2729
#[derive_where(Clone, PartialEq, Eq, Hash; I: Interner)]
28-
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
2930
#[rustc_diagnostic_item = "Ty"]
3031
#[rustc_pass_by_value]
3132
pub struct Ty<I: Interner>(pub I::Interned<WithCachedTypeInfo<TyKind<I>>>);
@@ -42,6 +43,21 @@ impl<I: Interner> Ty<I> {
4243
}
4344
}
4445

46+
#[cfg(feature = "nightly")]
47+
impl<CTX, I: Interner> HashStable<CTX> for Ty<I> {
48+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
49+
// Hashing the cached fingerprint avoids recursive trait obligations like
50+
// `Ty: HashStable -> TyKind: HashStable -> Ty: HashStable`.
51+
let stable_hash = self.0.stable_hash;
52+
debug_assert_ne!(
53+
stable_hash,
54+
Fingerprint::ZERO,
55+
"Ty should only be stably hashed once it has a cached stable hash",
56+
);
57+
stable_hash.hash_stable(hcx, hasher);
58+
}
59+
}
60+
4561
impl<I: Interner> fmt::Debug for Ty<I>
4662
where
4763
I::Interned<WithCachedTypeInfo<TyKind<I>>>: Deref<Target = WithCachedTypeInfo<TyKind<I>>>,

0 commit comments

Comments
 (0)