Skip to content

Commit 9880e29

Browse files
committed
Rollup merge of rust-lang#55302 - goffrie:impl-stable-hash, r=varkor
Extend the impl_stable_hash_for! macro for miri. Fixes rust-lang#54075.
2 parents e897fe7 + 4747d83 commit 9880e29

File tree

2 files changed

+40
-76
lines changed

2 files changed

+40
-76
lines changed

src/librustc/macros.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ macro_rules! __impl_stable_hash_field {
8383
macro_rules! impl_stable_hash_for {
8484
// FIXME(mark-i-m): Some of these should be `?` rather than `*`. See the git blame and change
8585
// them back when `?` is supported again.
86-
(enum $enum_name:path { $( $variant:ident $( ( $($field:ident $(-> $delegate:tt)*),* ) )* ),* $(,)* }) => {
86+
(enum $enum_name:path {
87+
$( $variant:ident
88+
// this incorrectly allows specifying both tuple-like and struct-like fields, as in `Variant(a,b){c,d}`,
89+
// when it should be only one or the other
90+
$( ( $($field:ident $(-> $delegate:tt)*),* ) )*
91+
$( { $($named_field:ident $(-> $named_delegate:tt)*),* } )*
92+
),* $(,)*
93+
}) => {
8794
impl<'a, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $enum_name {
8895
#[inline]
8996
fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
@@ -94,8 +101,9 @@ macro_rules! impl_stable_hash_for {
94101

95102
match *self {
96103
$(
97-
$variant $( ( $(ref $field),* ) )* => {
104+
$variant $( ( $(ref $field),* ) )* $( { $(ref $named_field),* } )* => {
98105
$($( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)*) );*)*
106+
$($( __impl_stable_hash_field!($named_field, __ctx, __hasher $(, $named_delegate)*) );*)*
99107
}
100108
)*
101109
}
@@ -133,10 +141,11 @@ macro_rules! impl_stable_hash_for {
133141
}
134142
};
135143

136-
(impl<$tcx:lifetime $(, $T:ident)*> for struct $struct_name:path {
137-
$($field:ident),* $(,)*
144+
(impl<$tcx:lifetime $(, $lt:lifetime $(: $lt_bound:lifetime)*)* $(, $T:ident)*> for struct $struct_name:path {
145+
$($field:ident $(-> $delegate:tt)*),* $(,)*
138146
}) => {
139-
impl<'a, $tcx, $($T,)*> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name
147+
impl<'a, $tcx, $($lt $(: $lt_bound)*,)* $($T,)*>
148+
::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name
140149
where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),*
141150
{
142151
#[inline]
@@ -147,7 +156,7 @@ macro_rules! impl_stable_hash_for {
147156
$(ref $field),*
148157
} = *self;
149158

150-
$( $field.hash_stable(__ctx, __hasher));*
159+
$( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)*) );*
151160
}
152161
}
153162
};

src/librustc_mir/interpret/snapshot.rs

Lines changed: 25 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
// it is not used by the general miri engine, just by CTFE.
77

88
use std::hash::{Hash, Hasher};
9-
use std::mem;
109

11-
use rustc::ich::{StableHashingContext, StableHashingContextProvider};
10+
use rustc::ich::StableHashingContextProvider;
1211
use rustc::mir;
1312
use rustc::mir::interpret::{
1413
AllocId, Pointer, Scalar,
@@ -20,7 +19,7 @@ use rustc::ty::{self, TyCtxt};
2019
use rustc::ty::layout::Align;
2120
use rustc_data_structures::fx::FxHashSet;
2221
use rustc_data_structures::indexed_vec::IndexVec;
23-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHasherResult};
22+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2423
use syntax::ast::Mutability;
2524
use syntax::source_map::Span;
2625

@@ -217,23 +216,10 @@ impl_snapshot_for!(struct MemPlace {
217216
align -> *align, // just copy alignment verbatim
218217
});
219218

220-
// Can't use the macro here because that does not support named enum fields.
221-
impl<'a> HashStable<StableHashingContext<'a>> for Place {
222-
fn hash_stable<W: StableHasherResult>(
223-
&self, hcx: &mut StableHashingContext<'a>,
224-
hasher: &mut StableHasher<W>)
225-
{
226-
mem::discriminant(self).hash_stable(hcx, hasher);
227-
match self {
228-
Place::Ptr(mem_place) => mem_place.hash_stable(hcx, hasher),
229-
230-
Place::Local { frame, local } => {
231-
frame.hash_stable(hcx, hasher);
232-
local.hash_stable(hcx, hasher);
233-
},
234-
}
235-
}
236-
}
219+
impl_stable_hash_for!(enum ::interpret::Place {
220+
Ptr(mem_place),
221+
Local { frame, local },
222+
});
237223
impl<'a, Ctx> Snapshot<'a, Ctx> for Place
238224
where Ctx: SnapshotContext<'a>,
239225
{
@@ -317,20 +303,10 @@ impl<'a, Ctx> Snapshot<'a, Ctx> for &'a Allocation
317303
}
318304
}
319305

