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

Pointer alignment errors occurring inconsistently #23400

Open
GoldenStack opened this issue Mar 29, 2025 · 1 comment
Open

Pointer alignment errors occurring inconsistently #23400

GoldenStack opened this issue Mar 29, 2025 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@GoldenStack
Copy link

GoldenStack commented Mar 29, 2025

Zig Version

0.15.0-dev.149+2b57f6b71

Steps to Reproduce and Observed Behavior

I've come across a few inconsistencies with aligned fields in structs. Primarily, the following code produces error: struct layout depends on being pointer aligned (pointing to the definition of A):

pub fn main() !void {
    _ = struct { a: A }; // Line A
    _ = A; // Line B
}

pub const A = struct {
    b: struct {
        a: *align(@alignOf(struct { a: A })) void,
    },
    a: *A,
    c: struct {
        a: *align(@alignOf(struct { a: A })) void,
    },
    d: u128,
};

but if you swap the order of line A and line B, no such error occurs. Furthermore, commenting out line B makes it error once again, but an error is not produced with solely line B.

Finally, if a: *A is moved up as to become the first field, with just line B (so line A is commented out), the error occurs once again.

However, if struct { a: *align(@alignOf(struct { a: A })) void } is extracted to a separate variable (e.g. B) and is referred to by A (b: B, c: B), the error does not occur in any case that I have tested.

My best guess as to why this is occurring is that the alignment of the struct is being retrieved before the alignment has been correctly set (since A depends on the alignment of itself) so the comptime evaluation of the types of fields of A is inconsistently wrong, dependent on the order of how the expressions are evaluated.

Expected Behavior

To my understanding, none of this code should error, but at a minimum, reordering the two lines in main should not change whether or not there is a compile error.

@GoldenStack GoldenStack added the bug Observed behavior contradicts documented or intended behavior label Mar 29, 2025
@GoldenStack
Copy link
Author

This doesn't occur on 0.13.0 (as well as 0.12.0, 0.11.0, 0.10.0), and occurs on 0.14.0 and master, so I assume this is a regression and was introduced somewhere between 0.13.0 and 0.14.0. I haven't bisected individual commits though.

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
Projects
None yet
Development

No branches or pull requests

1 participant