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