320-
// Can't use the macro here because that does not support named enum fields.
321-
impl<'a> HashStable<StableHashingContext<'a>> for StackPopCleanup {
322-
fn hash_stable<W: StableHasherResult>(
323-
&self,
324-
hcx: &mut StableHashingContext<'a>,
325-
hasher: &mut StableHasher<W>)
326-
{
327-
mem::discriminant(self).hash_stable(hcx, hasher);
328-
match self {
329-
StackPopCleanup::Goto(ref block) => block.hash_stable(hcx, hasher),
330-
StackPopCleanup::None { cleanup } => cleanup.hash_stable(hcx, hasher),
331-
}
332-
}
333-
}
306+
impl_stable_hash_for!(enum ::interpret::eval_context::StackPopCleanup {
307+
Goto(block),
308+
None { cleanup },
309+
});
334310

335311
#[derive(Eq, PartialEq)]
336312
struct FrameSnapshot<'a, 'tcx: 'a> {
@@ -343,28 +319,17 @@ struct FrameSnapshot<'a, 'tcx: 'a> {
343319
stmt: usize,
344320
}
345321

346-
// Not using the macro because that does not support types depending on two lifetimes
347-
impl<'a, 'mir, 'tcx: 'mir> HashStable<StableHashingContext<'a>> for Frame<'mir, 'tcx> {
348-
fn hash_stable<W: StableHasherResult>(
349-
&self,
350-
hcx: &mut StableHashingContext<'a>,
351-
hasher: &mut StableHasher<W>) {
352-
353-
let Frame {
354-
mir,
355-
instance,
356-
span,
357-
return_to_block,
358-
return_place,
359-
locals,
360-
block,
361-
stmt,
362-
} = self;
322+
impl_stable_hash_for!(impl<'tcx, 'mir: 'tcx> for struct Frame<'mir, 'tcx> {
323+
mir,
324+
instance,
325+
span,
326+
return_to_block,
327+
return_place -> (return_place.as_ref().map(|r| &**r)),
328+
locals,
329+
block,
330+
stmt,
331+
});
363332

364-
(mir, instance, span, return_to_block).hash_stable(hcx, hasher);
365-
(return_place.as_ref().map(|r| &**r), locals, block, stmt).hash_stable(hcx, hasher);
366-
}
367-
}
368333
impl<'a, 'mir, 'tcx, Ctx> Snapshot<'a, Ctx> for &'a Frame<'mir, 'tcx>
369334
where Ctx: SnapshotContext<'a>,
370335
{
@@ -443,21 +408,11 @@ impl<'a, 'mir, 'tcx> Hash for EvalSnapshot<'a, 'mir, 'tcx>
443408
}
444409
}
445410

446-
// Not using the macro because we need special handling for `memory`, which the macro
447-
// does not support at the same time as the extra bounds on the type.
448-
impl<'a, 'b, 'mir, 'tcx> HashStable<StableHashingContext<'b>>
449-
for EvalSnapshot<'a, 'mir, 'tcx>
450-
{
451-
fn hash_stable<W: StableHasherResult>(
452-
&self,
453-
hcx: &mut StableHashingContext<'b>,
454-
hasher: &mut StableHasher<W>)
455-
{
456-
// Not hashing memory: Avoid hashing memory all the time during execution
457-
let EvalSnapshot{ memory: _, stack } = self;
458-
stack.hash_stable(hcx, hasher);
459-
}
460-
}
411+
impl_stable_hash_for!(impl<'tcx, 'b, 'mir> for struct EvalSnapshot<'b, 'mir, 'tcx> {
412+
// Not hashing memory: Avoid hashing memory all the time during execution
413+
memory -> _,
414+
stack,
415+
});
461416

462417
impl<'a, 'mir, 'tcx> Eq for EvalSnapshot<'a, 'mir, 'tcx>
463418
{}

0 commit comments

Comments
 (0)