Skip to content

Commit

Permalink
Add mentions to Copy for union fields
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhenrymantilla committed Feb 16, 2022
1 parent 6421a49 commit 6d2cdbe
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 29 deletions.
9 changes: 7 additions & 2 deletions compiler/rustc_typeck/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,15 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b
tcx.sess,
field_span,
E0740,
"unions may not contain fields that need dropping"
"unions cannot contain fields that may need dropping"
)
.note(
"a type is guaranteed not to need dropping \
when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type",
)
.multipart_suggestion_verbose(
"wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped",
"when the type does not implement `Copy`, \
wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped",
vec![
(ty_span.shrink_to_lo(), format!("std::mem::ManuallyDrop<")),
(ty_span.shrink_to_hi(), ">".into()),
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/feature-gates/feature-gate-untagged_unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ union U22<T> { // OK
}

union U3 {
a: String, //~ ERROR unions may not contain fields that need dropping
a: String, //~ ERROR unions cannot contain fields that may need dropping
}

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

union U4<T> {
a: T, //~ ERROR unions may not contain fields that need dropping
a: T, //~ ERROR unions cannot contain fields that may need dropping
}

union U5 { // Having a drop impl is OK
Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/feature-gates/feature-gate-untagged_unions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ LL | a: std::cell::RefCell<i32>,
= note: see issue #55149 <https://github.com/rust-lang/rust/issues/55149> for more information
= help: add `#![feature(untagged_unions)]` to the crate attributes to enable

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

error[E0740]: unions may not contain fields that need dropping
error[E0740]: unions cannot contain fields that may need dropping
--> $DIR/feature-gate-untagged_unions.rs:24:5
|
LL | a: T,
| ^^^^
|
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
|
LL | a: std::mem::ManuallyDrop<T>,
| +++++++++++++++++++++++ +
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/union/issue-41073.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(untagged_unions)]

union Test {
a: A, //~ ERROR unions may not contain fields that need dropping
a: A, //~ ERROR unions cannot contain fields that may need dropping
b: B
}

Expand Down
5 changes: 3 additions & 2 deletions src/test/ui/union/issue-41073.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
error[E0740]: unions may not contain fields that need dropping
error[E0740]: unions cannot contain fields that may need dropping
--> $DIR/issue-41073.rs:4:5
|
LL | a: A,
| ^^^^
|
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
|
LL | a: std::mem::ManuallyDrop<A>,
| +++++++++++++++++++++++ +
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/union/union-custom-drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![feature(untagged_unions)]

union Foo {
bar: Bar, //~ ERROR unions may not contain fields that need dropping
bar: Bar, //~ ERROR unions cannot contain fields that may need dropping
}

union Bar {
Expand Down
5 changes: 3 additions & 2 deletions src/test/ui/union/union-custom-drop.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
error[E0740]: unions may not contain fields that need dropping
error[E0740]: unions cannot contain fields that may need dropping
--> $DIR/union-custom-drop.rs:7:5
|
LL | bar: Bar,
| ^^^^^^^^
|
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
|
LL | bar: std::mem::ManuallyDrop<Bar>,
| +++++++++++++++++++++++ +
Expand Down
15 changes: 9 additions & 6 deletions src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
error[E0740]: unions may not contain fields that need dropping
error[E0740]: unions cannot contain fields that may need dropping
--> $DIR/union-with-drop-fields.rs:11:5
|
LL | a: String,
| ^^^^^^^^^
|
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
|
LL | a: std::mem::ManuallyDrop<String>,
| +++++++++++++++++++++++ +

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

error[E0740]: unions may not contain fields that need dropping
error[E0740]: unions cannot contain fields that may need dropping
--> $DIR/union-with-drop-fields.rs:24:5
|
LL | a: T,
| ^^^^
|
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
|
LL | a: std::mem::ManuallyDrop<T>,
| +++++++++++++++++++++++ +
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/union/union-with-drop-fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ union U {
}

union W {
a: String, //~ ERROR unions may not contain fields that need dropping
a: String, //~ ERROR unions cannot contain fields that may need dropping
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 unions may not contain fields that need dropping
a: S, //~ ERROR unions cannot contain fields that may need dropping
}

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

union H<T: Copy> {
Expand Down
15 changes: 9 additions & 6 deletions src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
error[E0740]: unions may not contain fields that need dropping
error[E0740]: unions cannot contain fields that may need dropping
--> $DIR/union-with-drop-fields.rs:11:5
|
LL | a: String,
| ^^^^^^^^^
|
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
|
LL | a: std::mem::ManuallyDrop<String>,
| +++++++++++++++++++++++ +

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

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

0 comments on commit 6d2cdbe

Please sign in to comment.