about summary refs log tree commit diff
path: root/tests/ui/statics/read_before_init.rs
blob: 32cc2554e1a16e55d05d78cdc31e82cfa4940b40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//! This test checks the one code path that does not go through
//! the regular CTFE memory access (as an optimization). We forgot
//! to duplicate the static item self-initialization check, allowing
//! reading from the uninitialized static memory before it was
//! initialized at the end of the static initializer.
//!
//! https://github.com/rust-lang/rust/issues/142532

use std::mem::MaybeUninit;

pub static X: (i32, MaybeUninit<i32>) = (1, foo(&X.0, 1));
//~^ ERROR: encountered static that tried to access itself during initialization
pub static Y: (i32, MaybeUninit<i32>) = (1, foo(&Y.0, 0));
//~^ ERROR: encountered static that tried to access itself during initialization

const fn foo(x: &i32, num: usize) -> MaybeUninit<i32> {
    let mut temp = MaybeUninit::<i32>::uninit();
    unsafe {
        std::ptr::copy(x, temp.as_mut_ptr(), num);
    }
    temp
}

fn main() {}