comptime

Zig places importance on the concept of whether an expression is known at compile-time. There are a few different places this concept is used, and these building blocks are used to keep the language small, readable, and powerful.

Introducing the Compile-Time Concept

Compile-Time Parameters

Compile-time parameters is how Zig implements generics. It is compile-time duck typing.

compile-time_duck_typing.zig

  1. fn max(comptime T: type, a: T, b: T) T {
  2. return if (a > b) a else b;
  3. }
  4. fn gimmeTheBiggerFloat(a: f32, b: f32) f32 {
  5. return max(f32, a, b);
  6. }
  7. fn gimmeTheBiggerInteger(a: u64, b: u64) u64 {
  8. return max(u64, a, b);
  9. }

In Zig, types are first-class citizens. They can be assigned to variables, passed as parameters to functions, and returned from functions. However, they can only be used in expressions which are known at compile-time, which is why the parameter T in the above snippet must be marked with comptime.

A comptime parameter means that:

  • At the callsite, the value must be known at compile-time, or it is a compile error.
  • In the function definition, the value is known at compile-time.

For example, if we were to introduce another function to the above snippet:

test_unresolved_comptime_value.zig

  1. fn max(comptime T: type, a: T, b: T) T {
  2. return if (a > b) a else b;
  3. }
  4. test "try to pass a runtime type" {
  5. foo(false);
  6. }
  7. fn foo(condition: bool) void {
  8. const result = max(
  9. if (condition) f32 else u64,
  10. 1234,
  11. 5678);
  12. _ = result;
  13. }

Shell

  1. $ zig test test_unresolved_comptime_value.zig
  2. docgen_tmp/test_unresolved_comptime_value.zig:9:13: error: unable to resolve comptime value
  3. if (condition) f32 else u64,
  4. ^~~~~~~~~
  5. docgen_tmp/test_unresolved_comptime_value.zig:9:13: note: condition in comptime branch must be comptime-known
  6. referenced by:
  7. test.try to pass a runtime type: docgen_tmp/test_unresolved_comptime_value.zig:5:5
  8. remaining reference traces hidden; use '-freference-trace' to see all reference traces

This is an error because the programmer attempted to pass a value only known at run-time to a function which expects a value known at compile-time.

Another way to get an error is if we pass a type that violates the type checker when the function is analyzed. This is what it means to have compile-time duck typing.

For example:

test_comptime_mismatched_type.zig

  1. fn max(comptime T: type, a: T, b: T) T {
  2. return if (a > b) a else b;
  3. }
  4. test "try to compare bools" {
  5. _ = max(bool, true, false);
  6. }

Shell

  1. $ zig test test_comptime_mismatched_type.zig
  2. docgen_tmp/test_comptime_mismatched_type.zig:2:18: error: operator > not allowed for type 'bool'
  3. return if (a > b) a else b;
  4. ~~^~~
  5. referenced by:
  6. test.try to compare bools: docgen_tmp/test_comptime_mismatched_type.zig:5:12
  7. remaining reference traces hidden; use '-freference-trace' to see all reference traces

On the flip side, inside the function definition with the comptime parameter, the value is known at compile-time. This means that we actually could make this work for the bool type if we wanted to:

test_comptime_max_with_bool.zig

  1. fn max(comptime T: type, a: T, b: T) T {
  2. if (T == bool) {
  3. return a or b;
  4. } else if (a > b) {
  5. return a;
  6. } else {
  7. return b;
  8. }
  9. }
  10. test "try to compare bools" {
  11. try @import("std").testing.expect(max(bool, false, true) == true);
  12. }

Shell

  1. $ zig test test_comptime_max_with_bool.zig
  2. 1/1 test_comptime_max_with_bool.test.try to compare bools... OK
  3. All 1 tests passed.

This works because Zig implicitly inlines if expressions when the condition is known at compile-time, and the compiler guarantees that it will skip analysis of the branch not taken.

This means that the actual function generated for max in this situation looks like this:

compiler_generated_function.zig

  1. fn max(a: bool, b: bool) bool {
  2. {
  3. return a or b;
  4. }
  5. }

All the code that dealt with compile-time known values is eliminated and we are left with only the necessary run-time code to accomplish the task.

This works the same way for switch expressions - they are implicitly inlined when the target expression is compile-time known.

Compile-Time Variables

In Zig, the programmer can label variables as comptime. This guarantees to the compiler that every load and store of the variable is performed at compile-time. Any violation of this results in a compile error.

This combined with the fact that we can inline loops allows us to write a function which is partially evaluated at compile-time and partially at run-time.

For example:

test_comptime_evaluation.zig

  1. const expect = @import("std").testing.expect;
  2. const CmdFn = struct {
  3. name: []const u8,
  4. func: fn(i32) i32,
  5. };
  6. const cmd_fns = [_]CmdFn{
  7. CmdFn {.name = "one", .func = one},
  8. CmdFn {.name = "two", .func = two},
  9. CmdFn {.name = "three", .func = three},
  10. };
  11. fn one(value: i32) i32 { return value + 1; }
  12. fn two(value: i32) i32 { return value + 2; }
  13. fn three(value: i32) i32 { return value + 3; }
  14. fn performFn(comptime prefix_char: u8, start_value: i32) i32 {
  15. var result: i32 = start_value;
  16. comptime var i = 0;
  17. inline while (i < cmd_fns.len) : (i += 1) {
  18. if (cmd_fns[i].name[0] == prefix_char) {
  19. result = cmd_fns[i].func(result);
  20. }
  21. }
  22. return result;
  23. }
  24. test "perform fn" {
  25. try expect(performFn('t', 1) == 6);
  26. try expect(performFn('o', 0) == 1);
  27. try expect(performFn('w', 99) == 99);
  28. }

Shell

  1. $ zig test test_comptime_evaluation.zig
  2. 1/1 test_comptime_evaluation.test.perform fn... OK
  3. All 1 tests passed.

This example is a bit contrived, because the compile-time evaluation component is unnecessary; this code would work fine if it was all done at run-time. But it does end up generating different code. In this example, the function performFn is generated three different times, for the different values of prefix_char provided:

performFn_1

  1. // From the line:
  2. // expect(performFn('t', 1) == 6);
  3. fn performFn(start_value: i32) i32 {
  4. var result: i32 = start_value;
  5. result = two(result);
  6. result = three(result);
  7. return result;
  8. }

performFn_2

  1. // From the line:
  2. // expect(performFn('o', 0) == 1);
  3. fn performFn(start_value: i32) i32 {
  4. var result: i32 = start_value;
  5. result = one(result);
  6. return result;
  7. }

performFn_3

  1. // From the line:
  2. // expect(performFn('w', 99) == 99);
  3. fn performFn(start_value: i32) i32 {
  4. var result: i32 = start_value;
  5. _ = &result;
  6. return result;
  7. }

Note that this happens even in a debug build. This is not a way to write more optimized code, but it is a way to make sure that what should happen at compile-time, does happen at compile-time. This catches more errors and allows expressiveness that in other languages requires using macros, generated code, or a preprocessor to accomplish.

Compile-Time Expressions

In Zig, it matters whether a given expression is known at compile-time or run-time. A programmer can use a comptime expression to guarantee that the expression will be evaluated at compile-time. If this cannot be accomplished, the compiler will emit an error. For example:

test_comptime_call_extern_function.zig

  1. extern fn exit() noreturn;
  2. test "foo" {
  3. comptime {
  4. exit();
  5. }
  6. }

Shell

  1. $ zig test test_comptime_call_extern_function.zig
  2. docgen_tmp/test_comptime_call_extern_function.zig:5:13: error: comptime call of extern function
  3. exit();
  4. ~~~~^~

It doesn’t make sense that a program could call exit() (or any other external function) at compile-time, so this is a compile error. However, a comptime expression does much more than sometimes cause a compile error.

Within a comptime expression:

  • All variables are comptime variables.
  • All if, while, for, and switch expressions are evaluated at compile-time, or emit a compile error if this is not possible.
  • All return and try expressions are invalid (unless the function itself is called at compile-time).
  • All code with runtime side effects or depending on runtime values emits a compile error.
  • All function calls cause the compiler to interpret the function at compile-time, emitting a compile error if the function tries to do something that has global runtime side effects.

This means that a programmer can create a function which is called both at compile-time and run-time, with no modification to the function required.

Let’s look at an example:

test_fibonacci_recursion.zig

  1. const expect = @import("std").testing.expect;
  2. fn fibonacci(index: u32) u32 {
  3. if (index < 2) return index;
  4. return fibonacci(index - 1) + fibonacci(index - 2);
  5. }
  6. test "fibonacci" {
  7. // test fibonacci at run-time
  8. try expect(fibonacci(7) == 13);
  9. // test fibonacci at compile-time
  10. try comptime expect(fibonacci(7) == 13);
  11. }

Shell

  1. $ zig test test_fibonacci_recursion.zig
  2. 1/1 test_fibonacci_recursion.test.fibonacci... OK
  3. All 1 tests passed.

Imagine if we had forgotten the base case of the recursive function and tried to run the tests:

test_fibonacci_comptime_overflow.zig

  1. const expect = @import("std").testing.expect;
  2. fn fibonacci(index: u32) u32 {
  3. //if (index < 2) return index;
  4. return fibonacci(index - 1) + fibonacci(index - 2);
  5. }
  6. test "fibonacci" {
  7. try comptime expect(fibonacci(7) == 13);
  8. }

Shell

  1. $ zig test test_fibonacci_comptime_overflow.zig
  2. docgen_tmp/test_fibonacci_comptime_overflow.zig:5:28: error: overflow of integer type 'u32' with value '-1'
  3. return fibonacci(index - 1) + fibonacci(index - 2);
  4. ~~~~~~^~~
  5. docgen_tmp/test_fibonacci_comptime_overflow.zig:5:21: note: called from here (7 times)
  6. return fibonacci(index - 1) + fibonacci(index - 2);
  7. ~~~~~~~~~^~~~~~~~~~~
  8. docgen_tmp/test_fibonacci_comptime_overflow.zig:9:34: note: called from here
  9. try comptime expect(fibonacci(7) == 13);
  10. ~~~~~~~~~^~~

The compiler produces an error which is a stack trace from trying to evaluate the function at compile-time.

Luckily, we used an unsigned integer, and so when we tried to subtract 1 from 0, it triggered undefined behavior, which is always a compile error if the compiler knows it happened. But what would have happened if we used a signed integer?

fibonacci_comptime_infinite_recursion.zig

  1. const assert = @import("std").debug.assert;
  2. fn fibonacci(index: i32) i32 {
  3. //if (index < 2) return index;
  4. return fibonacci(index - 1) + fibonacci(index - 2);
  5. }
  6. test "fibonacci" {
  7. try comptime assert(fibonacci(7) == 13);
  8. }

The compiler is supposed to notice that evaluating this function at compile-time took more than 1000 branches, and thus emits an error and gives up. If the programmer wants to increase the budget for compile-time computation, they can use a built-in function called @setEvalBranchQuota to change the default number 1000 to something else.

However, there is a design flaw in the compiler causing it to stack overflow instead of having the proper behavior here. I’m terribly sorry about that. I hope to get this resolved before the next release.

What if we fix the base case, but put the wrong value in the expect line?

test_fibonacci_comptime_unreachable.zig

  1. const assert = @import("std").debug.assert;
  2. fn fibonacci(index: i32) i32 {
  3. if (index < 2) return index;
  4. return fibonacci(index - 1) + fibonacci(index - 2);
  5. }
  6. test "fibonacci" {
  7. try comptime assert(fibonacci(7) == 99999);
  8. }

Shell

  1. $ zig test test_fibonacci_comptime_unreachable.zig
  2. /home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/debug.zig:403:14: error: reached unreachable code
  3. if (!ok) unreachable; // assertion failure
  4. ^~~~~~~~~~~
  5. docgen_tmp/test_fibonacci_comptime_unreachable.zig:9:24: note: called from here
  6. try comptime assert(fibonacci(7) == 99999);
  7. ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

At container level (outside of any function), all expressions are implicitly comptime expressions. This means that we can use functions to initialize complex static data. For example:

test_container-level_comptime_expressions.zig

  1. const first_25_primes = firstNPrimes(25);
  2. const sum_of_first_25_primes = sum(&first_25_primes);
  3. fn firstNPrimes(comptime n: usize) [n]i32 {
  4. var prime_list: [n]i32 = undefined;
  5. var next_index: usize = 0;
  6. var test_number: i32 = 2;
  7. while (next_index < prime_list.len) : (test_number += 1) {
  8. var test_prime_index: usize = 0;
  9. var is_prime = true;
  10. while (test_prime_index < next_index) : (test_prime_index += 1) {
  11. if (test_number % prime_list[test_prime_index] == 0) {
  12. is_prime = false;
  13. break;
  14. }
  15. }
  16. if (is_prime) {
  17. prime_list[next_index] = test_number;
  18. next_index += 1;
  19. }
  20. }
  21. return prime_list;
  22. }
  23. fn sum(numbers: []const i32) i32 {
  24. var result: i32 = 0;
  25. for (numbers) |x| {
  26. result += x;
  27. }
  28. return result;
  29. }
  30. test "variable values" {
  31. try @import("std").testing.expect(sum_of_first_25_primes == 1060);
  32. }

Shell

  1. $ zig test test_container-level_comptime_expressions.zig
  2. 1/1 test_container-level_comptime_expressions.test.variable values... OK
  3. All 1 tests passed.

When we compile this program, Zig generates the constants with the answer pre-computed. Here are the lines from the generated LLVM IR:

  1. @0 = internal unnamed_addr constant [25 x i32] [i32 2, i32 3, i32 5, i32 7, i32 11, i32 13, i32 17, i32 19, i32 23, i32 29, i32 31, i32 37, i32 41, i32 43, i32 47, i32 53, i32 59, i32 61, i32 67, i32 71, i32 73, i32 79, i32 83, i32 89, i32 97]
  2. @1 = internal unnamed_addr constant i32 1060

Note that we did not have to do anything special with the syntax of these functions. For example, we could call the sum function as is with a slice of numbers whose length and values were only known at run-time.

Generic Data Structures

Zig uses comptime capabilities to implement generic data structures without introducing any special-case syntax.

Here is an example of a generic List data structure.

generic_data_structure.zig

  1. fn List(comptime T: type) type {
  2. return struct {
  3. items: []T,
  4. len: usize,
  5. };
  6. }
  7. // The generic List data structure can be instantiated by passing in a type:
  8. var buffer: [10]i32 = undefined;
  9. var list = List(i32){
  10. .items = &buffer,
  11. .len = 0,
  12. };

That’s it. It’s a function that returns an anonymous struct. For the purposes of error messages and debugging, Zig infers the name "List(i32)" from the function name and parameters invoked when creating the anonymous struct.

To explicitly give a type a name, we assign it to a constant.

anonymous_struct_name.zig

  1. const Node = struct {
  2. next: ?*Node,
  3. name: []const u8,
  4. };
  5. var node_a = Node{
  6. .next = null,
  7. .name = "Node A",
  8. };
  9. var node_b = Node{
  10. .next = &node_a,
  11. .name = "Node B",
  12. };

In this example, the Node struct refers to itself. This works because all top level declarations are order-independent. As long as the compiler can determine the size of the struct, it is free to refer to itself. In this case, Node refers to itself as a pointer, which has a well-defined size at compile time, so it works fine.

Case Study: print in Zig

Putting all of this together, let’s see how print works in Zig.

print.zig

  1. const print = @import("std").debug.print;
  2. const a_number: i32 = 1234;
  3. const a_string = "foobar";
  4. pub fn main() void {
  5. print("here is a string: '{s}' here is a number: {}\n", .{a_string, a_number});
  6. }

Shell

  1. $ zig build-exe print.zig
  2. $ ./print
  3. here is a string: 'foobar' here is a number: 1234

Let’s crack open the implementation of this and see how it works:

poc_print_fn.zig

  1. const Writer = struct {
  2. /// Calls print and then flushes the buffer.
  3. pub fn print(self: *Writer, comptime format: []const u8, args: anytype) anyerror!void {
  4. const State = enum {
  5. start,
  6. open_brace,
  7. close_brace,
  8. };
  9. comptime var start_index: usize = 0;
  10. comptime var state = State.start;
  11. comptime var next_arg: usize = 0;
  12. inline for (format, 0..) |c, i| {
  13. switch (state) {
  14. State.start => switch (c) {
  15. '{' => {
  16. if (start_index < i) try self.write(format[start_index..i]);
  17. state = State.open_brace;
  18. },
  19. '}' => {
  20. if (start_index < i) try self.write(format[start_index..i]);
  21. state = State.close_brace;
  22. },
  23. else => {},
  24. },
  25. State.open_brace => switch (c) {
  26. '{' => {
  27. state = State.start;
  28. start_index = i;
  29. },
  30. '}' => {
  31. try self.printValue(args[next_arg]);
  32. next_arg += 1;
  33. state = State.start;
  34. start_index = i + 1;
  35. },
  36. 's' => {
  37. continue;
  38. },
  39. else => @compileError("Unknown format character: " ++ [1]u8{c}),
  40. },
  41. State.close_brace => switch (c) {
  42. '}' => {
  43. state = State.start;
  44. start_index = i;
  45. },
  46. else => @compileError("Single '}' encountered in format string"),
  47. },
  48. }
  49. }
  50. comptime {
  51. if (args.len != next_arg) {
  52. @compileError("Unused arguments");
  53. }
  54. if (state != State.start) {
  55. @compileError("Incomplete format string: " ++ format);
  56. }
  57. }
  58. if (start_index < format.len) {
  59. try self.write(format[start_index..format.len]);
  60. }
  61. try self.flush();
  62. }
  63. fn write(self: *Writer, value: []const u8) !void {
  64. _ = self;
  65. _ = value;
  66. }
  67. pub fn printValue(self: *Writer, value: anytype) !void {
  68. _ = self;
  69. _ = value;
  70. }
  71. fn flush(self: *Writer) !void {
  72. _ = self;
  73. }
  74. };

This is a proof of concept implementation; the actual function in the standard library has more formatting capabilities.

Note that this is not hard-coded into the Zig compiler; this is userland code in the standard library.

When this function is analyzed from our example code above, Zig partially evaluates the function and emits a function that actually looks like this:

Emitted print Function

  1. pub fn print(self: *Writer, arg0: []const u8, arg1: i32) !void {
  2. try self.write("here is a string: '");
  3. try self.printValue(arg0);
  4. try self.write("' here is a number: ");
  5. try self.printValue(arg1);
  6. try self.write("\n");
  7. try self.flush();
  8. }

printValue is a function that takes a parameter of any type, and does different things depending on the type:

poc_printValue_fn.zig

  1. const Writer = struct {
  2. pub fn printValue(self: *Writer, value: anytype) !void {
  3. switch (@typeInfo(@TypeOf(value))) {
  4. .Int => {
  5. return self.writeInt(value);
  6. },
  7. .Float => {
  8. return self.writeFloat(value);
  9. },
  10. .Pointer => {
  11. return self.write(value);
  12. },
  13. else => {
  14. @compileError("Unable to print type '" ++ @typeName(@TypeOf(value)) ++ "'");
  15. },
  16. }
  17. }
  18. fn write(self: *Writer, value: []const u8) !void {
  19. _ = self;
  20. _ = value;
  21. }
  22. fn writeInt(self: *Writer, value: anytype) !void {
  23. _ = self;
  24. _ = value;
  25. }
  26. fn writeFloat(self: *Writer, value: anytype) !void {
  27. _ = self;
  28. _ = value;
  29. }
  30. };

And now, what happens if we give too many arguments to print?

test_print_too_many_args.zig

  1. const print = @import("std").debug.print;
  2. const a_number: i32 = 1234;
  3. const a_string = "foobar";
  4. test "print too many arguments" {
  5. print("here is a string: '{s}' here is a number: {}\n", .{
  6. a_string,
  7. a_number,
  8. a_number,
  9. });
  10. }

Shell

  1. $ zig test test_print_too_many_args.zig
  2. /home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/fmt.zig:203:18: error: unused argument in 'here is a string: '{s}' here is a number: {}
  3. '
  4. 1 => @compileError("unused argument in '" ++ fmt ++ "'"),
  5. ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. referenced by:
  7. print__anon_2564: /home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/io/Writer.zig:23:26
  8. print: /home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/io.zig:324:47
  9. remaining reference traces hidden; use '-freference-trace' to see all reference traces

Zig gives programmers the tools needed to protect themselves against their own mistakes.

Zig doesn’t care whether the format argument is a string literal, only that it is a compile-time known value that can be coerced to a []const u8:

print_comptime-known_format.zig

  1. const print = @import("std").debug.print;
  2. const a_number: i32 = 1234;
  3. const a_string = "foobar";
  4. const fmt = "here is a string: '{s}' here is a number: {}\n";
  5. pub fn main() void {
  6. print(fmt, .{a_string, a_number});
  7. }

Shell

  1. $ zig build-exe print_comptime-known_format.zig
  2. $ ./print_comptime-known_format
  3. here is a string: 'foobar' here is a number: 1234

This works fine.

Zig does not special case string formatting in the compiler and instead exposes enough power to accomplish this task in userland. It does so without introducing another language on top of Zig, such as a macro language or a preprocessor language. It’s Zig all the way down.

See also: