Skip to content

stage2: @sizeOf packed struct is not working as in stage1 #12565

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
igor84 opened this issue Aug 21, 2022 · 4 comments
Closed

stage2: @sizeOf packed struct is not working as in stage1 #12565

igor84 opened this issue Aug 21, 2022 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@igor84
Copy link
Contributor

igor84 commented Aug 21, 2022

Zig Version

0.10.0-dev.3664+02070ae26

Steps to Reproduce

const Rgb24 = packed struct {
    r: u8,
    g: u8,
    b: u8,
};

test {
    try std.testing.expectEqual(@as(usize, 3), @sizeOf(Rgb24));
}

Expected Behavior

The above test should pass.

Some time ago I fixed issues with packed struct in stage1 so that @sizeOf(Rgb24) would return 3 and if I have pixels: []Rgb24 I could use reader.readAll(std.mem.sliceAsBytes(pixels)) to load tightly packed Rgb24 data.

Actual Behavior

Now in stage2 @sizeOf(Rgb24) returns 4 and the array also isn't tightly packed but has additional byte of padding for each element which breaks readAll code.

@igor84 igor84 added the bug Observed behavior contradicts documented or intended behavior label Aug 21, 2022
@nektro
Copy link
Contributor

nektro commented Aug 21, 2022

the issue still stands, but for your mentioned use case you likely want extern struct not packed

@Vexu
Copy link
Member

Vexu commented Aug 21, 2022

Working as intended, the self-hosted compiler implements #5049

@Vexu Vexu closed this as not planned Won't fix, can't repro, duplicate, stale Aug 21, 2022
@igor84
Copy link
Contributor Author

igor84 commented Aug 21, 2022

@Vexu As far as I know stage1 doesn't compile with packed struct(u24) so is there a way to write code so it works with both stage1 and stage2?

@nektro I would say I need packed because I also have Rgb565{u5, u6, u5} for example and extern will not work for that.

@Vexu
Copy link
Member

Vexu commented Aug 21, 2022

As far as I know stage1 doesn't compile with packed struct(u24) so is there a way to write code so it works with both stage1 and stage2?

You can use @import("builtin").zig_backend to check what you are using but I recommend sticking to either stage1 or stage2.

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

3 participants