ziglang/zig
Patterns and conventions
Conventions visible from reading the source. There is no STYLE.md; what follows is what the codebase actually does.
Files and naming
- One type per file when the file is named after a type.
src/Compilation.zig,src/Zcu.zig,src/Sema.zig,lib/std/Build.zig,lib/std/Build/Step.zigare allpub const Self = @This();-style files. The file name matches the exported type. - lowercase for module-style files.
src/codegen.zig,src/link.zig,src/target.zig,lib/std/mem.zigare namespaces, not types. They exposepub const X = ...andpub fn .... fmtis enforced.zig fmt --checkruns in CI.
Imports
Files start with imports at the top, grouped roughly as: stdlib first, then internal modules. Example from src/main.zig:
const std = @import("std");
const Compilation = @import("Compilation.zig");
const link = @import("link.zig");
const Package = @import("Package.zig");Allocation
- Pass
Allocatorexplicitly. The compiler usually has a top-level arena (gpa) and per-stage arenas (comp.arena,zcu.gpa). Look atsrc/Compilation.zigfor the canonical setup. - Container types in
lib/std/come in Managed (carries an allocator) and Unmanaged (does not) flavors; e.g.ArrayListandArrayListUnmanaged. Compiler internals lean on the Unmanaged ones. defer ... .deinit()is used everywhere to avoid leaks; many helpers also supporterrdefer.
Errors
- All fallible functions return
!T; the error set is usually inferred. When a function explicitly enumerates the set, the names are descriptive (e.g.error.OutOfMemory,error.Overflow,error.InvalidElfFile). - The compiler builds an
ErrorBundle(seelib/std/zig/ErrorBundle.zig, ~33 KB) for diagnostics rather than printing inline.Zcuaccumulates these andmain.zigrenders them at the end. tryis the dominant control flow; explicitcatchis reserved for places that need to map or accumulate errors.
Comptime
- Heavy use of
comptimefor compile-time tables, type generation, and sentinel checks. inline fnandinline for/inline whileare used where the optimizer should specialize per-type.- ZIR is the target of
comptimeevaluation; AIR contains only post-evaluation results.
Concurrency
- The compiler's threading model is one
ThreadPoolplus per-decl work items. Seesrc/Compilation.zigfor the queues. lib/std/Thread.zig(~72 KB) is the source forstd.Thread;lib/std/atomic.zigfor atomics.- The new
lib/std/Io.zigmodel is the recommended path for I/O concurrency in user code.
Logging
std.log.warn,std.log.info,std.log.debug. Scopes are used liberally.src/main.zig'sstd_optionssets the default log level by build mode (.debugin Debug,.infoin ReleaseSafe/ReleaseFast,.errin ReleaseSmall).
Panic and asserts
std.debug.assertfor invariants.std.process.fatal(aliased asfatalinsrc/main.zig) for unrecoverable user-facing errors.- The compiler's panic handler is
crash_report.panic(see Debugging).
Tests
test "name" { ... }blocks colocated with the code under test.zig build test-behaviorandzig build test-fmtare CI gates.comptime { _ = ... }blocks are used to force the compiler to import test files at the bottom of namespaces (e.g.comptime { _ = start; }inlib/std/std.zig).
Self-deprecation markers
pub const @"bad O(N)" = void;insrc/main.zigis a literal compile-time tag used to flag and grep for inappropriate linear searches the maintainers want to fix.Deprecated;doc comments on aliases mark API churn (e.g.ArrayListAligned→array_list.Alignedinlib/std/std.zig).
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.