Skip to content

dropck unsoundness: unions are ignored #52786

Closed
@RalfJung

Description

@RalfJung

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerI-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions