Skip to content

Packed struct field alignment should be able to be derived from the alignment of the struct itself #12450

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

Closed
chr-1x opened this issue Aug 14, 2022 · 0 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

@chr-1x
Copy link

chr-1x commented Aug 14, 2022

Zig Version

0.10.0-dev.3559+d2342370f

Steps to Reproduce

const expect = @import("std").testing.expect;

const Foo = packed struct {
    a: i32,
    b: u8,
};

var buffer: [256]u8 = undefined;

test {
    var f1: *align(16) Foo = @alignCast(16, @ptrCast(*Foo, &buffer[0]));
    // Given: alignment of f1 is 16 (in this case, because we said it is, but
    // the aligned pointer could come from anywhere).
    try expect(@typeInfo(@TypeOf(f1)).Pointer.alignment == 16); // passes
    // Packed struct memory layout is guaranteed, and has no padding. So the
    // first field in the struct will have the same address as the struct.
    try expect(@ptrToInt(f1) == @ptrToInt(&f1.a)); // passes
    // Yet, the type of the address of the first field is *align(1) i32, not
    // *align(16) i32!
    try expect(@typeInfo(@TypeOf(&f1.a)).Pointer.alignment == 16); // fails
    // This would merely be an annoyance, but it makes atomic loads and stores
    // to these fields impossible without a cast, which shouldn't be necessary:
    //   @atomicStore(i32, &f1.a, 123, .Unordered);
    // error: expected type '*i32', found '*align(1) i32'
}

Expected Behavior

Taking the pointers of fields of a packed struct through an aligned pointer should give you aligned pointers to the fields.

Actual Behavior

All fields in a packed struct are *align(1), regardless if the pointer to the packed struct has a higher alignment.

@chr-1x chr-1x added the bug Observed behavior contradicts documented or intended behavior label Aug 14, 2022
@Vexu Vexu added the stage1 The process of building from source via WebAssembly and the C backend. label Aug 16, 2022
@Vexu Vexu added this to the 0.12.0 milestone Aug 16, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 15, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 15, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 15, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 16, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 17, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 18, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 18, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 21, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 27, 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

3 participants