From 93067ca089ea570e4e2bdfc456958c81a4d1e092 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 26 Aug 2016 19:23:42 +0300 Subject: Address comments and add requested tests --- .../borrowck/borrowck-union-borrow-nested.rs | 44 +++++++++ .../compile-fail/borrowck/borrowck-union-borrow.rs | 97 +++++++++++++++++++ .../borrowck/borrowck-union-move-assign.rs | 42 +++++++++ .../compile-fail/borrowck/borrowck-union-move.rs | 96 +++++++++++++++++++ .../borrowck/borrowck-union-uninitialized.rs | 30 ++++++ .../compile-fail/privacy/union-field-privacy-1.rs | 30 ++++++ .../compile-fail/privacy/union-field-privacy-2.rs | 28 ++++++ src/test/compile-fail/union-borrow-nested.rs | 44 --------- src/test/compile-fail/union-borrow.rs | 97 ------------------- src/test/compile-fail/union-const-eval.rs | 26 ------ src/test/compile-fail/union-const-pat.rs | 25 ----- src/test/compile-fail/union-derive.rs | 30 ------ src/test/compile-fail/union-empty.rs | 15 --- src/test/compile-fail/union-field-privacy.rs | 21 ----- src/test/compile-fail/union-fields.rs | 34 ------- src/test/compile-fail/union-move-assign.rs | 42 --------- src/test/compile-fail/union-move.rs | 96 ------------------- src/test/compile-fail/union-nonrepresentable.rs | 18 ---- src/test/compile-fail/union-uninitialized.rs | 30 ------ src/test/compile-fail/union-unsafe.rs | 23 ----- src/test/compile-fail/union-unsized.rs | 17 ---- .../compile-fail/union-with-drop-fields-lint.rs | 40 -------- src/test/compile-fail/union/union-const-eval.rs | 26 ++++++ src/test/compile-fail/union/union-const-pat.rs | 25 +++++ src/test/compile-fail/union/union-copy.rs | 26 ++++++ src/test/compile-fail/union/union-derive.rs | 30 ++++++ src/test/compile-fail/union/union-empty.rs | 15 +++ src/test/compile-fail/union/union-feature-gate.rs | 15 +++ src/test/compile-fail/union/union-fields.rs | 35 +++++++ src/test/compile-fail/union/union-generic.rs | 24 +++++ .../compile-fail/union/union-nonrepresentable.rs | 18 ++++ src/test/compile-fail/union/union-repr-c.rs | 29 ++++++ src/test/compile-fail/union/union-suggest-field.rs | 29 ++++++ src/test/compile-fail/union/union-unsafe.rs | 24 +++++ src/test/compile-fail/union/union-unsized.rs | 23 +++++ .../union/union-with-drop-fields-lint.rs | 40 ++++++++ src/test/run-pass/auxiliary/union.rs | 16 ---- src/test/run-pass/union-backcomp.rs | 23 ----- src/test/run-pass/union-basic.rs | 59 ------------ src/test/run-pass/union-const-trans.rs | 27 ------ src/test/run-pass/union-derive.rs | 31 ------ src/test/run-pass/union-drop-assign.rs | 44 --------- src/test/run-pass/union-drop.rs | 65 ------------- src/test/run-pass/union-packed.rs | 74 --------------- src/test/run-pass/union-pat-refutability.rs | 62 ------------ src/test/run-pass/union-transmute.rs | 40 -------- src/test/run-pass/union-xcrate.rs | 21 ----- src/test/run-pass/union/auxiliary/union.rs | 16 ++++ src/test/run-pass/union/union-backcomp.rs | 27 ++++++ src/test/run-pass/union/union-basic.rs | 59 ++++++++++++ src/test/run-pass/union/union-c-interop.rs | 47 ++++++++++ src/test/run-pass/union/union-const-trans.rs | 27 ++++++ src/test/run-pass/union/union-derive.rs | 31 ++++++ src/test/run-pass/union/union-drop-assign.rs | 44 +++++++++ src/test/run-pass/union/union-drop.rs | 65 +++++++++++++ src/test/run-pass/union/union-generic.rs | 43 +++++++++ src/test/run-pass/union/union-inherent-method.rs | 24 +++++ src/test/run-pass/union/union-macro.rs | 33 +++++++ src/test/run-pass/union/union-overwrite.rs | 80 ++++++++++++++++ src/test/run-pass/union/union-packed.rs | 104 +++++++++++++++++++++ src/test/run-pass/union/union-pat-refutability.rs | 62 ++++++++++++ src/test/run-pass/union/union-trait-impl.rs | 27 ++++++ src/test/run-pass/union/union-transmute.rs | 40 ++++++++ .../run-pass/union/union-with-drop-fields-lint.rs | 42 +++++++++ src/test/run-pass/union/union-xcrate.rs | 21 +++++ 65 files changed, 1518 insertions(+), 1020 deletions(-) create mode 100644 src/test/compile-fail/borrowck/borrowck-union-borrow-nested.rs create mode 100644 src/test/compile-fail/borrowck/borrowck-union-borrow.rs create mode 100644 src/test/compile-fail/borrowck/borrowck-union-move-assign.rs create mode 100644 src/test/compile-fail/borrowck/borrowck-union-move.rs create mode 100644 src/test/compile-fail/borrowck/borrowck-union-uninitialized.rs create mode 100644 src/test/compile-fail/privacy/union-field-privacy-1.rs create mode 100644 src/test/compile-fail/privacy/union-field-privacy-2.rs delete mode 100644 src/test/compile-fail/union-borrow-nested.rs delete mode 100644 src/test/compile-fail/union-borrow.rs delete mode 100644 src/test/compile-fail/union-const-eval.rs delete mode 100644 src/test/compile-fail/union-const-pat.rs delete mode 100644 src/test/compile-fail/union-derive.rs delete mode 100644 src/test/compile-fail/union-empty.rs delete mode 100644 src/test/compile-fail/union-field-privacy.rs delete mode 100644 src/test/compile-fail/union-fields.rs delete mode 100644 src/test/compile-fail/union-move-assign.rs delete mode 100644 src/test/compile-fail/union-move.rs delete mode 100644 src/test/compile-fail/union-nonrepresentable.rs delete mode 100644 src/test/compile-fail/union-uninitialized.rs delete mode 100644 src/test/compile-fail/union-unsafe.rs delete mode 100644 src/test/compile-fail/union-unsized.rs delete mode 100644 src/test/compile-fail/union-with-drop-fields-lint.rs create mode 100644 src/test/compile-fail/union/union-const-eval.rs create mode 100644 src/test/compile-fail/union/union-const-pat.rs create mode 100644 src/test/compile-fail/union/union-copy.rs create mode 100644 src/test/compile-fail/union/union-derive.rs create mode 100644 src/test/compile-fail/union/union-empty.rs create mode 100644 src/test/compile-fail/union/union-feature-gate.rs create mode 100644 src/test/compile-fail/union/union-fields.rs create mode 100644 src/test/compile-fail/union/union-generic.rs create mode 100644 src/test/compile-fail/union/union-nonrepresentable.rs create mode 100644 src/test/compile-fail/union/union-repr-c.rs create mode 100644 src/test/compile-fail/union/union-suggest-field.rs create mode 100644 src/test/compile-fail/union/union-unsafe.rs create mode 100644 src/test/compile-fail/union/union-unsized.rs create mode 100644 src/test/compile-fail/union/union-with-drop-fields-lint.rs delete mode 100644 src/test/run-pass/auxiliary/union.rs delete mode 100644 src/test/run-pass/union-backcomp.rs delete mode 100644 src/test/run-pass/union-basic.rs delete mode 100644 src/test/run-pass/union-const-trans.rs delete mode 100644 src/test/run-pass/union-derive.rs delete mode 100644 src/test/run-pass/union-drop-assign.rs delete mode 100644 src/test/run-pass/union-drop.rs delete mode 100644 src/test/run-pass/union-packed.rs delete mode 100644 src/test/run-pass/union-pat-refutability.rs delete mode 100644 src/test/run-pass/union-transmute.rs delete mode 100644 src/test/run-pass/union-xcrate.rs create mode 100644 src/test/run-pass/union/auxiliary/union.rs create mode 100644 src/test/run-pass/union/union-backcomp.rs create mode 100644 src/test/run-pass/union/union-basic.rs create mode 100644 src/test/run-pass/union/union-c-interop.rs create mode 100644 src/test/run-pass/union/union-const-trans.rs create mode 100644 src/test/run-pass/union/union-derive.rs create mode 100644 src/test/run-pass/union/union-drop-assign.rs create mode 100644 src/test/run-pass/union/union-drop.rs create mode 100644 src/test/run-pass/union/union-generic.rs create mode 100644 src/test/run-pass/union/union-inherent-method.rs create mode 100644 src/test/run-pass/union/union-macro.rs create mode 100644 src/test/run-pass/union/union-overwrite.rs create mode 100644 src/test/run-pass/union/union-packed.rs create mode 100644 src/test/run-pass/union/union-pat-refutability.rs create mode 100644 src/test/run-pass/union/union-trait-impl.rs create mode 100644 src/test/run-pass/union/union-transmute.rs create mode 100644 src/test/run-pass/union/union-with-drop-fields-lint.rs create mode 100644 src/test/run-pass/union/union-xcrate.rs (limited to 'src/test') diff --git a/src/test/compile-fail/borrowck/borrowck-union-borrow-nested.rs b/src/test/compile-fail/borrowck/borrowck-union-borrow-nested.rs new file mode 100644 index 00000000000..19975d79b60 --- /dev/null +++ b/src/test/compile-fail/borrowck/borrowck-union-borrow-nested.rs @@ -0,0 +1,44 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// ignore-tidy-linelength + +#![feature(untagged_unions)] + +#[derive(Clone, Copy)] +struct S { + a: u8, + b: u16, +} + +union U { + s: S, + c: u32, +} + +impl Clone for U { + fn clone(&self) -> Self { *self } +} +impl Copy for U {} + +fn main() { + unsafe { + { + let mut u = U { s: S { a: 0, b: 1 } }; + let ra = &mut u.s.a; + let b = u.s.b; // OK + } + { + let mut u = U { s: S { a: 0, b: 1 } }; + let ra = &mut u.s.a; + let b = u.c; //~ ERROR cannot use `u.c` because it was mutably borrowed + } + } +} diff --git a/src/test/compile-fail/borrowck/borrowck-union-borrow.rs b/src/test/compile-fail/borrowck/borrowck-union-borrow.rs new file mode 100644 index 00000000000..e8989a3c2d4 --- /dev/null +++ b/src/test/compile-fail/borrowck/borrowck-union-borrow.rs @@ -0,0 +1,97 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// ignore-tidy-linelength + +#![feature(untagged_unions)] + +union U { + a: u8, + b: u64, +} + +impl Clone for U { + fn clone(&self) -> Self { *self } +} +impl Copy for U {} + +fn main() { + unsafe { + let mut u = U { b: 0 }; + // Imm borrow, same field + { + let ra = &u.a; + let ra2 = &u.a; // OK + } + { + let ra = &u.a; + let a = u.a; // OK + } + { + let ra = &u.a; + let rma = &mut u.a; //~ ERROR cannot borrow `u.a` as mutable because it is also borrowed as immutable + } + { + let ra = &u.a; + u.a = 1; //~ ERROR cannot assign to `u.a` because it is borrowed + } + // Imm borrow, other field + { + let ra = &u.a; + let rb = &u.b; // OK + } + { + let ra = &u.a; + let b = u.b; // OK + } + { + let ra = &u.a; + let rmb = &mut u.b; //~ ERROR cannot borrow `u` (via `u.b`) as mutable because `u` is also borrowed as immutable (via `u.a`) + } + { + let ra = &u.a; + u.b = 1; //~ ERROR cannot assign to `u.b` because it is borrowed + } + // Mut borrow, same field + { + let rma = &mut u.a; + let ra = &u.a; //~ ERROR cannot borrow `u.a` as immutable because it is also borrowed as mutable + } + { + let ra = &mut u.a; + let a = u.a; //~ ERROR cannot use `u.a` because it was mutably borrowed + } + { + let rma = &mut u.a; + let rma2 = &mut u.a; //~ ERROR cannot borrow `u.a` as mutable more than once at a time + } + { + let rma = &mut u.a; + u.a = 1; //~ ERROR cannot assign to `u.a` because it is borrowed + } + // Mut borrow, other field + { + let rma = &mut u.a; + let rb = &u.b; //~ ERROR cannot borrow `u` (via `u.b`) as immutable because `u` is also borrowed as mutable (via `u.a`) + } + { + let ra = &mut u.a; + let b = u.b; //~ ERROR cannot use `u.b` because it was mutably borrowed + } + { + let rma = &mut u.a; + let rmb2 = &mut u.b; //~ ERROR cannot borrow `u` (via `u.b`) as mutable more than once at a time + } + { + let rma = &mut u.a; + u.b = 1; //~ ERROR cannot assign to `u.b` because it is borrowed + } + } +} diff --git a/src/test/compile-fail/borrowck/borrowck-union-move-assign.rs b/src/test/compile-fail/borrowck/borrowck-union-move-assign.rs new file mode 100644 index 00000000000..d4d7bc6b0f7 --- /dev/null +++ b/src/test/compile-fail/borrowck/borrowck-union-move-assign.rs @@ -0,0 +1,42 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +// Non-copy +struct A; +struct B; + +union U { + a: A, + b: B, +} + +fn main() { + unsafe { + { + let mut u = U { a: A }; + let a = u.a; + let a = u.a; //~ ERROR use of moved value: `u.a` + } + { + let mut u = U { a: A }; + let a = u.a; + u.a = A; + let a = u.a; // OK + } + { + let mut u = U { a: A }; + let a = u.a; + u.b = B; + let a = u.a; // OK + } + } +} diff --git a/src/test/compile-fail/borrowck/borrowck-union-move.rs b/src/test/compile-fail/borrowck/borrowck-union-move.rs new file mode 100644 index 00000000000..5320244cf43 --- /dev/null +++ b/src/test/compile-fail/borrowck/borrowck-union-move.rs @@ -0,0 +1,96 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +#[derive(Clone, Copy)] +struct Copy; +struct NonCopy; + +union Unn { + n1: NonCopy, + n2: NonCopy, +} +union Ucc { + c1: Copy, + c2: Copy, +} +union Ucn { + c: Copy, + n: NonCopy, +} + +fn main() { + unsafe { + // 2 NonCopy + { + let mut u = Unn { n1: NonCopy }; + let a = u.n1; + let a = u.n1; //~ ERROR use of moved value: `u.n1` + } + { + let mut u = Unn { n1: NonCopy }; + let a = u.n1; + let a = u; //~ ERROR use of partially moved value: `u` + } + { + let mut u = Unn { n1: NonCopy }; + let a = u.n1; + let a = u.n2; //~ ERROR use of moved value: `u.n2` + } + // 2 Copy + { + let mut u = Ucc { c1: Copy }; + let a = u.c1; + let a = u.c1; // OK + } + { + let mut u = Ucc { c1: Copy }; + let a = u.c1; + let a = u; // OK + } + { + let mut u = Ucc { c1: Copy }; + let a = u.c1; + let a = u.c2; // OK + } + // 1 Copy, 1 NonCopy + { + let mut u = Ucn { c: Copy }; + let a = u.c; + let a = u.c; // OK + } + { + let mut u = Ucn { c: Copy }; + let a = u.n; + let a = u.n; //~ ERROR use of moved value: `u.n` + } + { + let mut u = Ucn { c: Copy }; + let a = u.n; + let a = u.c; //~ ERROR use of moved value: `u.c` + } + { + let mut u = Ucn { c: Copy }; + let a = u.c; + let a = u.n; // OK + } + { + let mut u = Ucn { c: Copy }; + let a = u.c; + let a = u; // OK + } + { + let mut u = Ucn { c: Copy }; + let a = u.n; + let a = u; //~ ERROR use of partially moved value: `u` + } + } +} diff --git a/src/test/compile-fail/borrowck/borrowck-union-uninitialized.rs b/src/test/compile-fail/borrowck/borrowck-union-uninitialized.rs new file mode 100644 index 00000000000..36e062f8464 --- /dev/null +++ b/src/test/compile-fail/borrowck/borrowck-union-uninitialized.rs @@ -0,0 +1,30 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +struct S { + a: u8, +} + +union U { + a: u8, +} + +fn main() { + unsafe { + let mut s: S; + let mut u: U; + s.a = 0; + u.a = 0; + let sa = s.a; //~ ERROR use of possibly uninitialized variable: `s.a` + let ua = u.a; //~ ERROR use of possibly uninitialized variable: `u.a` + } +} diff --git a/src/test/compile-fail/privacy/union-field-privacy-1.rs b/src/test/compile-fail/privacy/union-field-privacy-1.rs new file mode 100644 index 00000000000..4924fabafb0 --- /dev/null +++ b/src/test/compile-fail/privacy/union-field-privacy-1.rs @@ -0,0 +1,30 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(pub_restricted)] +#![feature(untagged_unions)] + +mod m { + pub union U { + pub a: u8, + pub(super) b: u8, + c: u8, + } +} + +fn main() { + let u = m::U { a: 0 }; // OK + let u = m::U { b: 0 }; // OK + let u = m::U { c: 0 }; //~ ERROR field `c` of union `m::U` is private + + let m::U { a } = u; // OK + let m::U { b } = u; // OK + let m::U { c } = u; //~ ERROR field `c` of union `m::U` is private +} diff --git a/src/test/compile-fail/privacy/union-field-privacy-2.rs b/src/test/compile-fail/privacy/union-field-privacy-2.rs new file mode 100644 index 00000000000..7151538f412 --- /dev/null +++ b/src/test/compile-fail/privacy/union-field-privacy-2.rs @@ -0,0 +1,28 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(pub_restricted)] +#![feature(untagged_unions)] + +mod m { + pub union U { + pub a: u8, + pub(super) b: u8, + c: u8, + } +} + +fn main() { + let u = m::U { a: 10 }; + + let a = u.a; // OK + let b = u.b; // OK + let c = u.c; //~ ERROR field `c` of struct `m::U` is private +} diff --git a/src/test/compile-fail/union-borrow-nested.rs b/src/test/compile-fail/union-borrow-nested.rs deleted file mode 100644 index 19975d79b60..00000000000 --- a/src/test/compile-fail/union-borrow-nested.rs +++ /dev/null @@ -1,44 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// ignore-tidy-linelength - -#![feature(untagged_unions)] - -#[derive(Clone, Copy)] -struct S { - a: u8, - b: u16, -} - -union U { - s: S, - c: u32, -} - -impl Clone for U { - fn clone(&self) -> Self { *self } -} -impl Copy for U {} - -fn main() { - unsafe { - { - let mut u = U { s: S { a: 0, b: 1 } }; - let ra = &mut u.s.a; - let b = u.s.b; // OK - } - { - let mut u = U { s: S { a: 0, b: 1 } }; - let ra = &mut u.s.a; - let b = u.c; //~ ERROR cannot use `u.c` because it was mutably borrowed - } - } -} diff --git a/src/test/compile-fail/union-borrow.rs b/src/test/compile-fail/union-borrow.rs deleted file mode 100644 index e8989a3c2d4..00000000000 --- a/src/test/compile-fail/union-borrow.rs +++ /dev/null @@ -1,97 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// ignore-tidy-linelength - -#![feature(untagged_unions)] - -union U { - a: u8, - b: u64, -} - -impl Clone for U { - fn clone(&self) -> Self { *self } -} -impl Copy for U {} - -fn main() { - unsafe { - let mut u = U { b: 0 }; - // Imm borrow, same field - { - let ra = &u.a; - let ra2 = &u.a; // OK - } - { - let ra = &u.a; - let a = u.a; // OK - } - { - let ra = &u.a; - let rma = &mut u.a; //~ ERROR cannot borrow `u.a` as mutable because it is also borrowed as immutable - } - { - let ra = &u.a; - u.a = 1; //~ ERROR cannot assign to `u.a` because it is borrowed - } - // Imm borrow, other field - { - let ra = &u.a; - let rb = &u.b; // OK - } - { - let ra = &u.a; - let b = u.b; // OK - } - { - let ra = &u.a; - let rmb = &mut u.b; //~ ERROR cannot borrow `u` (via `u.b`) as mutable because `u` is also borrowed as immutable (via `u.a`) - } - { - let ra = &u.a; - u.b = 1; //~ ERROR cannot assign to `u.b` because it is borrowed - } - // Mut borrow, same field - { - let rma = &mut u.a; - let ra = &u.a; //~ ERROR cannot borrow `u.a` as immutable because it is also borrowed as mutable - } - { - let ra = &mut u.a; - let a = u.a; //~ ERROR cannot use `u.a` because it was mutably borrowed - } - { - let rma = &mut u.a; - let rma2 = &mut u.a; //~ ERROR cannot borrow `u.a` as mutable more than once at a time - } - { - let rma = &mut u.a; - u.a = 1; //~ ERROR cannot assign to `u.a` because it is borrowed - } - // Mut borrow, other field - { - let rma = &mut u.a; - let rb = &u.b; //~ ERROR cannot borrow `u` (via `u.b`) as immutable because `u` is also borrowed as mutable (via `u.a`) - } - { - let ra = &mut u.a; - let b = u.b; //~ ERROR cannot use `u.b` because it was mutably borrowed - } - { - let rma = &mut u.a; - let rmb2 = &mut u.b; //~ ERROR cannot borrow `u` (via `u.b`) as mutable more than once at a time - } - { - let rma = &mut u.a; - u.b = 1; //~ ERROR cannot assign to `u.b` because it is borrowed - } - } -} diff --git a/src/test/compile-fail/union-const-eval.rs b/src/test/compile-fail/union-const-eval.rs deleted file mode 100644 index b2bf173c59c..00000000000 --- a/src/test/compile-fail/union-const-eval.rs +++ /dev/null @@ -1,26 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -union U { - a: usize, - b: usize, -} - -const C: U = U { a: 10 }; - -fn main() { - unsafe { - let a: [u8; C.a]; // OK - let b: [u8; C.b]; //~ ERROR constant evaluation error - //~^ NOTE nonexistent struct field - } -} diff --git a/src/test/compile-fail/union-const-pat.rs b/src/test/compile-fail/union-const-pat.rs deleted file mode 100644 index 3d168980ed2..00000000000 --- a/src/test/compile-fail/union-const-pat.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -union U { - a: usize, - b: usize, -} - -const C: U = U { a: 10 }; - -fn main() { - match C { - C => {} //~ ERROR cannot use unions in constant patterns - _ => {} - } -} diff --git a/src/test/compile-fail/union-derive.rs b/src/test/compile-fail/union-derive.rs deleted file mode 100644 index 0f78e96f640..00000000000 --- a/src/test/compile-fail/union-derive.rs +++ /dev/null @@ -1,30 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// Most traits cannot be derived for unions. - -#![feature(untagged_unions)] - -#[derive( - Clone, //~ ERROR this trait cannot be derived for unions - PartialEq, //~ ERROR this trait cannot be derived for unions - Eq, //~ ERROR this trait cannot be derived for unions - PartialOrd, //~ ERROR this trait cannot be derived for unions - Ord, //~ ERROR this trait cannot be derived for unions - Hash, //~ ERROR this trait cannot be derived for unions - Default, //~ ERROR this trait cannot be derived for unions - Debug, //~ ERROR this trait cannot be derived for unions -)] -union U { - a: u8, - b: u16, -} - -fn main() {} diff --git a/src/test/compile-fail/union-empty.rs b/src/test/compile-fail/union-empty.rs deleted file mode 100644 index ce5bbf60fee..00000000000 --- a/src/test/compile-fail/union-empty.rs +++ /dev/null @@ -1,15 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -union U {} //~ ERROR unions cannot have zero fields - -fn main() {} diff --git a/src/test/compile-fail/union-field-privacy.rs b/src/test/compile-fail/union-field-privacy.rs deleted file mode 100644 index d1f2bbbc3d0..00000000000 --- a/src/test/compile-fail/union-field-privacy.rs +++ /dev/null @@ -1,21 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -mod m { - pub union U { - a: u8 - } -} - -fn main() { - let u = m::U { a: 0 }; //~ ERROR field `a` of union `m::U` is private -} diff --git a/src/test/compile-fail/union-fields.rs b/src/test/compile-fail/union-fields.rs deleted file mode 100644 index 2bd1b8a7b32..00000000000 --- a/src/test/compile-fail/union-fields.rs +++ /dev/null @@ -1,34 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -union U { - a: u8, - b: u16, -} - -fn main() { - let u = U {}; //~ ERROR union expressions should have exactly one field - let u = U { a: 0 }; // OK - let u = U { a: 0, b: 1 }; //~ ERROR union expressions should have exactly one field - let u = U { a: 0, b: 1, c: 2 }; //~ ERROR union expressions should have exactly one field - //~^ ERROR union `U` has no field named `c` - let u = U { ..u }; //~ ERROR union expressions should have exactly one field - //~^ ERROR functional record update syntax requires a struct - - let U {} = u; //~ ERROR union patterns without `..` should have at least one field - let U { a } = u; // OK - let U { a, b } = u; //~ ERROR union patterns can have at most one field - let U { a, b, c } = u; //~ ERROR union patterns can have at most one field - //~^ ERROR union `U` does not have a field named `c` - let U { .. } = u; // OK - let U { a, .. } = u; // OK -} diff --git a/src/test/compile-fail/union-move-assign.rs b/src/test/compile-fail/union-move-assign.rs deleted file mode 100644 index d4d7bc6b0f7..00000000000 --- a/src/test/compile-fail/union-move-assign.rs +++ /dev/null @@ -1,42 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -// Non-copy -struct A; -struct B; - -union U { - a: A, - b: B, -} - -fn main() { - unsafe { - { - let mut u = U { a: A }; - let a = u.a; - let a = u.a; //~ ERROR use of moved value: `u.a` - } - { - let mut u = U { a: A }; - let a = u.a; - u.a = A; - let a = u.a; // OK - } - { - let mut u = U { a: A }; - let a = u.a; - u.b = B; - let a = u.a; // OK - } - } -} diff --git a/src/test/compile-fail/union-move.rs b/src/test/compile-fail/union-move.rs deleted file mode 100644 index 5320244cf43..00000000000 --- a/src/test/compile-fail/union-move.rs +++ /dev/null @@ -1,96 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -#[derive(Clone, Copy)] -struct Copy; -struct NonCopy; - -union Unn { - n1: NonCopy, - n2: NonCopy, -} -union Ucc { - c1: Copy, - c2: Copy, -} -union Ucn { - c: Copy, - n: NonCopy, -} - -fn main() { - unsafe { - // 2 NonCopy - { - let mut u = Unn { n1: NonCopy }; - let a = u.n1; - let a = u.n1; //~ ERROR use of moved value: `u.n1` - } - { - let mut u = Unn { n1: NonCopy }; - let a = u.n1; - let a = u; //~ ERROR use of partially moved value: `u` - } - { - let mut u = Unn { n1: NonCopy }; - let a = u.n1; - let a = u.n2; //~ ERROR use of moved value: `u.n2` - } - // 2 Copy - { - let mut u = Ucc { c1: Copy }; - let a = u.c1; - let a = u.c1; // OK - } - { - let mut u = Ucc { c1: Copy }; - let a = u.c1; - let a = u; // OK - } - { - let mut u = Ucc { c1: Copy }; - let a = u.c1; - let a = u.c2; // OK - } - // 1 Copy, 1 NonCopy - { - let mut u = Ucn { c: Copy }; - let a = u.c; - let a = u.c; // OK - } - { - let mut u = Ucn { c: Copy }; - let a = u.n; - let a = u.n; //~ ERROR use of moved value: `u.n` - } - { - let mut u = Ucn { c: Copy }; - let a = u.n; - let a = u.c; //~ ERROR use of moved value: `u.c` - } - { - let mut u = Ucn { c: Copy }; - let a = u.c; - let a = u.n; // OK - } - { - let mut u = Ucn { c: Copy }; - let a = u.c; - let a = u; // OK - } - { - let mut u = Ucn { c: Copy }; - let a = u.n; - let a = u; //~ ERROR use of partially moved value: `u` - } - } -} diff --git a/src/test/compile-fail/union-nonrepresentable.rs b/src/test/compile-fail/union-nonrepresentable.rs deleted file mode 100644 index cb4683c2a0e..00000000000 --- a/src/test/compile-fail/union-nonrepresentable.rs +++ /dev/null @@ -1,18 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -union U { //~ ERROR recursive type `U` has infinite size - a: u8, - b: U, -} - -fn main() {} diff --git a/src/test/compile-fail/union-uninitialized.rs b/src/test/compile-fail/union-uninitialized.rs deleted file mode 100644 index 36e062f8464..00000000000 --- a/src/test/compile-fail/union-uninitialized.rs +++ /dev/null @@ -1,30 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -struct S { - a: u8, -} - -union U { - a: u8, -} - -fn main() { - unsafe { - let mut s: S; - let mut u: U; - s.a = 0; - u.a = 0; - let sa = s.a; //~ ERROR use of possibly uninitialized variable: `s.a` - let ua = u.a; //~ ERROR use of possibly uninitialized variable: `u.a` - } -} diff --git a/src/test/compile-fail/union-unsafe.rs b/src/test/compile-fail/union-unsafe.rs deleted file mode 100644 index 762ac5d8751..00000000000 --- a/src/test/compile-fail/union-unsafe.rs +++ /dev/null @@ -1,23 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -union U { - a: u8 -} - -fn main() { - let u = U { a: 10 }; // OK - let a = u.a; //~ ERROR access to union field requires unsafe function or block - let U { a } = u; //~ ERROR matching on union field requires unsafe function or block - if let U { a: 11 } = u {} //~ ERROR matching on union field requires unsafe function or block - let U { .. } = u; // OK -} diff --git a/src/test/compile-fail/union-unsized.rs b/src/test/compile-fail/union-unsized.rs deleted file mode 100644 index 381122406d7..00000000000 --- a/src/test/compile-fail/union-unsized.rs +++ /dev/null @@ -1,17 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -union U { - a: str, //~ ERROR the trait bound `str: std::marker::Sized` is not satisfied -} - -fn main() {} diff --git a/src/test/compile-fail/union-with-drop-fields-lint.rs b/src/test/compile-fail/union-with-drop-fields-lint.rs deleted file mode 100644 index 87a72efbe08..00000000000 --- a/src/test/compile-fail/union-with-drop-fields-lint.rs +++ /dev/null @@ -1,40 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] -#![allow(dead_code)] -#![deny(unions_with_drop_fields)] - -union U { - a: u8, // OK -} - -union W { - a: String, //~ ERROR union contains a field with possibly non-trivial drop code - b: String, // OK, only one field is reported -} - -struct S(String); - -// `S` doesn't implement `Drop` trait, but still has non-trivial destructor -union Y { - a: S, //~ ERROR union contains a field with possibly non-trivial drop code -} - -// We don't know if `T` is trivially-destructable or not until trans -union J { - a: T, //~ ERROR union contains a field with possibly non-trivial drop code -} - -union H { - a: T, // OK, `T` is `Copy`, no destructor -} - -fn main() {} diff --git a/src/test/compile-fail/union/union-const-eval.rs b/src/test/compile-fail/union/union-const-eval.rs new file mode 100644 index 00000000000..b2bf173c59c --- /dev/null +++ b/src/test/compile-fail/union/union-const-eval.rs @@ -0,0 +1,26 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +union U { + a: usize, + b: usize, +} + +const C: U = U { a: 10 }; + +fn main() { + unsafe { + let a: [u8; C.a]; // OK + let b: [u8; C.b]; //~ ERROR constant evaluation error + //~^ NOTE nonexistent struct field + } +} diff --git a/src/test/compile-fail/union/union-const-pat.rs b/src/test/compile-fail/union/union-const-pat.rs new file mode 100644 index 00000000000..3d168980ed2 --- /dev/null +++ b/src/test/compile-fail/union/union-const-pat.rs @@ -0,0 +1,25 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +union U { + a: usize, + b: usize, +} + +const C: U = U { a: 10 }; + +fn main() { + match C { + C => {} //~ ERROR cannot use unions in constant patterns + _ => {} + } +} diff --git a/src/test/compile-fail/union/union-copy.rs b/src/test/compile-fail/union/union-copy.rs new file mode 100644 index 00000000000..6e08ae0074d --- /dev/null +++ b/src/test/compile-fail/union/union-copy.rs @@ -0,0 +1,26 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +union U { + a: u8 +} + +union W { + a: String +} + +impl Clone for U { fn clone(&self) { panic!(); } } +impl Clone for W { fn clone(&self) { panic!(); } } +impl Copy for U {} // OK +impl Copy for W {} //~ ERROR the trait `Copy` may not be implemented for this type + +fn main() {} diff --git a/src/test/compile-fail/union/union-derive.rs b/src/test/compile-fail/union/union-derive.rs new file mode 100644 index 00000000000..0f78e96f640 --- /dev/null +++ b/src/test/compile-fail/union/union-derive.rs @@ -0,0 +1,30 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Most traits cannot be derived for unions. + +#![feature(untagged_unions)] + +#[derive( + Clone, //~ ERROR this trait cannot be derived for unions + PartialEq, //~ ERROR this trait cannot be derived for unions + Eq, //~ ERROR this trait cannot be derived for unions + PartialOrd, //~ ERROR this trait cannot be derived for unions + Ord, //~ ERROR this trait cannot be derived for unions + Hash, //~ ERROR this trait cannot be derived for unions + Default, //~ ERROR this trait cannot be derived for unions + Debug, //~ ERROR this trait cannot be derived for unions +)] +union U { + a: u8, + b: u16, +} + +fn main() {} diff --git a/src/test/compile-fail/union/union-empty.rs b/src/test/compile-fail/union/union-empty.rs new file mode 100644 index 00000000000..ce5bbf60fee --- /dev/null +++ b/src/test/compile-fail/union/union-empty.rs @@ -0,0 +1,15 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +union U {} //~ ERROR unions cannot have zero fields + +fn main() {} diff --git a/src/test/compile-fail/union/union-feature-gate.rs b/src/test/compile-fail/union/union-feature-gate.rs new file mode 100644 index 00000000000..abfc4d90921 --- /dev/null +++ b/src/test/compile-fail/union/union-feature-gate.rs @@ -0,0 +1,15 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +union U { //~ ERROR unions are unstable and possibly buggy + a: u8, +} + +fn main() {} diff --git a/src/test/compile-fail/union/union-fields.rs b/src/test/compile-fail/union/union-fields.rs new file mode 100644 index 00000000000..a1721dda7de --- /dev/null +++ b/src/test/compile-fail/union/union-fields.rs @@ -0,0 +1,35 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +union U { + a: u8, + b: u16, +} + +fn main() { + let u = U {}; //~ ERROR union expressions should have exactly one field + let u = U { a: 0 }; // OK + let u = U { a: 0, b: 1 }; //~ ERROR union expressions should have exactly one field + let u = U { a: 0, b: 1, c: 2 }; //~ ERROR union expressions should have exactly one field + //~^ ERROR union `U` has no field named `c` + let u = U { ..u }; //~ ERROR union expressions should have exactly one field + //~^ ERROR functional record update syntax requires a struct + + let U {} = u; //~ ERROR union patterns should have exactly one field + let U { a } = u; // OK + let U { a, b } = u; //~ ERROR union patterns should have exactly one field + let U { a, b, c } = u; //~ ERROR union patterns should have exactly one field + //~^ ERROR union `U` does not have a field named `c` + let U { .. } = u; //~ ERROR union patterns should have exactly one field + //~^ ERROR `..` cannot be used in union patterns + let U { a, .. } = u; //~ ERROR `..` cannot be used in union patterns +} diff --git a/src/test/compile-fail/union/union-generic.rs b/src/test/compile-fail/union/union-generic.rs new file mode 100644 index 00000000000..e6586b0fb7f --- /dev/null +++ b/src/test/compile-fail/union/union-generic.rs @@ -0,0 +1,24 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +use std::rc::Rc; + +union U { + a: T +} + +fn main() { + let u = U { a: Rc::new(0u32) }; + //~^ ERROR the trait bound `std::rc::Rc: std::marker::Copy` is not satisfied + let u = U::> { a: Default::default() }; + //~^ ERROR the trait bound `std::rc::Rc: std::marker::Copy` is not satisfied +} diff --git a/src/test/compile-fail/union/union-nonrepresentable.rs b/src/test/compile-fail/union/union-nonrepresentable.rs new file mode 100644 index 00000000000..cb4683c2a0e --- /dev/null +++ b/src/test/compile-fail/union/union-nonrepresentable.rs @@ -0,0 +1,18 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +union U { //~ ERROR recursive type `U` has infinite size + a: u8, + b: U, +} + +fn main() {} diff --git a/src/test/compile-fail/union/union-repr-c.rs b/src/test/compile-fail/union/union-repr-c.rs new file mode 100644 index 00000000000..d7dfb126c93 --- /dev/null +++ b/src/test/compile-fail/union/union-repr-c.rs @@ -0,0 +1,29 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] +#![allow(unused)] +#![deny(improper_ctypes)] + +#[repr(C)] +union U { + a: u8, +} + +union W { + a: u8, +} + +extern "C" { + static FOREIGN1: U; // OK + static FOREIGN2: W; //~ ERROR found union without foreign-function-safe representation +} + +fn main() {} diff --git a/src/test/compile-fail/union/union-suggest-field.rs b/src/test/compile-fail/union/union-suggest-field.rs new file mode 100644 index 00000000000..b05e9b6e273 --- /dev/null +++ b/src/test/compile-fail/union/union-suggest-field.rs @@ -0,0 +1,29 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +union U { + principal: u8, +} + +impl U { + fn calculate(&self) {} +} + +fn main() { + let u = U { principle: 0 }; //~ ERROR union `U` has no field named `principle` + //~^ HELP did you mean `principal`? + let w = u.principial; //~ ERROR attempted access of field `principial` on type `U` + //~^ HELP did you mean `principal`? + + let y = u.calculate; //~ ERROR attempted to take value of method `calculate` on type `U` + //~^ HELP maybe a `()` to call it is missing? +} diff --git a/src/test/compile-fail/union/union-unsafe.rs b/src/test/compile-fail/union/union-unsafe.rs new file mode 100644 index 00000000000..97e1ec2cba8 --- /dev/null +++ b/src/test/compile-fail/union/union-unsafe.rs @@ -0,0 +1,24 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +union U { + a: u8 +} + +fn main() { + let mut u = U { a: 10 }; // OK + let a = u.a; //~ ERROR access to union field requires unsafe function or block + u.a = 11; //~ ERROR access to union field requires unsafe function or block + let U { a } = u; //~ ERROR matching on union field requires unsafe function or block + if let U { a: 12 } = u {} //~ ERROR matching on union field requires unsafe function or block + // let U { .. } = u; // OK +} diff --git a/src/test/compile-fail/union/union-unsized.rs b/src/test/compile-fail/union/union-unsized.rs new file mode 100644 index 00000000000..a238eaf0525 --- /dev/null +++ b/src/test/compile-fail/union/union-unsized.rs @@ -0,0 +1,23 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +union U { + a: str, //~ ERROR the trait bound `str: std::marker::Sized` is not satisfied + b: u8, +} + +union W { + a: u8, + b: str, //~ ERROR the trait bound `str: std::marker::Sized` is not satisfied +} + +fn main() {} diff --git a/src/test/compile-fail/union/union-with-drop-fields-lint.rs b/src/test/compile-fail/union/union-with-drop-fields-lint.rs new file mode 100644 index 00000000000..87a72efbe08 --- /dev/null +++ b/src/test/compile-fail/union/union-with-drop-fields-lint.rs @@ -0,0 +1,40 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] +#![allow(dead_code)] +#![deny(unions_with_drop_fields)] + +union U { + a: u8, // OK +} + +union W { + a: String, //~ ERROR union contains a field with possibly non-trivial drop code + b: String, // OK, only one field is reported +} + +struct S(String); + +// `S` doesn't implement `Drop` trait, but still has non-trivial destructor +union Y { + a: S, //~ ERROR union contains a field with possibly non-trivial drop code +} + +// We don't know if `T` is trivially-destructable or not until trans +union J { + a: T, //~ ERROR union contains a field with possibly non-trivial drop code +} + +union H { + a: T, // OK, `T` is `Copy`, no destructor +} + +fn main() {} diff --git a/src/test/run-pass/auxiliary/union.rs b/src/test/run-pass/auxiliary/union.rs deleted file mode 100644 index dc0ca7c81c0..00000000000 --- a/src/test/run-pass/auxiliary/union.rs +++ /dev/null @@ -1,16 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -pub union U { - pub a: u8, - b: u16, -} diff --git a/src/test/run-pass/union-backcomp.rs b/src/test/run-pass/union-backcomp.rs deleted file mode 100644 index c1210dd6212..00000000000 --- a/src/test/run-pass/union-backcomp.rs +++ /dev/null @@ -1,23 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -fn main() { - let union = 10; - - union; - - union as u8; - - union U { - a: u8, - } -} diff --git a/src/test/run-pass/union-basic.rs b/src/test/run-pass/union-basic.rs deleted file mode 100644 index 1651aa901b9..00000000000 --- a/src/test/run-pass/union-basic.rs +++ /dev/null @@ -1,59 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -use std::mem::{size_of, align_of, zeroed}; - -union U { - a: u8, -} - -union U64 { - a: u64, -} - -union W { - a: u8, - b: u64, -} - -#[repr(C)] -union Y { - f1: u16, - f2: [u8; 4], -} - -fn main() { - assert_eq!(size_of::(), 1); - assert_eq!(size_of::(), 8); - assert_eq!(size_of::(), 8); - assert_eq!(align_of::(), 1); - assert_eq!(align_of::(), align_of::()); - assert_eq!(align_of::(), align_of::()); - assert_eq!(size_of::(), 4); - assert_eq!(align_of::(), 2); - - let u = U { a: 10 }; - unsafe { - assert_eq!(u.a, 10); - let U { a } = u; - assert_eq!(a, 10); - } - - let mut w = W { 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); - } -} diff --git a/src/test/run-pass/union-const-trans.rs b/src/test/run-pass/union-const-trans.rs deleted file mode 100644 index bdae1a0eaf8..00000000000 --- a/src/test/run-pass/union-const-trans.rs +++ /dev/null @@ -1,27 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -union U { - a: u64, - b: u64, -} - -const C: U = U { b: 10 }; - -fn main() { - unsafe { - let a = C.a; - let b = C.b; - assert_eq!(a, 10); - assert_eq!(b, 10); - } -} diff --git a/src/test/run-pass/union-derive.rs b/src/test/run-pass/union-derive.rs deleted file mode 100644 index b71c23990a4..00000000000 --- a/src/test/run-pass/union-derive.rs +++ /dev/null @@ -1,31 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// Some traits can be derived for unions. - -#![feature(untagged_unions)] - -#[derive( - Copy, -)] -union U { - a: u8, - b: u16, -} - -impl Clone for U { - fn clone(&self) -> Self { *self } -} - -fn main() { - let u = U { b: 0 }; - let u1 = u; - let u2 = u.clone(); -} diff --git a/src/test/run-pass/union-drop-assign.rs b/src/test/run-pass/union-drop-assign.rs deleted file mode 100644 index 0da68e43f32..00000000000 --- a/src/test/run-pass/union-drop-assign.rs +++ /dev/null @@ -1,44 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// Drop works for union itself. - -#![feature(untagged_unions)] - -struct S; - -union U { - a: S -} - -impl Drop for S { - fn drop(&mut self) { - unsafe { CHECK += 10; } - } -} - -impl Drop for U { - fn drop(&mut self) { - unsafe { CHECK += 1; } - } -} - -static mut CHECK: u8 = 0; - -fn main() { - unsafe { - let mut u = U { a: S }; - assert_eq!(CHECK, 0); - u = U { a: S }; - assert_eq!(CHECK, 1); // union itself is assigned, union is dropped, field is not dropped - u.a = S; - assert_eq!(CHECK, 11); // union field is assigned, field is dropped - } -} diff --git a/src/test/run-pass/union-drop.rs b/src/test/run-pass/union-drop.rs deleted file mode 100644 index 2ca68dc3b6e..00000000000 --- a/src/test/run-pass/union-drop.rs +++ /dev/null @@ -1,65 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// Drop works for union itself. - -#![feature(untagged_unions)] - -struct S; - -union U { - a: u8 -} - -union W { - a: S, -} - -union Y { - a: S, -} - -impl Drop for S { - fn drop(&mut self) { - unsafe { CHECK += 10; } - } -} - -impl Drop for U { - fn drop(&mut self) { - unsafe { CHECK += 1; } - } -} - -impl Drop for W { - fn drop(&mut self) { - unsafe { CHECK += 1; } - } -} - -static mut CHECK: u8 = 0; - -fn main() { - unsafe { - assert_eq!(CHECK, 0); - { - let u = U { a: 1 }; - } - assert_eq!(CHECK, 1); // 1, dtor of U is called - { - let w = W { a: S }; - } - assert_eq!(CHECK, 2); // 2, not 11, dtor of S is not called - { - let y = Y { a: S }; - } - assert_eq!(CHECK, 2); // 2, not 12, dtor of S is not called - } -} diff --git a/src/test/run-pass/union-packed.rs b/src/test/run-pass/union-packed.rs deleted file mode 100644 index b1650ae3a7c..00000000000 --- a/src/test/run-pass/union-packed.rs +++ /dev/null @@ -1,74 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -use std::mem::{size_of, size_of_val, align_of, align_of_val}; - -struct S { - a: u16, - b: [u8; 3], -} - -#[repr(packed)] -struct Sp { - a: u16, - b: [u8; 3], -} - -union U { - a: u16, - b: [u8; 3], -} - -#[repr(packed)] -union Up { - a: u16, - b: [u8; 3], -} - -const CS: S = S { a: 0, b: [0, 0, 0] }; -const CSP: Sp = Sp { a: 0, b: [0, 0, 0] }; -const CU: U = U { b: [0, 0, 0] }; -const CUP: Up = Up { b: [0, 0, 0] }; - -fn main() { - let s = S { a: 0, b: [0, 0, 0] }; - assert_eq!(size_of::(), 6); - assert_eq!(size_of_val(&s), 6); - assert_eq!(size_of_val(&CS), 6); - assert_eq!(align_of::(), 2); - assert_eq!(align_of_val(&s), 2); - assert_eq!(align_of_val(&CS), 2); - - let sp = Sp { a: 0, b: [0, 0, 0] }; - assert_eq!(size_of::(), 5); - assert_eq!(size_of_val(&sp), 5); - assert_eq!(size_of_val(&CSP), 5); - assert_eq!(align_of::(), 1); - assert_eq!(align_of_val(&sp), 1); - assert_eq!(align_of_val(&CSP), 1); - - let u = U { b: [0, 0, 0] }; - assert_eq!(size_of::(), 4); - assert_eq!(size_of_val(&u), 4); - assert_eq!(size_of_val(&CU), 4); - assert_eq!(align_of::(), 2); - assert_eq!(align_of_val(&u), 2); - assert_eq!(align_of_val(&CU), 2); - - let up = Up { b: [0, 0, 0] }; - assert_eq!(size_of::(), 3); - assert_eq!(size_of_val(&up), 3); - assert_eq!(size_of_val(&CUP), 3); - assert_eq!(align_of::(), 1); - assert_eq!(align_of_val(&up), 1); - assert_eq!(align_of_val(&CUP), 1); -} diff --git a/src/test/run-pass/union-pat-refutability.rs b/src/test/run-pass/union-pat-refutability.rs deleted file mode 100644 index e6144f35f1d..00000000000 --- a/src/test/run-pass/union-pat-refutability.rs +++ /dev/null @@ -1,62 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(untagged_unions)] - -#[repr(u32)] -enum Tag { I, F } - -#[repr(C)] -union U { - i: i32, - f: f32, -} - -#[repr(C)] -struct Value { - tag: Tag, - u: U, -} - -fn is_zero(v: Value) -> bool { - unsafe { - match v { - Value { tag: Tag::I, u: U { i: 0 } } => true, - Value { tag: Tag::F, u: U { f: 0.0 } } => true, - _ => false, - } - } -} - -union W { - a: u8, - b: u8, -} - -fn refut(w: W) { - unsafe { - match w { - W { a: 10 } => { - panic!(); - } - W { b } => { - assert_eq!(b, 11); - } - } - } -} - -fn main() { - let v = Value { tag: Tag::I, u: U { i: 1 } }; - assert_eq!(is_zero(v), false); - - let w = W { a: 11 }; - refut(w); -} diff --git a/src/test/run-pass/union-transmute.rs b/src/test/run-pass/union-transmute.rs deleted file mode 100644 index 4eb66268ab8..00000000000 --- a/src/test/run-pass/union-transmute.rs +++ /dev/null @@ -1,40 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![feature(core_float)] -#![feature(float_extras)] -#![feature(untagged_unions)] - -extern crate core; -use core::num::Float; - -union U { - a: (u8, u8), - b: u16, -} - -union W { - a: u32, - b: f32, -} - -fn main() { - unsafe { - let mut u = U { a: (1, 1) }; - assert_eq!(u.b, (1 << 8) + 1); - u.b = (2 << 8) + 2; - assert_eq!(u.a, (2, 2)); - - let mut w = W { a: 0b0_11111111_00000000000000000000000 }; - assert_eq!(w.b, f32::infinity()); - w.b = f32::neg_infinity(); - assert_eq!(w.a, 0b1_11111111_00000000000000000000000); - } -} diff --git a/src/test/run-pass/union-xcrate.rs b/src/test/run-pass/union-xcrate.rs deleted file mode 100644 index 2a76c96ef25..00000000000 --- a/src/test/run-pass/union-xcrate.rs +++ /dev/null @@ -1,21 +0,0 @@ -// 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 or the MIT license -// , 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; - -type A = union::U; - -fn main() { - assert_eq!(std::mem::size_of::(), 8); -} diff --git a/src/test/run-pass/union/auxiliary/union.rs b/src/test/run-pass/union/auxiliary/union.rs new file mode 100644 index 00000000000..dc0ca7c81c0 --- /dev/null +++ b/src/test/run-pass/union/auxiliary/union.rs @@ -0,0 +1,16 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +pub union U { + pub a: u8, + b: u16, +} diff --git a/src/test/run-pass/union/union-backcomp.rs b/src/test/run-pass/union/union-backcomp.rs new file mode 100644 index 00000000000..9394b618ddf --- /dev/null +++ b/src/test/run-pass/union/union-backcomp.rs @@ -0,0 +1,27 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +fn union() {} + +fn main() { + union(); + + let union = 10; + + union; + + union as u8; + + union U { + a: u8, + } +} 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..1651aa901b9 --- /dev/null +++ b/src/test/run-pass/union/union-basic.rs @@ -0,0 +1,59 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +use std::mem::{size_of, align_of, zeroed}; + +union U { + a: u8, +} + +union U64 { + a: u64, +} + +union W { + a: u8, + b: u64, +} + +#[repr(C)] +union Y { + f1: u16, + f2: [u8; 4], +} + +fn main() { + assert_eq!(size_of::(), 1); + assert_eq!(size_of::(), 8); + assert_eq!(size_of::(), 8); + assert_eq!(align_of::(), 1); + assert_eq!(align_of::(), align_of::()); + assert_eq!(align_of::(), align_of::()); + assert_eq!(size_of::(), 4); + assert_eq!(align_of::(), 2); + + let u = U { a: 10 }; + unsafe { + assert_eq!(u.a, 10); + let U { a } = u; + assert_eq!(a, 10); + } + + let mut w = W { 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); + } +} diff --git a/src/test/run-pass/union/union-c-interop.rs b/src/test/run-pass/union/union-c-interop.rs new file mode 100644 index 00000000000..a9f97620ebd --- /dev/null +++ b/src/test/run-pass/union/union-c-interop.rs @@ -0,0 +1,47 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +#[derive(Copy)] +#[repr(C)] +struct LARGE_INTEGER_U { + LowPart: u32, + HighPart: u32, +} + +#[derive(Copy)] +#[repr(C)] +union LARGE_INTEGER { + __unnamed__: LARGE_INTEGER_U, + u: LARGE_INTEGER_U, + QuadPart: u64, +} + +impl Clone for LARGE_INTEGER_U { fn clone(&self) -> Self { *self } } +impl Clone for LARGE_INTEGER { fn clone(&self) -> Self { *self } } + +#[link(name = "rust_test_helpers")] +extern "C" { + fn increment_all_parts(_: LARGE_INTEGER) -> LARGE_INTEGER; +} + +fn main() { + unsafe { + let mut li = LARGE_INTEGER { QuadPart: 0 }; + let li_c = increment_all_parts(li); + li.__unnamed__.LowPart += 1; + li.__unnamed__.HighPart += 1; + li.u.LowPart += 1; + li.u.HighPart += 1; + li.QuadPart += 1; + assert_eq!(li.QuadPart, li_c.QuadPart); + } +} diff --git a/src/test/run-pass/union/union-const-trans.rs b/src/test/run-pass/union/union-const-trans.rs new file mode 100644 index 00000000000..bdae1a0eaf8 --- /dev/null +++ b/src/test/run-pass/union/union-const-trans.rs @@ -0,0 +1,27 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +union U { + a: u64, + b: u64, +} + +const C: U = U { b: 10 }; + +fn main() { + unsafe { + let a = C.a; + let b = C.b; + assert_eq!(a, 10); + assert_eq!(b, 10); + } +} diff --git a/src/test/run-pass/union/union-derive.rs b/src/test/run-pass/union/union-derive.rs new file mode 100644 index 00000000000..b71c23990a4 --- /dev/null +++ b/src/test/run-pass/union/union-derive.rs @@ -0,0 +1,31 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Some traits can be derived for unions. + +#![feature(untagged_unions)] + +#[derive( + Copy, +)] +union U { + a: u8, + b: u16, +} + +impl Clone for U { + fn clone(&self) -> Self { *self } +} + +fn main() { + let u = U { b: 0 }; + let u1 = u; + let u2 = u.clone(); +} diff --git a/src/test/run-pass/union/union-drop-assign.rs b/src/test/run-pass/union/union-drop-assign.rs new file mode 100644 index 00000000000..0da68e43f32 --- /dev/null +++ b/src/test/run-pass/union/union-drop-assign.rs @@ -0,0 +1,44 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Drop works for union itself. + +#![feature(untagged_unions)] + +struct S; + +union U { + a: S +} + +impl Drop for S { + fn drop(&mut self) { + unsafe { CHECK += 10; } + } +} + +impl Drop for U { + fn drop(&mut self) { + unsafe { CHECK += 1; } + } +} + +static mut CHECK: u8 = 0; + +fn main() { + unsafe { + let mut u = U { a: S }; + assert_eq!(CHECK, 0); + u = U { a: S }; + assert_eq!(CHECK, 1); // union itself is assigned, union is dropped, field is not dropped + u.a = S; + assert_eq!(CHECK, 11); // union field is assigned, field is dropped + } +} diff --git a/src/test/run-pass/union/union-drop.rs b/src/test/run-pass/union/union-drop.rs new file mode 100644 index 00000000000..2ca68dc3b6e --- /dev/null +++ b/src/test/run-pass/union/union-drop.rs @@ -0,0 +1,65 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Drop works for union itself. + +#![feature(untagged_unions)] + +struct S; + +union U { + a: u8 +} + +union W { + a: S, +} + +union Y { + a: S, +} + +impl Drop for S { + fn drop(&mut self) { + unsafe { CHECK += 10; } + } +} + +impl Drop for U { + fn drop(&mut self) { + unsafe { CHECK += 1; } + } +} + +impl Drop for W { + fn drop(&mut self) { + unsafe { CHECK += 1; } + } +} + +static mut CHECK: u8 = 0; + +fn main() { + unsafe { + assert_eq!(CHECK, 0); + { + let u = U { a: 1 }; + } + assert_eq!(CHECK, 1); // 1, dtor of U is called + { + let w = W { a: S }; + } + assert_eq!(CHECK, 2); // 2, not 11, dtor of S is not called + { + let y = Y { a: S }; + } + assert_eq!(CHECK, 2); // 2, not 12, dtor of S is not called + } +} diff --git a/src/test/run-pass/union/union-generic.rs b/src/test/run-pass/union/union-generic.rs new file mode 100644 index 00000000000..9293805edbf --- /dev/null +++ b/src/test/run-pass/union/union-generic.rs @@ -0,0 +1,43 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +union MaybeItem { + elem: T::Item, + none: (), +} + +union U { + a: A, + b: B, +} + +unsafe fn union_transmute(a: A) -> B { + U { a: a }.b +} + +fn main() { + unsafe { + let u = U::> { a: String::from("abcd") }; + + assert_eq!(u.b.len(), 4); + assert_eq!(u.b[0], b'a'); + + let b = union_transmute::<(u8, u8), u16>((1, 1)); + assert_eq!(b, (1 << 8) + 1); + + let v: Vec = vec![1, 2, 3]; + let mut i = v.iter(); + i.next(); + let mi = MaybeItem::> { elem: i.next().unwrap() }; + assert_eq!(*mi.elem, 2); + } +} diff --git a/src/test/run-pass/union/union-inherent-method.rs b/src/test/run-pass/union/union-inherent-method.rs new file mode 100644 index 00000000000..adea27bd254 --- /dev/null +++ b/src/test/run-pass/union/union-inherent-method.rs @@ -0,0 +1,24 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +union U { + a: u8, +} + +impl U { + fn method(&self) -> u8 { unsafe { self.a } } +} + +fn main() { + let u = U { a: 10 }; + assert_eq!(u.method(), 10); +} diff --git a/src/test/run-pass/union/union-macro.rs b/src/test/run-pass/union/union-macro.rs new file mode 100644 index 00000000000..a23fbc3be9e --- /dev/null +++ b/src/test/run-pass/union/union-macro.rs @@ -0,0 +1,33 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +macro_rules! duplicate { + ($i: item) => { + mod m1 { + $i + } + mod m2 { + $i + } + } +} + +duplicate! { + pub union U { + pub a: u8 + } +} + +fn main() { + let u1 = m1::U { a: 0 }; + let u2 = m2::U { a: 0 }; +} diff --git a/src/test/run-pass/union/union-overwrite.rs b/src/test/run-pass/union/union-overwrite.rs new file mode 100644 index 00000000000..9389a6237bc --- /dev/null +++ b/src/test/run-pass/union/union-overwrite.rs @@ -0,0 +1,80 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +#[repr(C)] +struct Pair(T, U); +#[repr(C)] +struct Triple(T, T, T); + +#[repr(C)] +union U { + a: Pair, + b: B, +} + +#[repr(C)] +union W { + a: A, + b: B, +} + +#[cfg(target_endian = "little")] +unsafe fn check() { + let mut u = U:: { b: 0xDE_DE }; + u.a.0 = 0xBE; + assert_eq!(u.b, 0xDE_BE); + + let mut u = U:: { b: 0xDEAD_DEAD }; + u.a.0 = 0xBEEF; + assert_eq!(u.b, 0xDEAD_BEEF); + + let mut u = U:: { b: 0xDEADBEEF_DEADBEEF }; + u.a.0 = 0xBAADF00D; + assert_eq!(u.b, 0xDEADBEEF_BAADF00D); + + let mut w = W::, u8>, u32> { b: 0xDEAD_DEAD }; + w.a.0 = Triple(0, 0, 0); + assert_eq!(w.b, 0xDE00_0000); + + let mut w = W::>, u32> { b: 0xDEAD_DEAD }; + w.a.1 = Triple(0, 0, 0); + assert_eq!(w.b, 0x0000_00AD); +} + +#[cfg(target_endian = "big")] +unsafe fn check() { + let mut u = U:: { b: 0xDE_DE }; + u.a.0 = 0xBE; + assert_eq!(u.b, 0xBE_DE); + + let mut u = U:: { b: 0xDEAD_DEAD }; + u.a.0 = 0xBEEF; + assert_eq!(u.b, 0xBEEF_DEAD); + + let mut u = U:: { b: 0xDEADBEEF_DEADBEEF }; + u.a.0 = 0xBAADF00D; + assert_eq!(u.b, 0xBAADF00D_DEADBEEF); + + let mut w = W::, u8>, u32> { b: 0xDEAD_DEAD }; + w.a.0 = Triple(0, 0, 0); + assert_eq!(w.b, 0x0000_00AD); + + let mut w = W::>, u32> { b: 0xDEAD_DEAD }; + w.a.1 = Triple(0, 0, 0); + assert_eq!(w.b, 0xDE00_0000); +} + +fn main() { + unsafe { + check(); + } +} diff --git a/src/test/run-pass/union/union-packed.rs b/src/test/run-pass/union/union-packed.rs new file mode 100644 index 00000000000..6a61280823e --- /dev/null +++ b/src/test/run-pass/union/union-packed.rs @@ -0,0 +1,104 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +use std::mem::{size_of, size_of_val, align_of, align_of_val}; + +struct S { + a: u16, + b: [u8; 3], +} + +#[repr(packed)] +struct Sp { + a: u16, + b: [u8; 3], +} + +union U { + a: u16, + b: [u8; 3], +} + +#[repr(packed)] +union Up { + a: u16, + b: [u8; 3], +} + +const CS: S = S { a: 0, b: [0, 0, 0] }; +const CSP: Sp = Sp { a: 0, b: [0, 0, 0] }; +const CU: U = U { b: [0, 0, 0] }; +const CUP: Up = Up { b: [0, 0, 0] }; + +fn main() { + let s = S { a: 0, b: [0, 0, 0] }; + assert_eq!(size_of::(), 6); + assert_eq!(size_of_val(&s), 6); + assert_eq!(size_of_val(&CS), 6); + assert_eq!(align_of::(), 2); + assert_eq!(align_of_val(&s), 2); + assert_eq!(align_of_val(&CS), 2); + + let sp = Sp { a: 0, b: [0, 0, 0] }; + assert_eq!(size_of::(), 5); + assert_eq!(size_of_val(&sp), 5); + assert_eq!(size_of_val(&CSP), 5); + assert_eq!(align_of::(), 1); + assert_eq!(align_of_val(&sp), 1); + assert_eq!(align_of_val(&CSP), 1); + + let u = U { b: [0, 0, 0] }; + assert_eq!(size_of::(), 4); + assert_eq!(size_of_val(&u), 4); + assert_eq!(size_of_val(&CU), 4); + assert_eq!(align_of::(), 2); + assert_eq!(align_of_val(&u), 2); + assert_eq!(align_of_val(&CU), 2); + + let up = Up { b: [0, 0, 0] }; + assert_eq!(size_of::(), 3); + assert_eq!(size_of_val(&up), 3); + assert_eq!(size_of_val(&CUP), 3); + assert_eq!(align_of::(), 1); + assert_eq!(align_of_val(&up), 1); + assert_eq!(align_of_val(&CUP), 1); + + hybrid::check_hybrid(); +} + +mod hybrid { + use std::mem::size_of; + + #[repr(packed)] + struct S1 { + a: u16, + b: u8, + } + + #[repr(packed)] + union U { + s: S1, + c: u16, + } + + #[repr(packed)] + struct S2 { + d: u8, + u: U, + } + + pub fn check_hybrid() { + assert_eq!(size_of::(), 3); + assert_eq!(size_of::(), 3); + assert_eq!(size_of::(), 4); + } +} diff --git a/src/test/run-pass/union/union-pat-refutability.rs b/src/test/run-pass/union/union-pat-refutability.rs new file mode 100644 index 00000000000..e6144f35f1d --- /dev/null +++ b/src/test/run-pass/union/union-pat-refutability.rs @@ -0,0 +1,62 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +#[repr(u32)] +enum Tag { I, F } + +#[repr(C)] +union U { + i: i32, + f: f32, +} + +#[repr(C)] +struct Value { + tag: Tag, + u: U, +} + +fn is_zero(v: Value) -> bool { + unsafe { + match v { + Value { tag: Tag::I, u: U { i: 0 } } => true, + Value { tag: Tag::F, u: U { f: 0.0 } } => true, + _ => false, + } + } +} + +union W { + a: u8, + b: u8, +} + +fn refut(w: W) { + unsafe { + match w { + W { a: 10 } => { + panic!(); + } + W { b } => { + assert_eq!(b, 11); + } + } + } +} + +fn main() { + let v = Value { tag: Tag::I, u: U { i: 1 } }; + assert_eq!(is_zero(v), false); + + let w = W { a: 11 }; + refut(w); +} diff --git a/src/test/run-pass/union/union-trait-impl.rs b/src/test/run-pass/union/union-trait-impl.rs new file mode 100644 index 00000000000..a5a2be0133a --- /dev/null +++ b/src/test/run-pass/union/union-trait-impl.rs @@ -0,0 +1,27 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] + +use std::fmt; + +union U { + a: u8 +} + +impl fmt::Display for U { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + unsafe { write!(f, "Oh hai {}", self.a) } + } +} + +fn main() { + assert_eq!(U { a: 2 }.to_string(), "Oh hai 2"); +} diff --git a/src/test/run-pass/union/union-transmute.rs b/src/test/run-pass/union/union-transmute.rs new file mode 100644 index 00000000000..4eb66268ab8 --- /dev/null +++ b/src/test/run-pass/union/union-transmute.rs @@ -0,0 +1,40 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(core_float)] +#![feature(float_extras)] +#![feature(untagged_unions)] + +extern crate core; +use core::num::Float; + +union U { + a: (u8, u8), + b: u16, +} + +union W { + a: u32, + b: f32, +} + +fn main() { + unsafe { + let mut u = U { a: (1, 1) }; + assert_eq!(u.b, (1 << 8) + 1); + u.b = (2 << 8) + 2; + assert_eq!(u.a, (2, 2)); + + let mut w = W { a: 0b0_11111111_00000000000000000000000 }; + assert_eq!(w.b, f32::infinity()); + w.b = f32::neg_infinity(); + assert_eq!(w.a, 0b1_11111111_00000000000000000000000); + } +} diff --git a/src/test/run-pass/union/union-with-drop-fields-lint.rs b/src/test/run-pass/union/union-with-drop-fields-lint.rs new file mode 100644 index 00000000000..5a1424830d0 --- /dev/null +++ b/src/test/run-pass/union/union-with-drop-fields-lint.rs @@ -0,0 +1,42 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// ignore-pretty + +#![feature(untagged_unions)] +#![allow(dead_code)] +#![allow(unions_with_drop_fields)] + +union U { + a: u8, // OK +} + +union W { + a: String, // OK + b: String, // OK +} + +struct S(String); + +// `S` doesn't implement `Drop` trait, but still has non-trivial destructor +union Y { + a: S, // OK +} + +// We don't know if `T` is trivially-destructable or not until trans +union J { + a: T, // OK +} + +union H { + a: T, // OK +} + +fn main() {} diff --git a/src/test/run-pass/union/union-xcrate.rs b/src/test/run-pass/union/union-xcrate.rs new file mode 100644 index 00000000000..2a76c96ef25 --- /dev/null +++ b/src/test/run-pass/union/union-xcrate.rs @@ -0,0 +1,21 @@ +// 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 or the MIT license +// , 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; + +type A = union::U; + +fn main() { + assert_eq!(std::mem::size_of::(), 8); +} -- cgit 1.4.1-3-g733a5