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

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

Closed
SamZhang3 opened this issue Apr 29, 2023 · 1 comment
Closed
Labels
C-bug Category: This is a bug.

Comments

@SamZhang3
Copy link
Contributor

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
@SamZhang3 SamZhang3 added the C-bug Category: This is a bug. label Apr 29, 2023
@Noratrieb
Copy link
Member

There's more discussion about this in #87520.
Thank you for the report, but closing as a duplicate

@Noratrieb Noratrieb closed this as not planned Won't fix, can't repro, duplicate, stale Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants