You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
comptime: infer comptime-safety so ordinary functions run at compile time (remove comptime fn coloring) #2213
Only functions marked comptime fn may run during compilation; the evaluator refuses anything else with comptime can only call comptime functions. So an ordinary function cannot be used at compile time:
fn fib(n: i32) -> i32:
if n < 2: n else: fib(n - 1) + fib(n - 2)
const TABLE = comptime fib(20) // error: fib is not a comptime fn
Marking it comptime fn makes it compile-time-only, so a function wanted at both times needs two versions. That is function coloring: every function is one color, and the color spreads through what it calls. It is the same problem async has elsewhere, which With avoided for async with fibers.
Zig runs any function at compile time when its inputs are known (const table = comptime fib(20);), and much of its std (integer parsing, formatting, sorting) works there unplanned. It complains only when evaluation reaches something impossible, such as a system call.
Why With marks functions today
With's comptime promises no file access, no network, no clock, no environment, and deterministic output. Marking functions makes those promises checkable one function at a time; Zig only finds out when evaluation hits something forbidden.
Proposal: infer comptime-safety, as may_suspend is inferred
A plain fn is callable at compile time when nothing it calls, directly or transitively, does I/O, calls C, uses a capability, or reads the clock. Sema already infers properties from bodies (may_suspend); comptime-safety is the same kind of fixed point.
fib above works unmarked, at both times.
A compile-time call that reaches something impure is refused, and the error prints the call chain that led there (fib -> helper -> print: I/O), so the reason is explicit.
comptime fn stays as an optional promise, worth writing on public library functions: it pins the property in the signature, so a library author who adds a print breaks their own build instead of silently breaking every downstream compile-time use. That is Zig's gap; nothing in a Zig signature says a function must stay compile-time-safe.
Every guarantee comptime makes today is kept, and the coloring goes. It fits the mission's rule: the compiler can already tell which functions are safe to run, so the programmer should not have to say so.
Spec
This changes the spec's rule that a comptime fn may call only comptime fns (and the evaluator's refusal); the wording is Eric's to bless before implementation.
Ruled (Eric, 2026-10-07): yes, as a hybrid. comptime fn is checked statically at the declaration (every callee must resolve; an indirect call is refused there); a plain function is judged by the evaluator when a compile-time call reaches it, where each indirect callee is known. A forbidden operation reached at compile time reports the chain of calls to it — the witness chain is a hard requirement. Stated plainly in the ruling: a plain function called at compile time is still broken by an upstream print three calls deep (Zig's hole, accepted); comptime fn is the promise spelling. Spec §17.1; decision D104. Implementation pending.
Activity
ehartford commentedon Oct 6, 2026
Ruled (Eric, 2026-10-07): yes, as a hybrid.
comptime fnis checked statically at the declaration (every callee must resolve; an indirect call is refused there); a plain function is judged by the evaluator when a compile-time call reaches it, where each indirect callee is known. A forbidden operation reached at compile time reports the chain of calls to it — the witness chain is a hard requirement. Stated plainly in the ruling: a plain function called at compile time is still broken by an upstream print three calls deep (Zig's hole, accepted);comptime fnis the promise spelling. Spec §17.1; decision D104. Implementation pending.D103, D104: value-to-Option conversion (§4.9a); comptime-callability …
D104: comptime-callability is inferred; a plain function runs at comp…
D104: comptime-callability is inferred; a plain function runs at comp…