Skip to content

Zig Errors Explained (start here)

Got an error from the Zig compiler and not sure what it wants? You’re in the right place. Zig is strict: it has no warnings, only errors, and it checks things other languages let slide, like unused names, a var that never changes, or an error you forgot to handle. So you’ll see a lot of messages in your first weeks. The good news is that a small set of them covers most of what beginners hit, and each has a fix you only need to learn once.

Each page below takes one real Zig 0.17.1 message, shows the smallest program that causes it, explains it in plain words, and gives fixes you can run, most likely fix first.

What the compiler says In plain words
unused local variable You made a name and never used it (also unused local constant)
local variable is never mutated A var never changes: use const, or add the update you forgot
variable of type ‘comptime_int’ must be const or comptime var x = 0; needs a type: var x: u32 = 0;
cannot assign to constant You changed a const, a parameter, a loop capture or a string literal
unused function parameter A function never reads one of its inputs
pointless discard of local variable A leftover _ = x; for a name you now use
use of undeclared identifier ‘x’ A typo, a missing @import, or a name used outside its block
expected type ‘X’, found ‘Y’ A value of the wrong type: often []const u8, !T, ?T or integer sizes
error union of type ‘X’ is ignored A call can fail, and you didn’t use try, catch or if
expected error union type, found ‘X’ try on something that can’t fail, or on an optional
no field or member function named ‘x’ in ‘T’ The method doesn’t exist on that type (like push instead of append)
root source file struct ‘X’ has no member named ‘Y’ Code for an older Zig (std.io, std.fs.cwd), a typo, or a missing pub
expected N argument(s), found M Wrong number of arguments, often a missing .{} or allocator
missing struct field: x A struct literal left out a field with no default
switch must handle all possibilities A switch is missing a case or an else
unable to resolve comptime value A runtime value where Zig needs one known while compiling

These aren’t compile errors: the program builds, then crashes while running. Debug builds check for them and print thread <id> panic: with a stack trace.

What the program says In plain words
panic: integer overflow A number went past its type’s range, often len - 1 on an empty list
panic: index out of bounds: index N, len M You read past the end of an array or slice
panic: attempt to use null value A .? met a null

Here’s a small program with a mistake in it:

const std = @import("std");
pub fn main() void {
var total = 0;
const prices = [_]u32{ 3, 4 };
for (prices) |p| total += p;
std.debug.print("total: {d}\n", .{total});
}
main.zig:4:9: error: variable of type 'comptime_int' must be const or comptime
var total = 0;
^~~~~
main.zig:4:9: note: to modify this variable at runtime, it must be given an explicit fixed-size number type
referenced by:
callMain [inlined]: .../lib/std/start.zig:788:64
callMainWithArgs [inlined]: .../lib/std/start.zig:729:20
posixCallMainAndExit: .../lib/std/start.zig:681:38
2 reference(s) hidden; use '-freference-trace=5' to see all references

Read it like this:

  1. main.zig:4:9 is the file, the line and the column. Line 4, column 9 is where total starts.
  2. error: ... is the message. This is the line people paste into a search engine, and the one the pages above are named after.
  3. The source line and the ^~~~~ show the exact code. The ^ is the spot, the ~ marks the rest of the expression.
  4. note: ... lines add context. Read them: they often say the fix outright, like “consider using ‘const’” or “consider using ‘try’, ‘catch’, or ‘if’”. Here it says the variable needs a fixed-size type, so the fix is var total: u32 = 0;.
  5. referenced by: is the reference trace: how the compiler got to this code, starting from your main. When the error is in your own file, you can skip it. The lines with start.zig are Zig’s own startup code (.../lib stands for the folder Zig is installed in, which is a long path on your machine).

If there are several errors, start with the first one. One mistake can cause follow-up errors, and fixing the first one often makes others disappear. But do read the whole list: Zig doesn’t always print errors in line order.

When the error points into the standard library

Section titled “When the error points into the standard library”

Sometimes the error: line isn’t in your file at all:

const std = @import("std");
pub fn main() void {
const name: []const u8 = "Ada";
std.debug.print("{}\n", .{name});
}
.../lib/std/Io/Writer.zig:1503:21: error: cannot format slice without a specifier (i.e. {s}, {x}, {b64}, or {any})
@compileError("cannot format slice without a specifier (i.e. {s}, {x}, {b64}, or {any})");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
print__func_790: .../lib/std/Io/Writer.zig:805:25
print__func_789: .../lib/std/debug.zig:330:39

This is a check written inside the standard library, so it fails there. The bug is still yours: printing a string with {} instead of {s}. To find your line, ask for a longer reference trace:

Terminal window
zig run -freference-trace=10 main.zig

Now the trace includes a line from your file:

