Skip to content

Unexpected compile pass when pattern matching on union without unsafe #110974

Closed as not planned
@SamZhang3

Description

@SamZhang3

I tried this code:

union U {
    a: i32,
    b: (),
}

fn main() {
    let u = U {a: 123};
    match u {
        U {b: ()} => println!("Hello, world!"),
    }
}

I expected to see this happen: Compiler error, since pattern matching on a union is done without using unsafe; see the reference.

Instead, this happened: Compile succeeds.

It seems that this only works when matching with a zero-sized union field, and it does not appear possible to actually extract the field's value by binding it to a name.

Meta

rustc --version --verbose:

rustc 1.68.2 (9eb3afe9e 2023-03-27)
binary: rustc
commit-hash: 9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0
commit-date: 2023-03-27
host: x86_64-apple-darwin
release: 1.68.2
LLVM version: 15.0.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions