Skip to content

Commit 051050d

Browse files
committed
Auto merge of #49424 - oli-obk:stable_allocid_hash, r=michaelwoerister
Fix stable hashing of AllocIds r? @michaelwoerister fixes #49081
2 parents 6960761 + fa60b72 commit 051050d

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

src/librustc/ich/hcx.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use hir::map::definitions::Definitions;
1515
use ich::{self, CachingCodemapView, Fingerprint};
1616
use middle::cstore::CrateStore;
1717
use ty::{TyCtxt, fast_reject};
18+
use mir::interpret::AllocId;
1819
use session::Session;
1920

2021
use std::cmp::Ord;
@@ -59,6 +60,8 @@ pub struct StableHashingContext<'a> {
5960
// CachingCodemapView, so we initialize it lazily.
6061
raw_codemap: &'a CodeMap,
6162
caching_codemap: Option<CachingCodemapView<'a>>,
63+
64+
pub(super) alloc_id_recursion_tracker: FxHashSet<AllocId>,
6265
}
6366

6467
#[derive(PartialEq, Eq, Clone, Copy)]
@@ -102,6 +105,7 @@ impl<'a> StableHashingContext<'a> {
102105
hash_spans: hash_spans_initial,
103106
hash_bodies: true,
104107
node_id_hashing_mode: NodeIdHashingMode::HashDefPath,
108+
alloc_id_recursion_tracker: Default::default(),
105109
}
106110
}
107111

src/librustc/ich/impls_ty.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,13 @@ impl_stable_hash_for!(struct mir::interpret::MemoryPointer {
379379
});
380380

381381
enum AllocDiscriminant {
382-
Static,
383-
Constant,
382+
Alloc,
383+
ExternStatic,
384384
Function,
385385
}
386386
impl_stable_hash_for!(enum self::AllocDiscriminant {
387-
Static,
388-
Constant,
387+
Alloc,
388+
ExternStatic,
389389
Function
390390
});
391391

@@ -397,17 +397,23 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::AllocId {
397397
) {
398398
ty::tls::with_opt(|tcx| {
399399
let tcx = tcx.expect("can't hash AllocIds during hir lowering");
400-
if let Some(def_id) = tcx.interpret_interner.get_corresponding_static_def_id(*self) {
401-
AllocDiscriminant::Static.hash_stable(hcx, hasher);
402-
// statics are unique via their DefId
403-
def_id.hash_stable(hcx, hasher);
404-
} else if let Some(alloc) = tcx.interpret_interner.get_alloc(*self) {
405-
// not a static, can't be recursive, hash the allocation
406-
AllocDiscriminant::Constant.hash_stable(hcx, hasher);
407-
alloc.hash_stable(hcx, hasher);
400+
if let Some(alloc) = tcx.interpret_interner.get_alloc(*self) {
401+
AllocDiscriminant::Alloc.hash_stable(hcx, hasher);
402+
if !hcx.alloc_id_recursion_tracker.insert(*self) {
403+
tcx
404+
.interpret_interner
405+
.get_corresponding_static_def_id(*self)
406+
.hash_stable(hcx, hasher);
407+
alloc.hash_stable(hcx, hasher);
408+
assert!(hcx.alloc_id_recursion_tracker.remove(self));
409+
}
408410
} else if let Some(inst) = tcx.interpret_interner.get_fn(*self) {
409411
AllocDiscriminant::Function.hash_stable(hcx, hasher);
410412
inst.hash_stable(hcx, hasher);
413+
} else if let Some(def_id) = tcx.interpret_interner
414+
.get_corresponding_static_def_id(*self) {
415+
AllocDiscriminant::ExternStatic.hash_stable(hcx, hasher);
416+
def_id.hash_stable(hcx, hasher);
411417
} else {
412418
bug!("no allocation for {}", self);
413419
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// https://github.com/rust-lang/rust/issues/49081
12+
13+
// revisions:rpass1 rpass2
14+
15+
#[cfg(rpass1)]
16+
pub static A: &str = "hello";
17+
#[cfg(rpass2)]
18+
pub static A: &str = "xxxxx";
19+
20+
#[cfg(rpass1)]
21+
fn main() {
22+
assert_eq!(A, "hello");
23+
}
24+
25+
#[cfg(rpass2)]
26+
fn main() {
27+
assert_eq!(A, "xxxxx");
28+
}

0 commit comments

Comments
 (0)