Skip to content

Commit 7c5588e

Browse files
authored
Rollup merge of #87734 - Smittyvb:more-union-tests, r=LeSeulArtichaut
Test dropping union fields more Now that #87403 is merged, a few more tests can be added for reads/writes to dropping union fields. r? ``@LeSeulArtichaut``
2 parents 1666c26 + 6953f17 commit 7c5588e

5 files changed

+151
-22
lines changed

src/test/ui/union/union-drop.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// run-pass
2+
// revisions: mirunsafeck thirunsafeck
3+
// [thirunsafeck]compile-flags: -Z thir-unsafeck
4+
25
#![allow(dead_code)]
36
#![allow(unused_variables)]
47

src/test/ui/union/union-drop.thirunsafeck.stderr

-22
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
error[E0133]: access to union field is unsafe and requires unsafe function or block
2+
--> $DIR/union-assignop.rs:20:5
3+
|
4+
LL | foo.a += 5;
5+
| ^^^^^^^^^^ access to union field
6+
|
7+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
8+
9+
error[E0133]: access to union field is unsafe and requires unsafe function or block
10+
--> $DIR/union-assignop.rs:21:5
11+
|
12+
LL | foo.b += Dropping;
13+
| ^^^^^ access to union field
14+
|
15+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
16+
17+
error[E0133]: assignment to union field that might need dropping is unsafe and requires unsafe function or block
18+
--> $DIR/union-assignop.rs:22:5
19+
|
20+
LL | foo.b = Dropping;
21+
| ^^^^^^^^^^^^^^^^ assignment to union field that might need dropping
22+
|
23+
= note: the previous content of the field will be dropped, which causes undefined behavior if the field was not properly initialized
24+
25+
error[E0133]: access to union field is unsafe and requires unsafe function or block
26+
--> $DIR/union-assignop.rs:23:5
27+
|
28+
LL | foo.a;
29+
| ^^^^^ access to union field
30+
|
31+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
32+
33+
error[E0133]: access to union field is unsafe and requires unsafe function or block
34+
--> $DIR/union-assignop.rs:25:5
35+
|
36+
LL | foo.b;
37+
| ^^^^^ access to union field
38+
|
39+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
40+
41+
error[E0133]: access to union field is unsafe and requires unsafe function or block
42+
--> $DIR/union-assignop.rs:27:13
43+
|
44+
LL | foo.b = foo.b;
45+
| ^^^^^ access to union field
46+
|
47+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
48+
49+
error[E0133]: assignment to union field that might need dropping is unsafe and requires unsafe function or block
50+
--> $DIR/union-assignop.rs:27:5
51+
|
52+
LL | foo.b = foo.b;
53+
| ^^^^^^^^^^^^^ assignment to union field that might need dropping
54+
|
55+
= note: the previous content of the field will be dropped, which causes undefined behavior if the field was not properly initialized
56+
57+
error: aborting due to 7 previous errors
58+
59+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/unsafe/union-assignop.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// revisions: mirunsafeck thirunsafeck
2+
// [thirunsafeck]compile-flags: -Z thir-unsafeck
3+
4+
#![feature(untagged_unions)]
5+
6+
use std::ops::AddAssign;
7+
8+
struct Dropping;
9+
impl AddAssign for Dropping {
10+
fn add_assign(&mut self, _: Self) {}
11+
}
12+
13+
union Foo {
14+
a: u8, // non-dropping
15+
b: Dropping, // treated as dropping
16+
}
17+
18+
fn main() {
19+
let mut foo = Foo { a: 42 };
20+
foo.a += 5; //~ ERROR access to union field is unsafe
21+
foo.b += Dropping; //~ ERROR access to union field is unsafe
22+
foo.b = Dropping; //~ ERROR assignment to union field that might need dropping is unsafe
23+
foo.a; //~ ERROR access to union field is unsafe
24+
let foo = Foo { a: 42 };
25+
foo.b; //~ ERROR access to union field is unsafe
26+
let mut foo = Foo { a: 42 };
27+
foo.b = foo.b;
28+
//~^ ERROR access to union field is unsafe
29+
//~| ERROR assignment to union field that might need dropping
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
error[E0133]: access to union field is unsafe and requires unsafe function or block
2+
--> $DIR/union-assignop.rs:20:5
3+
|
4+
LL | foo.a += 5;
5+
| ^^^^^ access to union field
6+
|
7+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
8+
9+
error[E0133]: access to union field is unsafe and requires unsafe function or block
10+
--> $DIR/union-assignop.rs:21:5
11+
|
12+
LL | foo.b += Dropping;
13+
| ^^^^^ access to union field
14+
|
15+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
16+
17+
error[E0133]: assignment to union field that might need dropping is unsafe and requires unsafe function or block
18+
--> $DIR/union-assignop.rs:22:5
19+
|
20+
LL | foo.b = Dropping;
21+
| ^^^^^^^^^^^^^^^^ assignment to union field that might need dropping
22+
|
23+
= note: the previous content of the field will be dropped, which causes undefined behavior if the field was not properly initialized
24+
25+
error[E0133]: access to union field is unsafe and requires unsafe function or block
26+
--> $DIR/union-assignop.rs:23:5
27+
|
28+
LL | foo.a;
29+
| ^^^^^ access to union field
30+
|
31+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
32+
33+
error[E0133]: access to union field is unsafe and requires unsafe function or block
34+
--> $DIR/union-assignop.rs:25:5
35+
|
36+
LL | foo.b;
37+
| ^^^^^ access to union field
38+
|
39+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
40+
41+
error[E0133]: assignment to union field that might need dropping is unsafe and requires unsafe function or block
42+
--> $DIR/union-assignop.rs:27:5
43+
|
44+
LL | foo.b = foo.b;
45+
| ^^^^^^^^^^^^^ assignment to union field that might need dropping
46+
|
47+
= note: the previous content of the field will be dropped, which causes undefined behavior if the field was not properly initialized
48+
49+
error[E0133]: access to union field is unsafe and requires unsafe function or block
50+
--> $DIR/union-assignop.rs:27:13
51+
|
52+
LL | foo.b = foo.b;
53+
| ^^^^^ access to union field
54+
|
55+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
56+
57+
error: aborting due to 7 previous errors
58+
59+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)