Skip to content

Commit 376a6b2

Browse files
committed
unions are not always trivially dropable
Fixes #52786
1 parent 0aa8d03 commit 376a6b2

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

src/librustc/traits/query/dropck_outlives.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,14 @@ pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'_, '_, 'tcx>, ty: Ty<'tcx>) ->
242242
.all(|t| trivial_dropck_outlives(tcx, t)),
243243

244244
ty::TyAdt(def, _) => {
245-
if def.is_union() {
246-
// Unions never have a dtor.
247-
true
248-
} else if Some(def.did) == tcx.lang_items().manually_drop() {
245+
if Some(def.did) == tcx.lang_items().manually_drop() {
249246
// `ManuallyDrop` never has a dtor.
250247
true
251248
} else {
252249
// Other types might. Moreover, PhantomData doesn't
253250
// have a dtor, but it is considered to own its
254-
// content, so it is non-trivial.
251+
// content, so it is non-trivial. Unions can have `impl Drop`,
252+
// and hence are non-trivial as well.
255253
false
256254
}
257255
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0597]: `v` does not live long enough
2+
--> $DIR/dropck-union.rs:39:18
3+
|
4+
LL | v.0.set(Some(&v)); //~ ERROR: `v` does not live long enough
5+
| ^^ borrowed value does not live long enough
6+
LL | }
7+
| -
8+
| |
9+
| `v` dropped here while still borrowed
10+
| borrow later used here, when `v` is dropped
11+
|
12+
= note: values in a scope are dropped in the opposite order they are defined
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0597`.

src/test/ui/dropck/dropck-union.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![feature(untagged_unions)]
2+
3+
use std::cell::Cell;
4+
use std::ops::Deref;
5+
use std::mem::ManuallyDrop;
6+
7+
union Wrap<T> { x: ManuallyDrop<T> }
8+
9+
impl<T> Drop for Wrap<T> {
10+
fn drop(&mut self) {
11+
unsafe { std::ptr::drop_in_place(&mut *self.x as *mut T); }
12+
}
13+
}
14+
15+
impl<T> Wrap<T> {
16+
fn new(x: T) -> Self {
17+
Wrap { x: ManuallyDrop::new(x) }
18+
}
19+
}
20+
21+
impl<T> Deref for Wrap<T> {
22+
type Target = T;
23+
#[inline]
24+
fn deref(&self) -> &Self::Target {
25+
unsafe {
26+
&self.x
27+
}
28+
}
29+
}
30+
31+
struct C<'a>(Cell<Option<&'a C<'a>>>);
32+
33+
impl<'a> Drop for C<'a> {
34+
fn drop(&mut self) {}
35+
}
36+
37+
fn main() {
38+
let v : Wrap<C> = Wrap::new(C(Cell::new(None)));
39+
v.0.set(Some(&v)); //~ ERROR: `v` does not live long enough
40+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0597]: `v` does not live long enough
2+
--> $DIR/dropck-union.rs:39:19
3+
|
4+
LL | v.0.set(Some(&v)); //~ ERROR: `v` does not live long enough
5+
| ^ borrowed value does not live long enough
6+
LL | }
7+
| - `v` dropped here while still borrowed
8+
|
9+
= note: values in a scope are dropped in the opposite order they are created
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)