Skip to content

Commit 6d2cdbe

Browse files
Add mentions to Copy for union fields
1 parent 6421a49 commit 6d2cdbe

10 files changed

+44
-29
lines changed

compiler/rustc_typeck/src/check/check.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,15 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b
382382
tcx.sess,
383383
field_span,
384384
E0740,
385-
"unions may not contain fields that need dropping"
385+
"unions cannot contain fields that may need dropping"
386+
)
387+
.note(
388+
"a type is guaranteed not to need dropping \
389+
when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type",
386390
)
387391
.multipart_suggestion_verbose(
388-
"wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped",
392+
"when the type does not implement `Copy`, \
393+
wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped",
389394
vec![
390395
(ty_span.shrink_to_lo(), format!("std::mem::ManuallyDrop<")),
391396
(ty_span.shrink_to_hi(), ">".into()),

src/test/ui/feature-gates/feature-gate-untagged_unions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ union U22<T> { // OK
1313
}
1414

1515
union U3 {
16-
a: String, //~ ERROR unions may not contain fields that need dropping
16+
a: String, //~ ERROR unions cannot contain fields that may need dropping
1717
}
1818

1919
union U32 { // field that does not drop but is not `Copy`, either -- this is the real feature gate test!
2020
a: std::cell::RefCell<i32>, //~ ERROR unions with non-`Copy` fields other than `ManuallyDrop<T>` are unstable
2121
}
2222

2323
union U4<T> {
24-
a: T, //~ ERROR unions may not contain fields that need dropping
24+
a: T, //~ ERROR unions cannot contain fields that may need dropping
2525
}
2626

2727
union U5 { // Having a drop impl is OK

src/test/ui/feature-gates/feature-gate-untagged_unions.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@ LL | a: std::cell::RefCell<i32>,
77
= note: see issue #55149 <https://github.com/rust-lang/rust/issues/55149> for more information
88
= help: add `#![feature(untagged_unions)]` to the crate attributes to enable
99

10-
error[E0740]: unions may not contain fields that need dropping
10+
error[E0740]: unions cannot contain fields that may need dropping
1111
--> $DIR/feature-gate-untagged_unions.rs:16:5
1212
|
1313
LL | a: String,
1414
| ^^^^^^^^^
1515
|
16-
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
16+
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
17+
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
1718
|
1819
LL | a: std::mem::ManuallyDrop<String>,
1920
| +++++++++++++++++++++++ +
2021

21-
error[E0740]: unions may not contain fields that need dropping
22+
error[E0740]: unions cannot contain fields that may need dropping
2223
--> $DIR/feature-gate-untagged_unions.rs:24:5
2324
|
2425
LL | a: T,
2526
| ^^^^
2627
|
27-
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
28+
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
29+
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
2830
|
2931
LL | a: std::mem::ManuallyDrop<T>,
3032
| +++++++++++++++++++++++ +

src/test/ui/union/issue-41073.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(untagged_unions)]
22

33
union Test {
4-
a: A, //~ ERROR unions may not contain fields that need dropping
4+
a: A, //~ ERROR unions cannot contain fields that may need dropping
55
b: B
66
}
77

src/test/ui/union/issue-41073.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
error[E0740]: unions may not contain fields that need dropping
1+
error[E0740]: unions cannot contain fields that may need dropping
22
--> $DIR/issue-41073.rs:4:5
33
|
44
LL | a: A,
55
| ^^^^
66
|
7-
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
7+
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
8+
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
89
|
910
LL | a: std::mem::ManuallyDrop<A>,
1011
| +++++++++++++++++++++++ +

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![feature(untagged_unions)]
55

66
union Foo {
7-
bar: Bar, //~ ERROR unions may not contain fields that need dropping
7+
bar: Bar, //~ ERROR unions cannot contain fields that may need dropping
88
}
99

1010
union Bar {

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
error[E0740]: unions may not contain fields that need dropping
1+
error[E0740]: unions cannot contain fields that may need dropping
22
--> $DIR/union-custom-drop.rs:7:5
33
|
44
LL | bar: Bar,
55
| ^^^^^^^^
66
|
7-
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
7+
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
8+
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
89
|
910
LL | bar: std::mem::ManuallyDrop<Bar>,
1011
| +++++++++++++++++++++++ +

src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1-
error[E0740]: unions may not contain fields that need dropping
1+
error[E0740]: unions cannot contain fields that may need dropping
22
--> $DIR/union-with-drop-fields.rs:11:5
33
|
44
LL | a: String,
55
| ^^^^^^^^^
66
|
7-
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
7+
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
8+
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
89
|
910
LL | a: std::mem::ManuallyDrop<String>,
1011
| +++++++++++++++++++++++ +
1112

12-
error[E0740]: unions may not contain fields that need dropping
13+
error[E0740]: unions cannot contain fields that may need dropping
1314
--> $DIR/union-with-drop-fields.rs:19:5
1415
|
1516
LL | a: S,
1617
| ^^^^
1718
|
18-
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
19+
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
20+
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
1921
|
2022
LL | a: std::mem::ManuallyDrop<S>,
2123
| +++++++++++++++++++++++ +
2224

23-
error[E0740]: unions may not contain fields that need dropping
25+
error[E0740]: unions cannot contain fields that may need dropping
2426
--> $DIR/union-with-drop-fields.rs:24:5
2527
|
2628
LL | a: T,
2729
| ^^^^
2830
|
29-
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
31+
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
32+
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
3033
|
3134
LL | a: std::mem::ManuallyDrop<T>,
3235
| +++++++++++++++++++++++ +

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ union U {
88
}
99

1010
union W {
11-
a: String, //~ ERROR unions may not contain fields that need dropping
11+
a: String, //~ ERROR unions cannot contain fields that may need dropping
1212
b: String, // OK, only one field is reported
1313
}
1414

1515
struct S(String);
1616

1717
// `S` doesn't implement `Drop` trait, but still has non-trivial destructor
1818
union Y {
19-
a: S, //~ ERROR unions may not contain fields that need dropping
19+
a: S, //~ ERROR unions cannot contain fields that may need dropping
2020
}
2121

2222
// We don't know if `T` is trivially-destructable or not until trans
2323
union J<T> {
24-
a: T, //~ ERROR unions may not contain fields that need dropping
24+
a: T, //~ ERROR unions cannot contain fields that may need dropping
2525
}
2626

2727
union H<T: Copy> {

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1-
error[E0740]: unions may not contain fields that need dropping
1+
error[E0740]: unions cannot contain fields that may need dropping
22
--> $DIR/union-with-drop-fields.rs:11:5
33
|
44
LL | a: String,
55
| ^^^^^^^^^
66
|
7-
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
7+
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
8+
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
89
|
910
LL | a: std::mem::ManuallyDrop<String>,
1011
| +++++++++++++++++++++++ +
1112

12-
error[E0740]: unions may not contain fields that need dropping
13+
error[E0740]: unions cannot contain fields that may need dropping
1314
--> $DIR/union-with-drop-fields.rs:19:5
1415
|
1516
LL | a: S,
1617
| ^^^^
1718
|
18-
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
19+
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
20+
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
1921
|
2022
LL | a: std::mem::ManuallyDrop<S>,
2123
| +++++++++++++++++++++++ +
2224

23-
error[E0740]: unions may not contain fields that need dropping
25+
error[E0740]: unions cannot contain fields that may need dropping
2426
--> $DIR/union-with-drop-fields.rs:24:5
2527
|
2628
LL | a: T,
2729
| ^^^^
2830
|
29-
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
31+
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
32+
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
3033
|
3134
LL | a: std::mem::ManuallyDrop<T>,
3235
| +++++++++++++++++++++++ +

0 commit comments

Comments
 (0)