Skip to content
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

"Partially moved" unions are dropped incorrectly #36246

Closed
petrochenkov opened this issue Sep 3, 2016 · 2 comments
Closed

"Partially moved" unions are dropped incorrectly #36246

petrochenkov opened this issue Sep 3, 2016 · 2 comments
Labels
A-destructors Area: destructors (Drop, ..) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@petrochenkov
Copy link
Contributor

Code:

#![feature(untagged_unions)]
#![allow(unused)]

struct S;
struct Z;

impl Drop for S { fn drop(&mut self) { println!("S"); } }
impl Drop for Z { fn drop(&mut self) { println!("Z"); } }

#[allow(unions_with_drop_fields)]
union U {
    s: S,
    z: Z,
}

fn main() {
    let u = U { s: S };
    let s = unsafe { u.s }; // Move `u.s` out of `u`.
    // Drop `s`.
    // Drop `u`, noop.
}

Expected output:

S

Actual output:

S
Z

i.e. u.z is dropped too for some reason.

This is probably caused by some union-specific logic missing from librustc_borrowck/borrowck/fragments.rs and librustc_borrowck/borrowck/mir/.

@pnkfelix pnkfelix added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-destructors Area: destructors (Drop, ..) labels Sep 5, 2016
@pnkfelix
Copy link
Member

pnkfelix commented Sep 5, 2016

cc #32836 (should ensure this is fixed before feature is stabilized)

@joshtriplett
Copy link
Member

joshtriplett commented Sep 6, 2016

Interestingly, if you remove the Drop impl for S, then Z's drop doesn't get called either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-destructors Area: destructors (Drop, ..) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants