-
Notifications
You must be signed in to change notification settings - Fork 13.4k
do not apply DerefMut on union field #75584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
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() }; | ||
| ^^^^^^^ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The span for this is slightly wrong, I'd expect it to only cover There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would guess this has to do with the special handling of multiple tuple field accesses in a row. Perhaps 0.0 is a float token. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know there was such special handling.^^ So this would be a problem elsewhere, in the code that sets up the span for these places? |
||
| | ||
= 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 | ||
|
Uh oh!
There was an error while loading. Please reload this page.