Skip to content

Unions with uninhabited variants are considered uninhabited #46845

Closed
@gereeter

Description

@gereeter

Uninhabited variants should ideally be irrelevant to the representation, just like in enums.

As a test, this prints out 0 instead of 8:

#![feature(never_type)]
use std::mem;

union Foo {
    a: u64,
    b: !
}

fn main() {
    println!("{}", mem::size_of::<Foo>());
}

This happens because rustc considers unions more like structs than enums, and the test for uninhabitedness sees a single variant with two fields, one of which is uninhabited.

I have a fix for this, but it's slightly easier to bundle it with some other changes I'm working on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions