-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
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.