Skip to content

Commit 0e0251e

Browse files
implement operators respecting HashStable
1 parent aff004b commit 0e0251e

File tree

13 files changed

+414
-43
lines changed

13 files changed

+414
-43
lines changed

compiler/rustc_codegen_cranelift/src/vtable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub(crate) fn get_vtable<'tcx>(
6868
ty: Ty<'tcx>,
6969
trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
7070
) -> Value {
71-
let alloc = fx.tcx.vtable_allocation((ty, trait_ref));
71+
let alloc = fx.tcx.vtable_allocation((ty, trait_ref)).0;
7272
let alloc_id = fx.tcx.create_memory_alloc(alloc);
7373
let data_id =
7474
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.module, alloc_id, Mutability::Not);

compiler/rustc_codegen_ssa/src/meth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
7272
return val;
7373
}
7474

75-
let vtable_allocation = tcx.vtable_allocation((ty, trait_ref));
75+
let vtable_allocation = tcx.vtable_allocation((ty, trait_ref)).0;
7676
let vtable_const = cx.const_data_from_alloc(vtable_allocation);
7777
let align = cx.data_layout().pointer_align.abi;
7878
let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));

compiler/rustc_const_eval/src/interpret/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
3030
ensure_monomorphic_enough(*self.tcx, ty)?;
3131
ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?;
3232

33-
let vtable_allocation = self.tcx.vtable_allocation((ty, poly_trait_ref));
33+
let vtable_allocation = self.tcx.vtable_allocation((ty, poly_trait_ref)).0;
3434
let vtable_allocation_id = self.tcx.create_memory_alloc(vtable_allocation);
3535
let vtable_ptr = self.memory.global_base_pointer(Pointer::from(vtable_allocation_id))?;
3636

compiler/rustc_data_structures/src/stable_hasher.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
223223
}
224224
}
225225

226+
impl<CTX> HashStable<CTX> for ::std::num::NonZeroU64 {
227+
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
228+
self.get().hash_stable(ctx, hasher)
229+
}
230+
}
231+
226232
impl<CTX> HashStable<CTX> for ::std::num::NonZeroUsize {
227233
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
228234
self.get().hash_stable(ctx, hasher)

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,17 @@ use crate::ty;
2424
/// type to account for the lack of an AllocId on this level. The Miri/CTFE core engine `memory`
2525
/// module provides higher-level access.
2626
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]
27-
#[derive(HashStable)]
2827
pub struct Allocation<Tag = AllocId, Extra = ()> {
2928
/// The actual bytes of the allocation.
3029
/// Note that the bytes of a pointer represent the offset of the pointer.
31-
bytes: Box<[u8]>,
30+
pub(super) bytes: Box<[u8]>,
3231
/// Maps from byte addresses to extra data for each pointer.
3332
/// Only the first byte of a pointer is inserted into the map; i.e.,
3433
/// every entry in this map applies to `pointer_size` consecutive bytes starting
3534
/// at the given offset.
36-
relocations: Relocations<Tag>,
35+
pub(super) relocations: Relocations<Tag>,
3736
/// Denotes which part of this allocation is initialized.
38-
init_mask: InitMask,
37+
pub(super) init_mask: InitMask,
3938
/// The alignment of the allocation to detect unaligned reads.
4039
/// (`Align` guarantees that this is a power of two.)
4140
pub align: Align,
@@ -47,6 +46,12 @@ pub struct Allocation<Tag = AllocId, Extra = ()> {
4746
pub extra: Extra,
4847
}
4948

49+
/// This type wraps an Allocation, but hashes its contents (specifically its relocations)
50+
/// according to the contents of the AllocId that the relocation points to. This is important
51+
/// for HashStable and Eq to be stable and compatible.
52+
#[derive(Copy, Clone, Debug)]
53+
pub struct AllocationModuloRelocations<'tcx>(pub &'tcx Allocation);
54+
5055
/// We have our own error type that does not know about the `AllocId`; that information
5156
/// is added when converting to `InterpError`.
5257
#[derive(Debug)]

compiler/rustc_middle/src/mir/interpret/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ mod allocation;
9393
mod error;
9494
mod pointer;
9595
mod queries;
96+
mod structural_impls;
9697
mod value;
9798

9899
use std::convert::TryFrom;
@@ -126,7 +127,8 @@ pub use self::error::{
126127
pub use self::value::{get_slice_bytes, ConstAlloc, ConstValue, Scalar, ScalarMaybeUninit};
127128

128129
pub use self::allocation::{
129-
alloc_range, AllocRange, Allocation, InitChunk, InitChunkIter, InitMask, Relocations,
130+
alloc_range, AllocRange, Allocation, AllocationModuloRelocations, InitChunk, InitChunkIter,
131+
InitMask, Relocations,
130132
};
131133

132134
pub use self::pointer::{Pointer, PointerArithmetic, Provenance};
@@ -379,7 +381,7 @@ impl<'s> AllocDecodingSession<'s> {
379381

380382
/// An allocation in the global (tcx-managed) memory can be either a function pointer,
381383
/// a static, or a "real" allocation with some data in it.
382-
#[derive(Debug, Clone, Eq, PartialEq, Hash, TyDecodable, TyEncodable, HashStable)]
384+
#[derive(Debug, Clone, TyDecodable, TyEncodable)]
383385
pub enum GlobalAlloc<'tcx> {
384386
/// The alloc ID is used as a function pointer.
385387
Function(Instance<'tcx>),

0 commit comments

Comments
 (0)