diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs index 423abc3227db6..ca2e75886c794 100644 --- a/compiler/rustc_typeck/src/check/check.rs +++ b/compiler/rustc_typeck/src/check/check.rs @@ -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()), diff --git a/src/test/ui/feature-gates/feature-gate-untagged_unions.rs b/src/test/ui/feature-gates/feature-gate-untagged_unions.rs index f5f9631c3bcf9..af8d8e92b20bd 100644 --- a/src/test/ui/feature-gates/feature-gate-untagged_unions.rs +++ b/src/test/ui/feature-gates/feature-gate-untagged_unions.rs @@ -13,7 +13,7 @@ union U22 { // 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! @@ -21,7 +21,7 @@ union U32 { // field that does not drop but is not `Copy`, either -- this is the } union U4 { - 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 diff --git a/src/test/ui/feature-gates/feature-gate-untagged_unions.stderr b/src/test/ui/feature-gates/feature-gate-untagged_unions.stderr index 0967cb7ba8bd2..9e4a89f80c852 100644 --- a/src/test/ui/feature-gates/feature-gate-untagged_unions.stderr +++ b/src/test/ui/feature-gates/feature-gate-untagged_unions.stderr @@ -7,24 +7,26 @@ LL | a: std::cell::RefCell, = note: see issue #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, | +++++++++++++++++++++++ + -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, | +++++++++++++++++++++++ + diff --git a/src/test/ui/union/issue-41073.rs b/src/test/ui/union/issue-41073.rs index 91e9a0d0b659c..80474b807e7f3 100644 --- a/src/test/ui/union/issue-41073.rs +++ b/src/test/ui/union/issue-41073.rs @@ -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 } diff --git a/src/test/ui/union/issue-41073.stderr b/src/test/ui/union/issue-41073.stderr index 8edf4db441b9c..7d4208b10da80 100644 --- a/src/test/ui/union/issue-41073.stderr +++ b/src/test/ui/union/issue-41073.stderr @@ -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, | +++++++++++++++++++++++ + diff --git a/src/test/ui/union/union-custom-drop.rs b/src/test/ui/union/union-custom-drop.rs index 8f816cc1b737c..4b333631ec0f7 100644 --- a/src/test/ui/union/union-custom-drop.rs +++ b/src/test/ui/union/union-custom-drop.rs @@ -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 { diff --git a/src/test/ui/union/union-custom-drop.stderr b/src/test/ui/union/union-custom-drop.stderr index 65ca5fd931d67..b5579eeef0977 100644 --- a/src/test/ui/union/union-custom-drop.stderr +++ b/src/test/ui/union/union-custom-drop.stderr @@ -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, | +++++++++++++++++++++++ + diff --git a/src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr b/src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr index f5e9681735c6f..93fe996d2a477 100644 --- a/src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr +++ b/src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr @@ -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, | +++++++++++++++++++++++ + -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, | +++++++++++++++++++++++ + -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, | +++++++++++++++++++++++ + diff --git a/src/test/ui/union/union-with-drop-fields.rs b/src/test/ui/union/union-with-drop-fields.rs index 96c293418b629..a7a8b69e784ab 100644 --- a/src/test/ui/union/union-with-drop-fields.rs +++ b/src/test/ui/union/union-with-drop-fields.rs @@ -8,7 +8,7 @@ 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 } @@ -16,12 +16,12 @@ 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 { - a: T, //~ ERROR unions may not contain fields that need dropping + a: T, //~ ERROR unions cannot contain fields that may need dropping } union H { diff --git a/src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr b/src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr index f5e9681735c6f..93fe996d2a477 100644 --- a/src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr +++ b/src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr @@ -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, | +++++++++++++++++++++++ + -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, | +++++++++++++++++++++++ + -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, | +++++++++++++++++++++++ +