]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issues/issue-49298.rs
db3c9792f3560fc532fabb9ac96db07908ea62ce
[rust.git] / src / test / run-pass / issues / issue-49298.rs
1 // Copyright 2018 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 // run-pass
12 #![feature(test)]
13 #![allow(unused_mut)] // under NLL we get warning about `x` below: rust-lang/rust#54499
14
15 // This test is bogus (i.e. should be compile-fail) during the period
16 // where #54986 is implemented and #54987 is *not* implemented. For
17 // now: just ignore it under nll
18 //
19 // ignore-compare-mode-nll
20
21 // This test is checking that the space allocated for `x.1` does not
22 // overlap with `y`. (The reason why such a thing happened at one
23 // point was because `x.0: Void` and thus the whole type of `x` was
24 // uninhabited, and so the compiler thought it was safe to use the
25 // space of `x.1` to hold `y`.)
26 //
27 // That's a fine thing to test when this code is accepted by the
28 // compiler, and this code is being transcribed accordingly into
29 // the ui test issue-21232-partial-init-and-use.rs
30
31 extern crate test;
32
33 enum Void {}
34
35 fn main() {
36     let mut x: (Void, usize);
37     let mut y = 42;
38     x.1 = 13;
39
40     // Make sure `y` stays on the stack.
41     test::black_box(&mut y);
42
43     // Check that the write to `x.1` did not overwrite `y`.
44     // Note that this doesn't fail with optimizations enabled,
45     // because we can't keep `x.1` on the stack, like we can `y`,
46     // as we can't borrow partially initialized variables.
47     assert_eq!(y.to_string(), "42");
48
49     // Check that `(Void, usize)` has space for the `usize` field.
50     assert_eq!(std::mem::size_of::<(Void, usize)>(),
51                std::mem::size_of::<usize>());
52 }