about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-02-17 06:30:03 +0100
committerGitHub <noreply@github.com>2022-02-17 06:30:03 +0100
commit91f70a8fdf3f39895d72d0b117722cff141015cf (patch)
treeccaaa34f4d90c69405044438597124a1a7861b42 /src/test
parenta1a750b5adce06fc77b22ee32eecd7c83ad2d090 (diff)
parent6d2cdbec3ef6e5460753986372ed898a1b3bc553 (diff)
downloadrust-91f70a8fdf3f39895d72d0b117722cff141015cf.tar.gz
rust-91f70a8fdf3f39895d72d0b117722cff141015cf.zip
Rollup merge of #94031 - danielhenrymantilla:diagnostics/union-drop-suggest-copy-bound-alternative, r=davidtwco
[diagnostics] Add mentions to `Copy` types being valid for `union` fields

This came up from some user on Discord which was using a `T : PrimitiveInt` generic type, and they wanted to use in a `union`. Rather than adding a `Copy` bound, they started pondering about the `ManuallyDrop<T>` road, and how to correctly use `unsafe` to perform the drops.

<img width="648" alt="Screen Shot 2022-02-15 at 22 28 34" src="https://user-images.githubusercontent.com/9920355/154152496-8f9be74b-ad59-4724-8f9e-48b446774e06.png">

  - [Discord link](https://discord.com/channels/442252698964721669/443150878111694848/943092778534072320)

So, it seemed like the error message for types with potential drop glue on `union` fields could be improved to also mention the `Copy` alternative, since in many cases where `union`s are concerned, people are dealing with PODs / `Copy` types anyways 🙂

___

``@rustbot`` modify labels: +A-diagnostics +D-terse
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/feature-gates/feature-gate-untagged_unions.rs4
-rw-r--r--src/test/ui/feature-gates/feature-gate-untagged_unions.stderr10
-rw-r--r--src/test/ui/union/issue-41073.rs2
-rw-r--r--src/test/ui/union/issue-41073.stderr5
-rw-r--r--src/test/ui/union/union-custom-drop.rs2
-rw-r--r--src/test/ui/union/union-custom-drop.stderr5
-rw-r--r--src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr15
-rw-r--r--src/test/ui/union/union-with-drop-fields.rs6
-rw-r--r--src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr15
9 files changed, 37 insertions, 27 deletions
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 f5f9631c3bc..af8d8e92b20 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<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!
@@ -21,7 +21,7 @@ union U32 { // field that does not drop but is not `Copy`, either -- this is the
 }
 
 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
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 0967cb7ba8b..9e4a89f80c8 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<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>,
    |        +++++++++++++++++++++++ +
diff --git a/src/test/ui/union/issue-41073.rs b/src/test/ui/union/issue-41073.rs
index 91e9a0d0b65..80474b807e7 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 8edf4db441b..7d4208b10da 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<A>,
    |        +++++++++++++++++++++++ +
diff --git a/src/test/ui/union/union-custom-drop.rs b/src/test/ui/union/union-custom-drop.rs
index 8f816cc1b73..4b333631ec0 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 65ca5fd931d..b5579eeef09 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<Bar>,
    |          +++++++++++++++++++++++   +
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 f5e9681735c..93fe996d2a4 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<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>,
    |        +++++++++++++++++++++++ +
diff --git a/src/test/ui/union/union-with-drop-fields.rs b/src/test/ui/union/union-with-drop-fields.rs
index 96c293418b6..a7a8b69e784 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<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> {
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 f5e9681735c..93fe996d2a4 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<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>,
    |        +++++++++++++++++++++++ +