Zero Bit Types

For some types, @sizeOf is 0:

These types can only ever have one possible value, and thus require 0 bits to represent. Code that makes use of these types is not included in the final generated code:

  1. export fn entry() void {
  2. var x: void = {};
  3. var y: void = {};
  4. x = y;
  5. }

When this turns into machine code, there is no code generated in the body of entry, even in Debug mode. For example, on x86_64:

  1. 0000000000000010 <entry>:
  2. 10: 55 push %rbp
  3. 11: 48 89 e5 mov %rsp,%rbp
  4. 14: 5d pop %rbp
  5. 15: c3 retq

These assembly instructions do not have any code associated with the void values - they only perform the function call prologue and epilog.

void

void can be useful for instantiating generic types. For example, given a Map(Key, Value), one can pass void for the Value type to make it into a Set:

test.zig

  1. const std = @import("std");
  2. const assert = std.debug.assert;
  3. test "turn HashMap into a set with void" {
  4. var map = std.HashMap(i32, void, hash_i32, eql_i32).init(std.testing.allocator);
  5. defer map.deinit();
  6. _ = try map.put(1, {});
  7. _ = try map.put(2, {});
  8. assert(map.contains(2));
  9. assert(!map.contains(3));
  10. _ = map.remove(2);
  11. assert(!map.contains(2));
  12. }
  13. fn hash_i32(x: i32) u32 {
  14. return @bitCast(u32, x);
  15. }
  16. fn eql_i32(a: i32, b: i32) bool {
  17. return a == b;
  18. }
  1. $ zig test test.zig
  2. 1/1 test "turn HashMap into a set with void"...OK
  3. All 1 tests passed.

Note that this is different from using a dummy value for the hash map value. By using void as the type of the value, the hash map entry type has no value field, and thus the hash map takes up less space. Further, all the code that deals with storing and loading the value is deleted, as seen above.

void is distinct from c_void, which is defined like this: pub const c_void = @OpaqueType();. void has a known size of 0 bytes, and c_void has an unknown, but non-zero, size.

Expressions of type void are the only ones whose value can be ignored. For example:

test.zig

  1. test "ignoring expression value" {
  2. foo();
  3. }
  4. fn foo() i32 {
  5. return 1234;
  6. }
  1. $ zig test test.zig
  2. ./docgen_tmp/test.zig:2:8: error: expression value is ignored
  3. foo();
  4. ^
  5. ./docgen_tmp/test.zig:1:34: note: referenced here
  6. test "ignoring expression value" {
  7. ^

However, if the expression has type void, there will be no error. Function return values can also be explicitly ignored by assigning them to _.

test.zig

  1. test "void is ignored" {
  2. returnsVoid();
  3. }
  4. test "explicitly ignoring expression value" {
  5. _ = foo();
  6. }
  7. fn returnsVoid() void {}
  8. fn foo() i32 {
  9. return 1234;
  10. }
  1. $ zig test test.zig
  2. 1/2 test "void is ignored"...OK
  3. 2/2 test "explicitly ignoring expression value"...OK
  4. All 2 tests passed.

Pointers to Zero Bit Types

Pointers to zero bit types also have zero bits. They always compare equal to each other:

test.zig

  1. const std = @import("std");
  2. const assert = std.debug.assert;
  3. test "pointer to empty struct" {
  4. const Empty = struct {};
  5. var a = Empty{};
  6. var b = Empty{};
  7. var ptr_a = &a;
  8. var ptr_b = &b;
  9. comptime assert(ptr_a == ptr_b);
  10. }
  1. $ zig test test.zig
  2. 1/1 test "pointer to empty struct"...OK
  3. All 1 tests passed.

The type being pointed to can only ever be one value; therefore loads and stores are never generated. ptrToInt and intToPtr are not allowed:

test.zig

  1. const Empty = struct {};
  2. test "@ptrToInt for pointer to zero bit type" {
  3. var a = Empty{};
  4. _ = @ptrToInt(&a);
  5. }
  6. test "@intToPtr for pointer to zero bit type" {
  7. _ = @intToPtr(*Empty, 0x1);
  8. }
  1. $ zig test test.zig
  2. ./docgen_tmp/test.zig:4:5: error: pointer to size 0 type has no address
  3. var a = Empty{};
  4. ^
  5. ./docgen_tmp/test.zig:5:9: note: referenced here
  6. _ = @ptrToInt(&a);
  7. ^
  8. ./docgen_tmp/test.zig:9:19: error: type '*Empty' has 0 bits and cannot store information
  9. _ = @intToPtr(*Empty, 0x1);
  10. ^
  11. ./docgen_tmp/test.zig:9:9: note: referenced here
  12. _ = @intToPtr(*Empty, 0x1);
  13. ^