Skip to content

Offset/alignment for 0 bit types #4016

Open

Description

@daurnimator

e.g. If I have a packed struct with comptime parametized types, I don't want to have things not work when a field is passed as void/u0/struct{}. Instead, the offset should be right where it is (note that this differs from C where a 0-sized field aligns to the next byte boundary)

    const a = packed struct {
        foo: i32,
        bar: u0,
        baz: i32,
    };
    assert(@bitOffsetOf(a, "bar") == @bitOffsetOf(a, "baz"));

Another use case for this is helping to create structs with trailing data:

const mystruct = packed struct {
    foo: X,
    bar: Y,
    trailing_data:  @alignOf(Z) void,
}

const slice = try allocator.alignedAlloc(u8, @alignOf(mystruct), @sizeOf(mystruct) + extra_size);
const a = @ptrCast(*mystruct, slice.ptr);
const trailing_data = @ptrCast([*]Z, &a.trailing_data);

Activity

  1. added
    proposal
    on Dec 31, 2019
  2. added this to the 0.7.0 milestone on Dec 31, 2019
  3. modified the milestones: 0.7.0, 0.8.0 on Oct 27, 2020
  4. added
    accepted
    on Nov 21, 2020
  5. SpexGuy commented on Nov 21, 2020

    @SpexGuy
    Contributor

    With the decision in #6706, pointers to zero-sized values are now real pointers. This also means that offset and alignment will behave as described in this issue.

  6. modified the milestones: 0.8.0, 0.9.0 on May 19, 2021
  7. modified the milestones: 0.9.0, 0.10.0 on Nov 20, 2021
  8. andrewrk commented on Aug 24, 2022

    @andrewrk
    Member

    I believe this is fixed now and only task left is to ensure there is behavior test coverage.

  9. modified the milestones: 0.10.0, 0.10.1 on Aug 24, 2022
  10. modified the milestones: 0.10.1, 0.11.0 on Jan 10, 2023
  11. modified the milestones: 0.11.0, 0.12.0 on Apr 9, 2023
  12. modified the milestones: 0.13.0, 0.12.0 on Jun 29, 2023
  13. modified the milestones: 0.14.0, 0.15.0 on Jan 25, 2025
  14. added
    contributor friendly
    on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedcontributor friendlyproposal

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions