Skip to content

strange memory layout for enums with repr in webassembly #76916

Closed
@Freax13

Description

@Freax13

I tried this code:

#[repr(u8)]
pub enum DirectEnum {
    A(u32),
    B(u32),
}

#[repr(u8)]
pub enum IndirectEnum {
    A(Value),
    B(Value),
}

#[repr(C)]
pub struct Value {
    val: u32,
}

#[no_mangle]
pub extern "C" fn test_direct() {
    unsafe { call_with_direct(DirectEnum::B(0xFFF)) }
}

#[no_mangle]
pub extern "C" fn test_indirect() {
    unsafe { call_with_indirect(IndirectEnum::B(Value { val: 0xFFF })) }
}

#[link(wasm_import_module = "something")]
extern "C" {
    fn call_with_direct(a: DirectEnum);
    fn call_with_indirect(a: IndirectEnum);
}

and compiled it with -C opt-level=3 --target wasm32-unknown-unknown https://godbolt.org/z/cTqbMq

I expected to see this happen: The code should be the same for test_direct and test_indirect. It should push two i32 values: 1 and 4096.

Instead, this happened: test_indirect pushes 8 values instead of two: 1, 0, 0, 0, 255, 15, 0 and 0. The numbers pushed seem to depend on the bitsize of the repr meaning only 4 values get pushed for repr(u16)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-layoutArea: Memory layout of typesC-bugCategory: This is a bug.O-wasmTarget: WASM (WebAssembly), http://webassembly.org/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