permission for opening this proposal given by @mlugg
Preamble
As of Zig 0.16, @alignOf produces a compilation error when given a vacuous type (e.g. noreturn). The language, however, certainly does have a notion of alignment for these types, as one can create a type const S = struct { foo: u16, bar: noreturn };, create an undefined pointer of type *const S, and then try to cast it to *align(1) const S, *align(2) const S, and *align(4) const S (the first two casts succeeding, the third failing).
Proposal
Make @alignOf a total function, returning 1 for vacuous or comptime-only types, and the expected maximum-of-all-fields for (non-packed) structs and unions.
Behaviour Cases
test{constexpectEqual=std.testing.expectEqual;constA=struct{x:u8,y:noreturn,};constB=struct{a:A,b:u16,};constC=union(enum){b:B,c:Aalign(8),};constD=union{};tryexpectEqual(1,@alignOf(noreturn));tryexpectEqual(1,@alignOf(A));tryexpectEqual(2,@alignOf(B));tryexpectEqual(8,@alignOf(C));tryexpectEqual(8,@alignOf([10]C));tryexpectEqual(1,@alignOf(D));}
permission for opening this proposal given by @mlugg
## Preamble
As of Zig 0.16, `@alignOf` produces a compilation error when given a vacuous type (e.g. `noreturn`). The language, however, certainly does have a notion of alignment for these types, as one can create a type `const S = struct { foo: u16, bar: noreturn };`, create an `undefined` pointer of type `*const S`, and then try to cast it to `*align(1) const S`, `*align(2) const S`, and `*align(4) const S` (the first two casts succeeding, the third failing).
## Proposal
Make `@alignOf` a total function, returning `1` for vacuous or `comptime`-only types, and the expected maximum-of-all-fields for (non-`packed`) `struct`s and `union`s.
## Behaviour Cases
```zig
test {
const expectEqual = std.testing.expectEqual;
const A = struct {
x: u8,
y: noreturn,
};
const B = struct {
a: A,
b: u16,
};
const C = union(enum) {
b: B,
c: A align(8),
};
const D = union {};
try expectEqual(1, @alignOf(noreturn));
try expectEqual(1, @alignOf(A));
try expectEqual(2, @alignOf(B));
try expectEqual(8, @alignOf(C));
try expectEqual(8, @alignOf([10]C));
try expectEqual(1, @alignOf(D));
}
```