Skip to content

Commit d9eaaf5

Browse files
committed
Derive HashStable in librustc_mir.
1 parent e1522fa commit d9eaaf5

File tree

6 files changed

+37
-57
lines changed

6 files changed

+37
-57
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3720,6 +3720,7 @@ dependencies = [
37203720
"rustc_errors",
37213721
"rustc_index",
37223722
"rustc_lexer",
3723+
"rustc_macros",
37233724
"rustc_target",
37243725
"serialize",
37253726
"smallvec 1.0.0",

src/librustc_mir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ rustc_data_structures = { path = "../librustc_data_structures" }
2222
rustc_index = { path = "../librustc_index" }
2323
rustc_errors = { path = "../librustc_errors" }
2424
rustc_lexer = { path = "../librustc_lexer" }
25+
rustc_macros = { path = "../librustc_macros" }
2526
rustc_serialize = { path = "../libserialize", package = "serialize" }
2627
syntax = { path = "../libsyntax" }
2728
syntax_pos = { path = "../libsyntax_pos" }

src/librustc_mir/interpret/eval_context.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fmt::Write;
33
use std::mem;
44

55
use syntax::source_map::{self, Span, DUMMY_SP};
6+
use rustc::ich::StableHashingContext;
67
use rustc::hir::def_id::DefId;
78
use rustc::hir::def::DefKind;
89
use rustc::mir;
@@ -18,6 +19,8 @@ use rustc::mir::interpret::{
1819
InterpResult, truncate, sign_extend,
1920
};
2021
use rustc_data_structures::fx::FxHashMap;
22+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
23+
use rustc_macros::HashStable;
2124

2225
use super::{
2326
Immediate, Operand, MemPlace, MPlaceTy, Place, PlaceTy, ScalarMaybeUndef,
@@ -93,7 +96,7 @@ pub struct Frame<'mir, 'tcx, Tag=(), Extra=()> {
9396
pub stmt: usize,
9497
}
9598

96-
#[derive(Clone, Eq, PartialEq, Debug)] // Miri debug-prints these
99+
#[derive(Clone, Eq, PartialEq, Debug, HashStable)] // Miri debug-prints these
97100
pub enum StackPopCleanup {
98101
/// Jump to the next block in the caller, or cause UB if None (that's a function
99102
/// that may never return). Also store layout of return place so
@@ -109,15 +112,16 @@ pub enum StackPopCleanup {
109112
}
110113

111114
/// State of a local variable including a memoized layout
112-
#[derive(Clone, PartialEq, Eq)]
115+
#[derive(Clone, PartialEq, Eq, HashStable)]
113116
pub struct LocalState<'tcx, Tag=(), Id=AllocId> {
114117
pub value: LocalValue<Tag, Id>,
115118
/// Don't modify if `Some`, this is only used to prevent computing the layout twice
119+
#[stable_hasher(ignore)]
116120
pub layout: Cell<Option<TyLayout<'tcx>>>,
117121
}
118122

119123
/// Current value of a local variable
120-
#[derive(Clone, PartialEq, Eq, Debug)] // Miri debug-prints these
124+
#[derive(Clone, PartialEq, Eq, Debug, HashStable)] // Miri debug-prints these
121125
pub enum LocalValue<Tag=(), Id=AllocId> {
122126
/// This local is not currently alive, and cannot be used at all.
123127
Dead,
@@ -827,3 +831,21 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
827831
frames
828832
}
829833
}
834+
835+
impl<'ctx, 'mir, 'tcx, Tag, Extra> HashStable<StableHashingContext<'ctx>>
836+
for Frame<'mir, 'tcx, Tag, Extra>
837+
where Extra: HashStable<StableHashingContext<'ctx>>,
838+
Tag: HashStable<StableHashingContext<'ctx>>
839+
{
840+
fn hash_stable(&self, hcx: &mut StableHashingContext<'ctx>, hasher: &mut StableHasher) {
841+
self.body.hash_stable(hcx, hasher);
842+
self.instance.hash_stable(hcx, hasher);
843+
self.span.hash_stable(hcx, hasher);
844+
self.return_to_block.hash_stable(hcx, hasher);
845+
self.return_place.as_ref().map(|r| &**r).hash_stable(hcx, hasher);
846+
self.locals.hash_stable(hcx, hasher);
847+
self.block.hash_stable(hcx, hasher);
848+
self.stmt.hash_stable(hcx, hasher);
849+
self.extra.hash_stable(hcx, hasher);
850+
}
851+
}

src/librustc_mir/interpret/operand.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use super::{
1818
MemPlace, MPlaceTy, PlaceTy, Place,
1919
};
2020
pub use rustc::mir::interpret::ScalarMaybeUndef;
21+
use rustc_macros::HashStable;
2122

2223
/// An `Immediate` represents a single immediate self-contained Rust value.
2324
///
@@ -26,7 +27,7 @@ pub use rustc::mir::interpret::ScalarMaybeUndef;
2627
/// operations and fat pointers. This idea was taken from rustc's codegen.
2728
/// In particular, thanks to `ScalarPair`, arithmetic operations and casts can be entirely
2829
/// defined on `Immediate`, and do not have to work with a `Place`.
29-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
30+
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable)]
3031
pub enum Immediate<Tag=(), Id=AllocId> {
3132
Scalar(ScalarMaybeUndef<Tag, Id>),
3233
ScalarPair(ScalarMaybeUndef<Tag, Id>, ScalarMaybeUndef<Tag, Id>),
@@ -103,7 +104,7 @@ impl<'tcx, Tag> ::std::ops::Deref for ImmTy<'tcx, Tag> {
103104
/// An `Operand` is the result of computing a `mir::Operand`. It can be immediate,
104105
/// or still in memory. The latter is an optimization, to delay reading that chunk of
105106
/// memory and to avoid having to store arbitrary-sized data here.
106-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
107+
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable)]
107108
pub enum Operand<Tag=(), Id=AllocId> {
108109
Immediate(Immediate<Tag, Id>),
109110
Indirect(MemPlace<Tag, Id>),

src/librustc_mir/interpret/place.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ use rustc::ty::layout::{
1212
self, Size, Align, LayoutOf, TyLayout, HasDataLayout, VariantIdx, PrimitiveExt
1313
};
1414
use rustc::ty::TypeFoldable;
15+
use rustc_macros::HashStable;
1516

1617
use super::{
1718
GlobalId, AllocId, Allocation, Scalar, InterpResult, Pointer, PointerArithmetic,
1819
InterpCx, Machine, AllocMap, AllocationExtra,
1920
RawConst, Immediate, ImmTy, ScalarMaybeUndef, Operand, OpTy, MemoryKind, LocalValue,
2021
};
2122

22-
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
23+
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, HashStable)]
2324
pub struct MemPlace<Tag=(), Id=AllocId> {
2425
/// A place may have an integral pointer for ZSTs, and since it might
2526
/// be turned back into a reference before ever being dereferenced.
@@ -32,7 +33,7 @@ pub struct MemPlace<Tag=(), Id=AllocId> {
3233
pub meta: Option<Scalar<Tag, Id>>,
3334
}
3435

35-
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
36+
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, HashStable)]
3637
pub enum Place<Tag=(), Id=AllocId> {
3738
/// A place referring to a value allocated in the `Memory` system.
3839
Ptr(MemPlace<Tag, Id>),

src/librustc_mir/interpret/snapshot.rs

+4-50
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc::ty::layout::{Align, Size};
1919
use rustc_data_structures::fx::FxHashSet;
2020
use rustc_index::vec::IndexVec;
2121
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
22+
use rustc_macros::HashStable;
2223
use syntax::ast::Mutability;
2324
use syntax::source_map::Span;
2425

@@ -197,21 +198,12 @@ impl_snapshot_for!(enum ScalarMaybeUndef {
197198
Undef,
198199
});
199200

200-
impl_stable_hash_for!(struct crate::interpret::MemPlace {
201-
ptr,
202-
align,
203-
meta,
204-
});
205201
impl_snapshot_for!(struct MemPlace {
206202
ptr,
207203
meta,
208204
align -> *align, // just copy alignment verbatim
209205
});
210206

211-
impl_stable_hash_for!(enum crate::interpret::Place {
212-
Ptr(mem_place),
213-
Local { frame, local },
214-
});
215207
impl<'a, Ctx> Snapshot<'a, Ctx> for Place
216208
where Ctx: SnapshotContext<'a>,
217209
{
@@ -229,29 +221,16 @@ impl<'a, Ctx> Snapshot<'a, Ctx> for Place
229221
}
230222
}
231223

