Skip to content

implement field alignment syntax for unions #3125

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

Open
andrewrk opened this issue Aug 27, 2019 · 1 comment
Open

implement field alignment syntax for unions #3125

andrewrk opened this issue Aug 27, 2019 · 1 comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Milestone

Comments

@andrewrk
Copy link
Member

Thanks to #3114 we now have syntax for specifying alignment on struct and union fields. Once #3115 is merged, the syntax for struct field alignment will work (#1512). However there is more work to do to implement support for this syntax for unions, and this issue is to track the implementation. Here's a test case:

const std = @import("std");
const assert = std.debug.assert;

const Number = union {
    int: u8 align(64),
    float: f64,
};

test "union field alignment syntax" {
    var num =  Number {.float  = 12.34};
    assert(num.float == 12.34);
    num = Number {.int = 100};
    assert(num.int == 100);
    assert(@typeOf(&num.int) == *align(64) u8);
    assert(@ptrToInt(&num.int) % 64 == 0);
}
@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. stage1 The process of building from source via WebAssembly and the C backend. labels Aug 27, 2019
@andrewrk andrewrk added this to the 0.6.0 milestone Aug 27, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Feb 11, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 14, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@hnakamur
Copy link
Contributor

I found another reproducer for this.
It is a union typedef in C like below:

typedef union
{
    uint64_t f1;
    uint8_t f2[0];
} my_union;

Please refer to https://github.com/hnakamur/zig-c-union-align-compile-error-repro for the full reproducer.

@Vexu Vexu removed the stage1 The process of building from source via WebAssembly and the C backend. label Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Projects
None yet
Development

No branches or pull requests

3 participants