Add tests showing how we duplicate allocations when we shouldn't

This commit is contained in:
Amanjeev Sethi 2022-09-02 19:12:03 -04:00 committed by Oli Scherer
parent 5aad51d015
commit 0a2475c50a
2 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,23 @@
// This is a support file for ../const-mut-refs-crate.rs
// This is to test that static inners from an external
// crate like this one, still preserves the alloc.
// That is, the address from the standpoint of rustc+llvm
// is the same.
// The need for this test originated from the GH issue
// https://github.com/rust-lang/rust/issues/57349
// See also ../const-mut-refs-crate.rs for more details
// about this test.
#![feature(const_mut_refs)]
// if we used immutable references here, then promotion would
// turn the `&42` into a promoted, which gets duplicated arbitrarily.
pub static mut FOO: &'static mut i32 = &mut 42;
pub static mut BAR: &'static mut i32 = unsafe { FOO };
pub mod inner {
pub static INNER_MOD_FOO: &'static i32 = &43;
pub static INNER_MOD_BAR: &'static i32 = INNER_MOD_FOO;
}

View File

@ -0,0 +1,35 @@
//@ run-pass
//@ aux-build:const_mut_refs_crate.rs
#![feature(const_mut_refs)]
//! Regression test for https://github.com/rust-lang/rust/issues/79738
//! Show how we are duplicationg allocations, even though static items that
//! copy their value from another static should not also be duplicating
//! memory behind references.
extern crate const_mut_refs_crate as other;
use other::{
inner::{INNER_MOD_BAR, INNER_MOD_FOO},
BAR, FOO,
};
pub static LOCAL_FOO: &'static i32 = &41;
pub static LOCAL_BAR: &'static i32 = LOCAL_FOO;
pub static mut COPY_OF_REMOTE_FOO: &'static mut i32 = unsafe { FOO };
static DOUBLE_REF: &&i32 = &&99;
static ONE_STEP_ABOVE: &i32 = *DOUBLE_REF;
pub fn main() {
unsafe {
assert_ne!(FOO as *const i32, BAR as *const i32);
assert_eq!(INNER_MOD_FOO as *const i32, INNER_MOD_BAR as *const i32);
assert_eq!(LOCAL_FOO as *const i32, LOCAL_BAR as *const i32);
assert_eq!(*DOUBLE_REF as *const i32, ONE_STEP_ABOVE as *const i32);
// bug!
assert_ne!(FOO as *const i32, COPY_OF_REMOTE_FOO as *const i32);
}
}