Skip to content

Packed/extern unions are checked within comptime blocks, while they shouldn't #3134

@DutchGhost

Description

@DutchGhost

According to the documentation https://ziglang.org/documentation/master/#Wrong-Union-Field-Access, the following should work, because extern and packed unions dont have the wrong field access check:

const MyUnion = packed union {
    n: usize,
    v: void,

    fn new() @This() {
        return MyUnion { .v = {} };
    }
};

test "this should work at compiletime" {
    comptime {
        var myUnion = MyUnion.new();

        var f = myUnion.n;
    }
}

However, when running zig test, it errors:

error: accessing union field 'n' while field 'v' is set
        var f = myUnion.n;

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorstage1The process of building from source via WebAssembly and the C backend.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions