forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#75584 - RalfJung:union-no-deref, r=matthewj…
…asper do not apply DerefMut on union field This implements the part of [RFC 2514](/~https://github.com/rust-lang/rfcs/blob/master/text/2514-union-initialization-and-drop.md) about `DerefMut`. Unlike described in the RFC, we only apply this warning specifically when doing `DerefMut` of a `ManuallyDrop` field; that is really the case we are worried about here. @matthewjasper suggested I patch `convert_place_derefs_to_mutable` and `convert_place_op_to_mutable` for this, but I could not find anything to do in `convert_place_op_to_mutable` and this is sufficient to make the test pass. However, maybe there are some other cases this misses? I have no familiarity with this code. This is a breaking change *in theory*, if someone used `ManuallyDrop<T>` in a union field and relied on automatic `DerefMut`. But on stable this means `T: Copy`, so the `ManuallyDrop` is rather pointless. Cc rust-lang#55149
- Loading branch information
Showing
3 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// ignore-tidy-linelength | ||
//! Test the part of RFC 2514 that is about not applying `DerefMut` coercions | ||
//! of union fields. | ||
#![feature(untagged_unions)] | ||
|
||
use std::mem::ManuallyDrop; | ||
|
||
union U1<T> { x:(), f: ManuallyDrop<(T,)> } | ||
|
||
union U2<T> { x:(), f: (ManuallyDrop<(T,)>,) } | ||
|
||
fn main() { | ||
let mut u : U1<Vec<i32>> = U1 { x: () }; | ||
unsafe { (*u.f).0 = Vec::new() }; // explicit deref, this compiles | ||
unsafe { u.f.0 = Vec::new() }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field | ||
unsafe { &mut (*u.f).0 }; // explicit deref, this compiles | ||
unsafe { &mut u.f.0 }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field | ||
unsafe { (*u.f).0.push(0) }; // explicit deref, this compiles | ||
unsafe { u.f.0.push(0) }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field | ||
|
||
let mut u : U2<Vec<i32>> = U2 { x: () }; | ||
unsafe { (*u.f.0).0 = Vec::new() }; // explicit deref, this compiles | ||
unsafe { u.f.0.0 = Vec::new() }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field | ||
unsafe { &mut (*u.f.0).0 }; // explicit deref, this compiles | ||
unsafe { &mut u.f.0.0 }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field | ||
unsafe { (*u.f.0).0.push(0) }; // explicit deref, this compiles | ||
unsafe { u.f.0.0.push(0) }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
error: not automatically applying `DerefMut` on `ManuallyDrop` union field | ||
--> $DIR/union-deref.rs:15:14 | ||
| | ||
LL | unsafe { u.f.0 = Vec::new() }; | ||
| ^^^ | ||
| | ||
= help: writing to this reference calls the destructor for the old value | ||
= help: add an explicit `*` if that is desired, or call `ptr::write` to not run the destructor | ||
|
||
error: not automatically applying `DerefMut` on `ManuallyDrop` union field | ||
--> $DIR/union-deref.rs:17:19 | ||
| | ||
LL | unsafe { &mut u.f.0 }; | ||
| ^^^ | ||
| | ||
= help: writing to this reference calls the destructor for the old value | ||
= help: add an explicit `*` if that is desired, or call `ptr::write` to not run the destructor | ||
|
||
error: not automatically applying `DerefMut` on `ManuallyDrop` union field | ||
--> $DIR/union-deref.rs:19:14 | ||
| | ||
LL | unsafe { u.f.0.push(0) }; | ||
| ^^^ | ||
| | ||
= help: writing to this reference calls the destructor for the old value | ||
= help: add an explicit `*` if that is desired, or call `ptr::write` to not run the destructor | ||
|
||
error: not automatically applying `DerefMut` on `ManuallyDrop` union field | ||
--> $DIR/union-deref.rs:23:14 | ||
| | ||
LL | unsafe { u.f.0.0 = Vec::new() }; | ||
| ^^^^^^^ | ||
| | ||
= help: writing to this reference calls the destructor for the old value | ||
= help: add an explicit `*` if that is desired, or call `ptr::write` to not run the destructor | ||
|
||
error: not automatically applying `DerefMut` on `ManuallyDrop` union field | ||
--> $DIR/union-deref.rs:25:19 | ||
| | ||
LL | unsafe { &mut u.f.0.0 }; | ||
| ^^^^^^^ | ||
| | ||
= help: writing to this reference calls the destructor for the old value | ||
= help: add an explicit `*` if that is desired, or call `ptr::write` to not run the destructor | ||
|
||
error: not automatically applying `DerefMut` on `ManuallyDrop` union field | ||
--> $DIR/union-deref.rs:27:14 | ||
| | ||
LL | unsafe { u.f.0.0.push(0) }; | ||
| ^^^^^^^ | ||
| | ||
= help: writing to this reference calls the destructor for the old value | ||
= help: add an explicit `*` if that is desired, or call `ptr::write` to not run the destructor | ||
|
||
error: aborting due to 6 previous errors | ||
|