Skip to content

"Partially moved" unions are dropped incorrectly #36246

Closed
@petrochenkov

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

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