referenced by:
print__func_790: .../lib/std/Io/Writer.zig:805:25
print__func_789: .../lib/std/debug.zig:330:39
main: main.zig:5:20

main: main.zig:5:20 is your print call. Read the reference trace from the top and stop at the first line that’s in your code. For this error, the fix is {s} for strings and {any} for other slices: see Zig Format Strings.

A compile error stops the build: nothing runs. It starts with file:line:col: error:.

A panic happens while the program runs. It starts with thread 12345 panic:, followed by a stack trace with addresses like 0x11e3de5. The first location in the trace is where it happened; the next ones are the functions that called it. Panics come from Zig’s safety checks, which are on in debug (the default) and safe builds, and off in fast and small. Zig Illegal Behavior explains what’s checked. The language reference lists every check under Illegal Behavior.

These are real Zig 0.17.1 messages too, each with a short fix and the lesson that explains it.

Message Fix
local constant 'total' shadows local constant from outer scope Zig never lets an inner name hide an outer one. Rename one of them. Zig Scope
unused capture A |x| you never use. Write _ in its place, or drop it. unused local variable
value of type 'i32' ignored Use the result, or write _ = f();. error union is ignored
error union is discarded _ = f(); doesn’t work for calls that can fail. Use try or catch. error union is ignored
division with 'i32' and 'comptime_int': signed integers must use @divTrunc, @divFloor, @divCeil, or @divExact Pick how to round: @divTrunc(a, b) matches C. Zig Operators
cannot format slice without a specifier (i.e. {s}, {x}, {b64}, or {any}) Use {s} for strings, {any} for other slices. Zig Format Strings
expected ';' after statement A statement is missing its ;, usually at the end of the line above the caret. Zig Syntax
binary operator '+' has whitespace on one side, but not the other Write x + 1 or x+1, not x+ 1. zig fmt fixes the spacing. Zig Syntax
ambiguous use of '&&'; use 'and' for logical AND, or change whitespace to ' & &' for bitwise AND Zig spells it and, or, !. Zig Operators
value with comptime-only type 'comptime_int' depends on runtime control flow Give the result a type: const w: u32 = if (n > 5) 1 else 2;. Zig If/Else
@intCast must have a known result type Say the target type: const small: u8 = @intCast(n);. Zig Type Casting
returning address of expired local variable 'n' A pointer to a local dies with the function. Return the value, or allocate. Zig Pointers
no field named 'z' in struct 'main.Point' The field doesn’t exist; check the struct. no field or member function
'add' is not marked 'pub' Add pub to use it from another file. root source file struct has no member
unreachable else prong; all cases already handled Every case is listed, so remove the else. switch must handle all possibilities
type 'u8' cannot represent integer value '300' The number doesn’t fit the type. integer overflow
optional type '?[]const u8' does not support field access Unwrap the optional first with if or orelse. Zig Optionals

And a few more runtime panics:

Panic Fix
reached unreachable code An unreachable (or catch unreachable) was reached after all. Zig Unreachable
integer does not fit in destination type An @intCast got a value too big for the target. Use std.math.cast. Zig Type Casting
division by zero Check the divisor before dividing. Zig Illegal Behavior
access of union field 'x' while field 'y' is active Use a switch on a tagged union. Zig Tagged Unions
  • Run often. zig run main.zig after every few lines means you meet one error at a time, not ten. Zig is strict about unused names, so half-written code fails a lot; _ = x; keeps it building while you work.
  • Set up ZLS. The Zig language server shows these errors as you type, with the message on hover, and lists the fields and methods of any value after a dot. See Zig Tools.
  • Check the Zig version of anything you copy. Zig is still before 1.0 and changes between releases. Code for 0.15 or older often fails on 0.17 with “has no member named” or argument count errors. The 0.17.0 release notes list what changed. Our lessons are all checked on 0.17.1.

Many of these errors are bugs that other languages only show at runtime, or never: an error nobody handled, a missing switch case, a value that doesn’t fit, a variable you meant to update. Zig moves those checks to compile time, and the ones it can’t (like an index from user input) become panics in debug builds instead of silent memory corruption. You pay with more messages early on. The pages above are here to make that part quicker.

  • Zig has no warnings: every message stops the build.
  • Read the first error: line first: file, line, column and message.
  • The ^~~~ shows the spot; the note: lines often name the fix.
  • referenced by: is the path the compiler took. Use -freference-trace=10 to find your line when the error is inside lib/std.
  • thread ... panic: is a runtime crash, not a compile error. The first trace location is where it happened.

Next up: unused local variable, probably the first Zig error you’ll see.

Quick check

1. An error starts with main.zig:12:9:. What do 12 and 9 mean?
2. The error: line points into .../lib/std/Io/Writer.zig. Where is the bug most likely?
3. The output starts with thread 4021 panic: integer overflow. What does that mean?