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

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

Closed
DutchGhost opened this issue Aug 28, 2019 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@DutchGhost
Copy link

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;
@DutchGhost DutchGhost changed the title Packed/extern unions are checked within comptime blocks Packed/extern unions are checked within comptime blocks, while they shouldn't Aug 28, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone Aug 28, 2019
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Aug 28, 2019
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Sep 11, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Mar 12, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@Manuzor
Copy link
Contributor

Manuzor commented May 17, 2021

I also ran into this. The behavior is inconsistent between comptime and runtime.

const Foo = packed union {
    a: i8,
    b: u8,
};

test "runs fine" {
    var foo: Foo = .{ .a = -42 };
    try std.testing.expect(foo.b == 214);
}

test "does not compile" {
    const foo: Foo = .{ .a = -42 };
    // compile error: accessing union field 'b' while field 'a' is set
    try std.testing.expect(foo.b == 214);
}

zig version 0.8.0-dev.2275+8467373bb on Windows 10.0.19042.

@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@Vexu
Copy link
Member

Vexu commented Dec 28, 2022

Works in correctly in the self-hosted compiler:

git grep "extern union doesn't trigger field check at comptime" test
test/behavior/union.zig:test "extern union doesn't trigger field check at comptime" {

@Vexu Vexu closed this as completed Dec 28, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

4 participants