]> git.lizzy.rs Git - rust.git/blob - src/test/ui/mir-dataflow/def-inits-1.rs
07ac1900bc71c66e1192caad287bc66a21257236
[rust.git] / src / test / ui / mir-dataflow / def-inits-1.rs
1 // General test of maybe_uninits state computed by MIR dataflow.
2
3 #![feature(nll)]
4 #![feature(core_intrinsics, rustc_attrs)]
5
6 use std::intrinsics::rustc_peek;
7 use std::mem::{drop, replace};
8
9 struct S(i32);
10
11 #[rustc_mir(rustc_peek_definite_init,stop_after_dataflow)]
12 fn foo(test: bool, x: &mut S, y: S, mut z: S) -> S {
13     let ret;
14     // `ret` starts off uninitialized
15     unsafe { rustc_peek(&ret); }  //~ ERROR rustc_peek: bit not set
16
17     // All function formal parameters start off initialized.
18
19     unsafe { rustc_peek(&x) };
20     unsafe { rustc_peek(&y) };
21     unsafe { rustc_peek(&z) };
22
23     ret = if test {
24         ::std::mem::replace(x, y)
25     } else {
26         z = y;
27         z
28     };
29
30     // `z` may be uninitialized here.
31     unsafe { rustc_peek(&z); } //~ ERROR rustc_peek: bit not set
32
33     // `y` is definitely uninitialized here.
34     unsafe { rustc_peek(&y); } //~ ERROR rustc_peek: bit not set
35
36     // `x` is still (definitely) initialized (replace above is a reborrow).
37     unsafe { rustc_peek(&x); }
38
39     ::std::mem::drop(x);
40
41     // `x` is *definitely* uninitialized here
42     unsafe { rustc_peek(&x); } //~ ERROR rustc_peek: bit not set
43
44     // `ret` is now definitely initialized (via `if` above).
45     unsafe { rustc_peek(&ret); }
46
47     ret
48 }
49 fn main() {
50     foo(true, &mut S(13), S(14), S(15));
51     foo(false, &mut S(13), S(14), S(15));
52 }