Skip to content

const_assert

const_assert <expr> evaluates a boolean expression in the compiler. If the result is false, compilation fails with E_AssertFailed and the optional message you provided — before any binary is generated. If it is true, it produces no code at all: zero runtime cost.

Use const_assert to protect invariants about constants, buffer sizes, protocol versions, and any other property you can verify at compile time. It complements the runtime assert(): while assert() checks conditions that depend on runtime input, const_assert verifies properties that should never change between builds.

Invariants over literals, const bindings, and usage inside functions; commented-out diagnostics at the end.

08-const-assert.zolo
Playground
// Feature: `const_assert` — compile-time assertions
// Syntax: `const_assert <expr> [, "message"]`
// When to use: validate compile-time invariants over `const`s and other
// comptime-constant expressions. If the expression evaluates to `false`,
// the build fails with `E_AssertFailed`. Successful asserts produce no
// runtime code — zero cost.
//
// See `specs/const-assert.md` for the full specification.

// Simple assertion over a literal.
const_assert 1 + 1 == 2
const_assert true

// Assertion with an optional human-readable message.
const_assert 10 > 5, "ten must be greater than five"

// Assertions can reference `const` bindings — the comptime engine
// looks them up automatically.
const MAX_BUFFER = 4096
const MIN_BUFFER = 64

const_assert MAX_BUFFER > MIN_BUFFER
const_assert MAX_BUFFER >= 256, "buffer too small for protocol header"
const_assert MAX_BUFFER % 2 == 0, "buffer size must be even"

// `const_assert` is valid inside functions too. Useful when generic
// or template-like code needs to enforce an invariant per call site.
fn validate() {
  const_assert MAX_BUFFER < 1000000
  print("validate ran (asserts already passed at compile time)")
}

validate()
print("PORT validated: max_buffer={MAX_BUFFER}, min={MIN_BUFFER}")
// expected:
// validate ran (asserts already passed at compile time)
// PORT validated: max_buffer=4096, min=64

// Diagnostics (uncomment to see):
//
// 1. `E_AssertFailed` — the expression evaluated to `false`.
//
//    const_assert false, "this must fail"
//    // error[E_AssertFailed]: static assertion failed: this must fail
//
// 2. `E_AssertNotBool` — the expression is not a bool.
//
//    const_assert 42
//    // error[E_AssertNotBool]: `const_assert` expects `bool`, got `Int`
//
// 3. `E_AssertNotConst` — the expression depends on runtime state.
//
//    let runtime_val = some_fn()
//    const_assert runtime_val == 3
//    // error[E_AssertNotConst]: expression in `const_assert` is not
//    //                          comptime-constant

// Difference vs. `assert()` (runtime):
//   const_assert  — evaluated at BUILD time, blocks compilation if false.
//   assert(cond)  — evaluated at RUN time, raises a panic if false.
//
// Use `const_assert` for invariants over `const`s, types, and buffer
// sizes. Use `assert()` for runtime preconditions that depend on input.

Three possible compile errors summarize the rules:

  • E_AssertFailed — the expression evaluated to false.
  • E_AssertNotBool — the expression is not boolean (e.g. const_assert 42).
  • E_AssertNotConst — the expression depends on runtime state.

Challenge

Add const MIN_BUFFER = 4096 and a const_assert MIN_BUFFER < MAX_BUFFER that fails. Observe the compiler error message and fix the value.

enespt-br