Skip to content

Proposal: Allow aligned zero-size pointers #6954

@SpexGuy

Description

@SpexGuy

Zero-sized pointers are really inconsistent about alignment.

// compiles
[]void, []align(0) void, []align(4) void, []align(@alignOf(void)) void
comptime { const x = @alignOf(void); }

// doesn't compile
var x: u29 = @alignOf(void);
var y: u29 = @typeInfo([]void).Pointer.alignment;
[]align(@compileError("")) void

fn slice(comptime ment: u29) type { return []align(ment) void; }
slice(@alignOf(void));

// crashes compiler
_ = ([]align(2) void == []align(4) void);

I see two ways to resolve this:

  • never allow alignment on zero-sized types
  • define default alignment on zero-sized types and allow it always

Never allowing alignment causes a lot of special cases for zero sized types. So I think we should standardize alignment. In all places, we have the invariant that @alignOf(T) <= @sizeOf(T). This means the only valid alignment for zero-sized types is 0. Which kind of makes sense, you could argue that they are bit packed. Defining the alignment this way isn't perfect but definitely cuts down on the number of special cases with zero-sized types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions