]> git.lizzy.rs Git - rust.git/blob - src/test/ui/mir-dataflow/inits-1.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / mir-dataflow / inits-1.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // General test of maybe_inits state computed by MIR dataflow.
12
13 #![feature(nll)]
14 #![feature(core_intrinsics, rustc_attrs)]
15
16 use std::intrinsics::rustc_peek;
17 use std::mem::{drop, replace};
18
19 struct S(i32);
20
21 #[rustc_mir(rustc_peek_maybe_init,stop_after_dataflow)]
22 fn foo(test: bool, x: &mut S, y: S, mut z: S) -> S {
23     let ret;
24     // `ret` starts off uninitialized, so we get an error report here.
25     unsafe { rustc_peek(&ret); }  //~ ERROR rustc_peek: bit not set
26
27     // All function formal parameters start off initialized.
28
29     unsafe { rustc_peek(&x) };
30     unsafe { rustc_peek(&y) };
31     unsafe { rustc_peek(&z) };
32
33     ret = if test {
34         ::std::mem::replace(x, y)
35     } else {
36         z = y;
37         z
38     };
39
40
41     // `z` may be initialized here.
42     unsafe { rustc_peek(&z); }
43
44     // `y` is definitely uninitialized here.
45     unsafe { rustc_peek(&y); }  //~ ERROR rustc_peek: bit not set
46
47     // `x` is still (definitely) initialized (replace above is a reborrow).
48     unsafe { rustc_peek(&x); }
49
50     ::std::mem::drop(x);
51
52     // `x` is *definitely* uninitialized here
53     unsafe { rustc_peek(&x); } //~ ERROR rustc_peek: bit not set
54
55     // `ret` is now definitely initialized (via `if` above).
56     unsafe { rustc_peek(&ret); }
57
58     ret
59 }
60
61 fn main() {
62     foo(true, &mut S(13), S(14), S(15));
63     foo(false, &mut S(13), S(14), S(15));
64 }