]> git.lizzy.rs Git - rust.git/blob - src/test/ui/mir-dataflow/inits-1.rs
13f900e4a75eeb7a8b0dd94373c92e89605102a4
[rust.git] / src / test / ui / mir-dataflow / inits-1.rs
1 // General test of maybe_inits 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_maybe_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, so we get an error report here.
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
31     // `z` may be initialized here.
32     unsafe { rustc_peek(&z); }
33
34     // `y` is definitely uninitialized here.
35     unsafe { rustc_peek(&y); }  //~ ERROR rustc_peek: bit not set
36
37     // `x` is still (definitely) initialized (replace above is a reborrow).
38     unsafe { rustc_peek(&x); }
39
40     ::std::mem::drop(x);
41
42     // `x` is *definitely* uninitialized here
43     unsafe { rustc_peek(&x); } //~ ERROR rustc_peek: bit not set
44
45     // `ret` is now definitely initialized (via `if` above).
46     unsafe { rustc_peek(&ret); }
47
48     ret
49 }
50
51 fn main() {
52     foo(true, &mut S(13), S(14), S(15));
53     foo(false, &mut S(13), S(14), S(15));
54 }