Skip to content

Commit d17e54f

Browse files
committed
Update tests for union fields with drop rule change
1 parent bfac2f7 commit d17e54f

File tree

8 files changed

+70
-65
lines changed

8 files changed

+70
-65
lines changed

src/test/run-pass/union/union-derive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ impl PartialEq for U { fn eq(&self, rhs: &Self) -> bool { true } }
2424
Copy,
2525
Eq
2626
)]
27-
union W<T> {
27+
union W<T: Copy> {
2828
a: T,
2929
}
3030

31-
impl<T> PartialEq for W<T> { fn eq(&self, rhs: &Self) -> bool { true } }
31+
impl<T: Copy> PartialEq for W<T> { fn eq(&self, rhs: &Self) -> bool { true } }
3232

3333
fn main() {
3434
let u = U { b: 0 };

src/test/run-pass/union/union-drop-assign.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
#![feature(untagged_unions)]
88

9+
use std::mem::ManuallyDrop;
10+
911
struct S;
1012

1113
union U {
12-
a: S
14+
a: ManuallyDrop<S>
1315
}
1416

1517
impl Drop for S {
@@ -28,11 +30,11 @@ static mut CHECK: u8 = 0;
2830

2931
fn main() {
3032
unsafe {
31-
let mut u = U { a: S };
33+
let mut u = U { a: ManuallyDrop::new(S) };
3234
assert_eq!(CHECK, 0);
33-
u = U { a: S };
35+
u = U { a: ManuallyDrop::new(S) };
3436
assert_eq!(CHECK, 1); // union itself is assigned, union is dropped, field is not dropped
35-
u.a = S;
37+
*u.a = S;
3638
assert_eq!(CHECK, 11); // union field is assigned, field is dropped
3739
}
3840
}

src/test/run-pass/union/union-drop.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ union Y {
2121
a: S,
2222
}
2323

24-
impl Drop for S {
25-
fn drop(&mut self) {
26-
unsafe { CHECK += 10; }
27-
}
28-
}
29-
3024
impl Drop for U {
3125
fn drop(&mut self) {
3226
unsafe { CHECK += 1; }
@@ -51,10 +45,10 @@ fn main() {
5145
{
5246
let w = W { a: S };
5347
}
54-
assert_eq!(CHECK, 2); // 2, not 11, dtor of S is not called
48+
assert_eq!(CHECK, 2); // 2, dtor of W is called
5549
{
5650
let y = Y { a: S };
5751
}
58-
assert_eq!(CHECK, 2); // 2, not 12, dtor of S is not called
52+
assert_eq!(CHECK, 2); // 2, dtor of Y is called
5953
}
6054
}

src/test/run-pass/union/union-generic.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
// run-pass
22
#![allow(dead_code)]
3-
#![allow(unions_with_drop_fields)]
43

54
#![feature(untagged_unions)]
65

7-
union MaybeItem<T: Iterator> {
6+
union MaybeItem<T: Iterator> where T::Item: Copy {
87
elem: T::Item,
98
none: (),
109
}
1110

12-
union U<A, B> {
11+
union U<A, B> where A: Copy, B: Copy {
1312
a: A,
1413
b: B,
1514
}
1615

17-
unsafe fn union_transmute<A, B>(a: A) -> B {
16+
unsafe fn union_transmute<A, B>(a: A) -> B where A: Copy, B: Copy {
1817
U { a: a }.b
1918
}
2019

2120
fn main() {
2221
unsafe {
23-
let u = U::<String, Vec<u8>> { a: String::from("abcd") };
24-
25-
assert_eq!(u.b.len(), 4);
26-
assert_eq!(u.b[0], b'a');
27-
2822
let b = union_transmute::<(u8, u8), u16>((1, 1));
2923
assert_eq!(b, (1 << 8) + 1);
3024

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#![feature(untagged_unions)]
2+
#![allow(dead_code)]
3+
// run-pass
4+
5+
use std::mem::needs_drop;
6+
use std::mem::ManuallyDrop;
7+
8+
struct NeedDrop;
9+
10+
impl Drop for NeedDrop {
11+
fn drop(&mut self) {}
12+
}
13+
14+
union UnionOk1<T> {
15+
empty: (),
16+
value: ManuallyDrop<T>,
17+
}
18+
19+
union UnionOk2 {
20+
value: ManuallyDrop<NeedDrop>,
21+
}
22+
23+
#[allow(dead_code)]
24+
union UnionOk3<T: Copy> {
25+
empty: (),
26+
value: T,
27+
}
28+
29+
trait Foo { }
30+
31+
trait ImpliesCopy : Copy { }
32+
33+
#[allow(dead_code)]
34+
union UnionOk4<T: ImpliesCopy> {
35+
value: T,
36+
}
37+
38+
fn main() {
39+
// NeedDrop should not make needs_drop true
40+
assert!(!needs_drop::<UnionOk1<NeedDrop>>());
41+
assert!(!needs_drop::<UnionOk3<&Foo>>());
42+
}

src/test/run-pass/union/union-nodrop.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// run-pass
22

3-
#![feature(core_intrinsics)]
43
#![feature(untagged_unions)]
54

6-
#![allow(unions_with_drop_fields)]
75
#![allow(dead_code)]
86

9-
use std::intrinsics::needs_drop;
7+
use std::mem::needs_drop;
8+
use std::mem::ManuallyDrop;
109

1110
struct NeedDrop;
1211

@@ -16,10 +15,10 @@ impl Drop for NeedDrop {
1615

1716
// Constant expressios allow `NoDrop` to go out of scope,
1817
// unlike a value of the interior type implementing `Drop`.
19-
static X: () = (NoDrop { inner: NeedDrop }, ()).1;
18+
static X: () = (NoDrop { inner: ManuallyDrop::new(NeedDrop) }, ()).1;
2019

2120
// A union that scrubs the drop glue from its inner type
22-
union NoDrop<T> {inner: T}
21+
union NoDrop<T> { inner: ManuallyDrop<T> }
2322

2423
// Copy currently can't be implemented on drop-containing unions,
2524
// this may change later
@@ -40,7 +39,7 @@ struct Baz {
4039
y: Box<u8>,
4140
}
4241

43-
union ActuallyDrop<T> {inner: T}
42+
union ActuallyDrop<T> { inner: ManuallyDrop<T> }
4443

4544
impl<T> Drop for ActuallyDrop<T> {
4645
fn drop(&mut self) {}

src/test/run-pass/union/union-overwrite.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
// run-pass
2-
#![allow(unions_with_drop_fields)]
3-
42
#![feature(untagged_unions)]
53

64
#[repr(C)]
5+
#[derive(Copy, Clone)]
76
struct Pair<T, U>(T, U);
87
#[repr(C)]
8+
#[derive(Copy, Clone)]
99
struct Triple<T>(T, T, T);
1010

1111
#[repr(C)]
12-
union U<A, B> {
12+
union U<A, B>
13+
where
14+
A: Copy, B: Copy
15+
{
1316
a: Pair<A, A>,
1417
b: B,
1518
}
1619

1720
#[repr(C)]
18-
union W<A, B> {
21+
union W<A, B>
22+
where
23+
A: Copy, B: Copy
24+
{
1925
a: A,
2026
b: B,
2127
}

src/test/run-pass/union/union-with-drop-fields-lint.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)