Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HashStable implementation on AllocId #93472

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_cranelift/src/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ pub(crate) fn get_vtable<'tcx>(
ty: Ty<'tcx>,
trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
) -> Value {
let alloc_id = fx.tcx.vtable_allocation((ty, trait_ref));
let alloc = fx.tcx.vtable_allocation((ty, trait_ref)).0;
let alloc_id = fx.tcx.create_memory_alloc(alloc);
let data_id =
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.module, alloc_id, Mutability::Not);
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_ssa/src/meth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
return val;
}

let vtable_alloc_id = tcx.vtable_allocation((ty, trait_ref));
let vtable_allocation = tcx.global_alloc(vtable_alloc_id).unwrap_memory();
let vtable_allocation = tcx.vtable_allocation((ty, trait_ref)).0;
let vtable_const = cx.const_data_from_alloc(vtable_allocation);
let align = cx.data_layout().pointer_align.abi;
let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_const_eval/src/interpret/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
ensure_monomorphic_enough(*self.tcx, ty)?;
ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?;

let vtable_allocation = self.tcx.vtable_allocation((ty, poly_trait_ref));

let vtable_ptr = self.memory.global_base_pointer(Pointer::from(vtable_allocation))?;
let vtable_allocation = self.tcx.vtable_allocation((ty, poly_trait_ref)).0;
let vtable_allocation_id = self.tcx.create_memory_alloc(vtable_allocation);
let vtable_ptr = self.memory.global_base_pointer(Pointer::from(vtable_allocation_id))?;

Ok(vtable_ptr.into())
}
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_data_structures/src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
}
}

impl<CTX> HashStable<CTX> for ::std::num::NonZeroU64 {
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
self.get().hash_stable(ctx, hasher)
}
}

impl<CTX> HashStable<CTX> for ::std::num::NonZeroUsize {
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
self.get().hash_stable(ctx, hasher)
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_middle/src/mir/interpret/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ use crate::ty;
/// type to account for the lack of an AllocId on this level. The Miri/CTFE core engine `memory`
/// module provides higher-level access.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]
#[derive(HashStable)]
pub struct Allocation<Tag = AllocId, Extra = ()> {
/// The actual bytes of the allocation.
/// Note that the bytes of a pointer represent the offset of the pointer.
bytes: Box<[u8]>,
pub(super) bytes: Box<[u8]>,
/// Maps from byte addresses to extra data for each pointer.
/// Only the first byte of a pointer is inserted into the map; i.e.,
/// every entry in this map applies to `pointer_size` consecutive bytes starting
/// at the given offset.
relocations: Relocations<Tag>,
pub(super) relocations: Relocations<Tag>,
/// Denotes which part of this allocation is initialized.
init_mask: InitMask,
pub(super) init_mask: InitMask,
/// The alignment of the allocation to detect unaligned reads.
/// (`Align` guarantees that this is a power of two.)
pub align: Align,
Expand All @@ -47,6 +46,12 @@ pub struct Allocation<Tag = AllocId, Extra = ()> {
pub extra: Extra,
}

/// This type wraps an Allocation, but hashes its contents (specifically its relocations)
/// according to the contents of the AllocId that the relocation points to. This is important
/// for HashStable and Eq to be stable and compatible.
#[derive(Copy, Clone, Debug)]
pub struct AllocationModuloRelocations<'tcx>(pub &'tcx Allocation);

/// We have our own error type that does not know about the `AllocId`; that information
/// is added when converting to `InterpError`.
#[derive(Debug)]
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_middle/src/mir/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ mod allocation;
mod error;
mod pointer;
mod queries;
mod structural_impls;
mod value;

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

pub use self::allocation::{
alloc_range, AllocRange, Allocation, InitChunk, InitChunkIter, InitMask, Relocations,
alloc_range, AllocRange, Allocation, AllocationModuloRelocations, InitChunk, InitChunkIter,
InitMask, Relocations,
};

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

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