Skip to content

attempted to leave type (enum) uninitialized, which is invalid #73573

Closed
@stepancheg

Description

@stepancheg

This code worked fine a couple of months ago:

#[derive(Copy, Clone, Debug)]
enum Fruit {
    Apple,
    _Banana,
}

fn foo() -> Fruit {
    unsafe {
        let mut r = mem::uninitialized();
        ptr::write(&mut r as *mut Fruit, Fruit::Apple);
        r
    }
}

Now (1.44.1), this code crashes with:

thread 'main' panicked at 'attempted to leave type `Fruit` uninitialized, which is invalid', ...

This code looks perfectly valid to me: we create a slot for a variable, write to that slot exactly once, and return the value.

Play

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