232-
impl_stable_hash_for!(enum crate::interpret::Immediate {
233-
Scalar(x),
234-
ScalarPair(x, y),
235-
});
236224
impl_snapshot_for!(enum Immediate {
237225
Scalar(s),
238226
ScalarPair(s, t),
239227
});
240228

241-
impl_stable_hash_for!(enum crate::interpret::Operand {
242-
Immediate(x),
243-
Indirect(x),
244-
});
245229
impl_snapshot_for!(enum Operand {
246230
Immediate(v),
247231
Indirect(m),
248232
});
249233

250-
impl_stable_hash_for!(enum crate::interpret::LocalValue {
251-
Dead,
252-
Uninitialized,
253-
Live(x),
254-
});
255234
impl_snapshot_for!(enum LocalValue {
256235
Dead,
257236
Uninitialized,
@@ -314,11 +293,6 @@ impl<'a, Ctx> Snapshot<'a, Ctx> for &'a Allocation
314293
}
315294
}
316295

317-
impl_stable_hash_for!(enum crate::interpret::eval_context::StackPopCleanup {
318-
Goto { ret, unwind },
319-
None { cleanup },
320-
});
321-
322296
#[derive(Eq, PartialEq)]
323297
struct FrameSnapshot<'a, 'tcx> {
324298
instance: ty::Instance<'tcx>,
@@ -330,18 +304,6 @@ struct FrameSnapshot<'a, 'tcx> {
330304
stmt: usize,
331305
}
332306

333-
impl_stable_hash_for!(impl<> for struct Frame<'mir, 'tcx> {
334-
body,
335-
instance,
336-
span,
337-
return_to_block,
338-
return_place -> (return_place.as_ref().map(|r| &**r)),
339-
locals,
340-
block,
341-
stmt,
342-
extra,
343-
});
344-
345307
impl<'a, 'mir, 'tcx, Ctx> Snapshot<'a, Ctx> for &'a Frame<'mir, 'tcx>
346308
where Ctx: SnapshotContext<'a>,
347309
{
@@ -383,11 +345,6 @@ impl<'a, 'tcx, Ctx> Snapshot<'a, Ctx> for &'a LocalState<'tcx>
383345
}
384346
}
385347

386-
impl_stable_hash_for!(struct LocalState<'tcx> {
387-
value,
388-
layout -> _,
389-
});
390-
391348
impl<'b, 'mir, 'tcx> SnapshotContext<'b>
392349
for Memory<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>
393350
{
@@ -399,7 +356,10 @@ impl<'b, 'mir, 'tcx> SnapshotContext<'b>
399356
/// The virtual machine state during const-evaluation at a given point in time.
400357
/// We assume the `CompileTimeInterpreter` has no interesting extra state that
401358
/// is worth considering here.
359+
#[derive(HashStable)]
402360
struct InterpSnapshot<'mir, 'tcx> {
361+
// Not hashing memory: Avoid hashing memory all the time during execution
362+
#[stable_hasher(ignore)]
403363
memory: Memory<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
404364
stack: Vec<Frame<'mir, 'tcx>>,
405365
}
@@ -434,12 +394,6 @@ impl<'mir, 'tcx> Hash for InterpSnapshot<'mir, 'tcx> {
434394
}
435395
}
436396

437-
impl_stable_hash_for!(impl<> for struct InterpSnapshot<'mir, 'tcx> {
438-
// Not hashing memory: Avoid hashing memory all the time during execution
439-
memory -> _,
440-
stack,
441-
});
442-
443397
impl<'mir, 'tcx> Eq for InterpSnapshot<'mir, 'tcx> {}
444398

445399
impl<'mir, 'tcx> PartialEq for InterpSnapshot<'mir, 'tcx> {

0 commit comments

Comments
 (0)