Skip to content

"Partially moved" unions are dropped incorrectly #36246

Closed

Description

@petrochenkov

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/.

Activity

  1. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-destructorsArea: Destructors (`Drop`, …)
    on Sep 5, 2016
  2. pnkfelix commented on Sep 5, 2016

    @pnkfelix
    Contributor

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

  3. joshtriplett commented on Sep 6, 2016

    @joshtriplett
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-destructorsArea: Destructors (`Drop`, …)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions