]> git.proxmox.com Git - rustc.git/blame - src/test/compile-fail/mir-dataflow/uninits-1.rs
New upstream version 1.15.0+dfsg1
[rustc.git] / src / test / compile-fail / mir-dataflow / uninits-1.rs
CommitLineData
3157f602
XL
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_uninits state computed by MIR dataflow.
12
476ff2be 13#![feature(core_intrinsics, rustc_attrs)]
3157f602
XL
14
15use std::intrinsics::rustc_peek;
16use std::mem::{drop, replace};
17
18struct S(i32);
19
20#[rustc_mir_borrowck]
21#[rustc_mir(rustc_peek_maybe_uninit,stop_after_dataflow)]
22fn foo(test: bool, x: &mut S, y: S, mut z: S) -> S {
23 let ret;
24 // `ret` starts off uninitialized
25 unsafe { rustc_peek(&ret); }
26
27 // All function formal parameters start off initialized.
28
29 unsafe { rustc_peek(&x) }; //~ ERROR rustc_peek: bit not set
30 unsafe { rustc_peek(&y) }; //~ ERROR rustc_peek: bit not set
31 unsafe { rustc_peek(&z) }; //~ ERROR rustc_peek: bit not set
32
33 ret = if test {
34 ::std::mem::replace(x, y)
35 } else {
36 z = y;
37 z
38 };
39
40 // `z` may be uninitialized here.
41 unsafe { rustc_peek(&z); }
42
43 // `y` is definitely uninitialized here.
44 unsafe { rustc_peek(&y); }
45
46 // `x` is still (definitely) initialized (replace above is a reborrow).
47 unsafe { rustc_peek(&x); } //~ ERROR rustc_peek: bit not set
48
49 ::std::mem::drop(x);
50
51 // `x` is *definitely* uninitialized here
52 unsafe { rustc_peek(&x); }
53
54 // `ret` is now definitely initialized (via `if` above).
55 unsafe { rustc_peek(&ret); } //~ ERROR rustc_peek: bit not set
56
57 ret
58}
59fn main() {
60 foo(true, &mut S(13), S(14), S(15));
61 foo(false, &mut S(13), S(14), S(15));
62}