Skip to content

Commit e82feab

Browse files
committed
langref: improve zero bit types documentation
In the Zero Bit Types section, clarify that a non empty zero bit union just needs to have all fields of zero bit type, instead of only one field of zero bit type. Add a doctest. Clarify that empty struct, enum, union and error set are currently problematic.
1 parent fe6dcdb commit e82feab

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

doc/langref.html.in

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6627,8 +6627,36 @@ test "peer type resolution: *const T and ?*T" {
66276627
<li>{#link|Arrays#} and {#link|Vectors#} with len 0, or with an element type that is a zero bit type.</li>
66286628
<li>An {#link|enum#} with only 1 tag.</li>
66296629
<li>A {#link|struct#} with all fields being zero bit types.</li>
6630-
<li>A {#link|union#} with only 1 field which is a zero bit type.</li>
6630+
<li>An {#link|union#} with all fields being zero bit types.</li>
66316631
</ul>
6632+
{#code_begin|test|test_zero_bit_types#}
6633+
const std = @import("std");
6634+
const testing = std.testing;
6635+
6636+
test "zero-bit primitive types" {
6637+
inline for (.{
6638+
void,
6639+
u0,
6640+
i0,
6641+
}) |typ| {
6642+
try testing.expect(@sizeOf(typ) == 0);
6643+
}
6644+
}
6645+
6646+
test "zero-bit types" {
6647+
inline for (.{
6648+
[0]u32,
6649+
[10]u0,
6650+
@Vector(0, u32),
6651+
@Vector(10, u0),
6652+
enum { empty },
6653+
struct { a: u0, b: void },
6654+
union { a: u0, b: void },
6655+
}) |typ| {
6656+
try testing.expect(@sizeOf(typ) == 0);
6657+
}
6658+
}
6659+
{#code_end#}
66326660
<p>
66336661
These types can only ever have one possible value, and thus
66346662
require 0 bits to represent. Code that makes use of these types is
@@ -6650,6 +6678,9 @@ export fn entry() void {
66506678
15: c3 retq </code></pre>
66516679
<p>These assembly instructions do not have any code associated with the void values -
66526680
they only perform the function call prologue and epilogue.</p>
6681+
<p>Using empty {#link|struct#}, empty {#link|enum#}, empty {#link|union#},
6682+
and empty {#link|Error Set Type#} is problematic, and may be a compile error in the future.
6683+
For details on this subscribe to <a href="https://github.com/ziglang/zig/issues/13807">this issue</a>.
66536684

66546685
{#header_open|void#}
66556686
<p>

0 commit comments

Comments
 (0)