Skip to content

Commit 6958deb

Browse files
amanjeevoli-obk
authored andcommitted
Add tests showing how we duplicate allocations when we shouldn't
1 parent 6dadb6e commit 6958deb

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This is a support file for ../const-mut-refs-crate.rs
2+
3+
// This is to test that static inners from an external
4+
// crate like this one, still preserves the alloc.
5+
// That is, the address from the standpoint of rustc+llvm
6+
// is the same.
7+
// The need for this test originated from the GH issue
8+
// https://github.com/rust-lang/rust/issues/57349
9+
10+
// See also ../const-mut-refs-crate.rs for more details
11+
// about this test.
12+
13+
#![feature(const_mut_refs)]
14+
15+
// if we used immutable references here, then promotion would
16+
// turn the `&42` into a promoted, which gets duplicated arbitrarily.
17+
pub static mut FOO: &'static mut i32 = &mut 42;
18+
pub static mut BAR: &'static mut i32 = unsafe { FOO };
19+
20+
pub mod inner {
21+
pub static INNER_MOD_FOO: &'static i32 = &43;
22+
pub static INNER_MOD_BAR: &'static i32 = INNER_MOD_FOO;
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//@ run-pass
2+
//@ aux-build:const_mut_refs_crate.rs
3+
4+
#![feature(const_mut_refs)]
5+
6+
//! Regression test for https://github.com/rust-lang/rust/issues/79738
7+
//! Show how we are duplicationg allocations, even though static items that
8+
//! copy their value from another static should not also be duplicating
9+
//! memory behind references.
10+
11+
extern crate const_mut_refs_crate as other;
12+
13+
use other::{
14+
inner::{INNER_MOD_BAR, INNER_MOD_FOO},
15+
BAR, FOO,
16+
};
17+
18+
pub static LOCAL_FOO: &'static i32 = &41;
19+
pub static LOCAL_BAR: &'static i32 = LOCAL_FOO;
20+
pub static mut COPY_OF_REMOTE_FOO: &'static mut i32 = unsafe { FOO };
21+
22+
static DOUBLE_REF: &&i32 = &&99;
23+
static ONE_STEP_ABOVE: &i32 = *DOUBLE_REF;
24+
25+
pub fn main() {
26+
unsafe {
27+
assert_ne!(FOO as *const i32, BAR as *const i32);
28+
assert_eq!(INNER_MOD_FOO as *const i32, INNER_MOD_BAR as *const i32);
29+
assert_eq!(LOCAL_FOO as *const i32, LOCAL_BAR as *const i32);
30+
assert_eq!(*DOUBLE_REF as *const i32, ONE_STEP_ABOVE as *const i32);
31+
32+
// bug!
33+
assert_ne!(FOO as *const i32, COPY_OF_REMOTE_FOO as *const i32);
34+
}
35+
}

0 commit comments

Comments
 (0)