about summary refs log tree commit diff
path: root/src/test/run-pass/union/union-basic.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-09-03 07:48:06 -0700
committerGitHub <noreply@github.com>2016-09-03 07:48:06 -0700
commitd748fa6ecccf6f5b4c7ae4abee0a2d206b165de2 (patch)
tree5ada2c11762533ba6c6f21a7593b015cb1a89912 /src/test/run-pass/union/union-basic.rs
parent01b35d82e51458870f61cf850b73479f253fcb54 (diff)
parent436cfe56534b405786816d4bbcccd11ed7571981 (diff)
Auto merge of #36016 - petrochenkov:union, r=nikomatsakis
Implement untagged unions (RFC 1444)

cc https://github.com/rust-lang/rust/issues/32836

Notes:
- The RFC doesn't talk about `#[packed]` unions, this implementation supports them, packing changes union's alignment to 1 and removes trailing padding.
- The RFC doesn't talk about dynamically sized unions, this implementation doesn't support them and rejects them during wf-checking (similarly, dynamically sized enums are not supported as well).
- The lint for drop fields in unions can't work precisely before monomorphization, so it works pessimistically - non-`Copy` generic fields are reported, types not implementing `Drop` directly, but having non-trivial drop code are reported.

    ```
    struct S(String); // Doesn't implement `Drop`
    union U<T> {
        a: S, // Reported
        b: T, // Reported
    }
    ```

- https://github.com/rust-lang/rust/pull/35764 was indeed helpful and landed timely, I didn't have to implement internal drop flags for unions.
- Unions are not permitted in constant patterns, because matching on union fields is unsafe, I didn't want unsafety checker to dig into all constants to uncover this possible unsafety.
- The RFC doesn't talk about `#[derive]`, generally trait impls cannot be derived for unions, but some of them can. I implemented only `#[derive(Copy)]` so far. In theory shallow `#[derive(Clone)]` can be derived as well if all union fields are `Copy`, I left it for later though, it requires changing how `Clone` impls are generated.
- Moving union fields is implemented as per https://github.com/rust-lang/rust/issues/32836#issuecomment-242511491.
- Testing strategy: union specific behavior is tested, sometimes very basically (e.g. debuginfo), behavior common for all ADTs (e.g. something like coherence
checks) is not generally tested.

r? @eddyb
Diffstat (limited to 'src/test/run-pass/union/union-basic.rs')
-rw-r--r--src/test/run-pass/union/union-basic.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/test/run-pass/union/union-basic.rs b/src/test/run-pass/union/union-basic.rs
new file mode 100644
index 00000000000..d23af4b41b7
--- /dev/null
+++ b/src/test/run-pass/union/union-basic.rs
@@ -0,0 +1,68 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// aux-build:union.rs
+
+#![feature(untagged_unions)]
+
+extern crate union;
+use std::mem::{size_of, align_of, zeroed};
+
+union U {
+    a: u8,
+    b: u16
+}
+
+fn local() {
+    assert_eq!(size_of::<U>(), 2);
+    assert_eq!(align_of::<U>(), 2);
+
+    let u = U { a: 10 };
+    unsafe {
+        assert_eq!(u.a, 10);
+        let U { a } = u;
+        assert_eq!(a, 10);
+    }
+
+    let mut w = U { b: 0 };
+    unsafe {
+        assert_eq!(w.a, 0);
+        assert_eq!(w.b, 0);
+        w.a = 1;
+        assert_eq!(w.a, 1);
+        assert_eq!(w.b, 1);
+    }
+}
+
+fn xcrate() {
+    assert_eq!(size_of::<union::U>(), 2);
+    assert_eq!(align_of::<union::U>(), 2);
+
+    let u = union::U { a: 10 };
+    unsafe {
+        assert_eq!(u.a, 10);
+        let union::U { a } = u;
+        assert_eq!(a, 10);
+    }
+
+    let mut w = union::U { b: 0 };
+    unsafe {
+        assert_eq!(w.a, 0);
+        assert_eq!(w.b, 0);
+        w.a = 1;
+        assert_eq!(w.a, 1);
+        assert_eq!(w.b, 1);
+    }
+}
+
+fn main() {
+    local();
+    xcrate();
+}