]> git.lizzy.rs Git - rust.git/blob - src/test/ui/unsafe/union-modification.rs
Don't run UB in test suite
[rust.git] / src / test / ui / unsafe / union-modification.rs
1 // run-pass
2 // revisions: mir thir
3 // [thir]compile-flags: -Z thir-unsafeck
4
5 #![feature(untagged_unions)]
6
7 union Foo {
8     bar: i8,
9     _blah: isize,
10     _zst: (),
11 }
12
13 struct FooHolder {
14     inner_foo: Foo
15 }
16
17 fn do_nothing(_x: &mut Foo) {}
18
19 pub fn main() {
20     let mut foo = Foo { bar: 5 };
21     do_nothing(&mut foo);
22     foo.bar = 6;
23     unsafe { foo.bar += 1; }
24     assert_eq!(unsafe { foo.bar }, 7);
25     unsafe {
26         let Foo { bar: inner } = foo;
27         assert_eq!(inner, 7);
28     }
29
30     let foo = Foo { bar: 5 };
31     let foo = if let 3 = if let true = true { 3 } else { 4 } { foo } else { foo };
32
33     let (_foo2, _random) = (foo, 42);
34
35     let mut foo_holder = FooHolder { inner_foo: Foo { bar: 5 } };
36     foo_holder.inner_foo.bar = 4;
37     assert_eq!(unsafe { foo_holder.inner_foo.bar }, 4);
38     drop(foo_holder);
39 }