Closed
Description
So it turns out that this is a problem: The following code should be rejected by dropck, but it compiles.
#![feature(untagged_unions)]
#![allow(unions_with_drop_fields)]
use std::cell::Cell;
use std::ops::Deref;
union Wrap<T> { x: T }
impl<T> Drop for Wrap<T> {
fn drop(&mut self) {
unsafe { std::ptr::drop_in_place(&mut self.x as *mut _); }
}
}
impl<T> Wrap<T> {
fn new(x: T) -> Self {
Wrap { x }
}
}
impl<T> Deref for Wrap<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
unsafe {
&self.x
}
}
}
struct C<'a>(Cell<Option<&'a C<'a>>>);
impl<'a> Drop for C<'a> {
fn drop(&mut self) {}
}
fn main() {
let v : Wrap<C> = Wrap::new(C(Cell::new(None)));
v.0.set(Some(&v));
}
This is a nightly-only (because implementing Drop
for unions is unstable) soundness issue. I need unsafe code to exploit it, but Wrap
above should be a perfectly safe to use type.