Compile Time and Runtime
What the evaluator proves, what becomes a runtime check, and release mode.
Contents
The compile-time evaluator runs after type checking and folds what it can. Generated programs contain runtime checks for everything the evaluator could not decide.
Pipeline#
The compiler parses, resolves modules, checks types, and folds the checked program. Folding evaluates module constants to a fixed point, checks definition-time contracts, and evaluates pure function bodies where possible. A contract that evaluates to false is a compile error; true is discharged; anything else is emitted as a runtime check.
const LIMIT i32 = 10
fn below(n i32) i32
: pre n < LIMIT
=> n
fn main() {
print(below(3)) // folded and accepted
print(below(12)) // compile-time error
}
Evaluator Support#
| Construct | Evaluated at compile time | Note |
|---|---|---|
| Integer, float, boolean, string literals | Yes | Fixed-width arithmetic |
| Variables in the constant environment | Yes | Parameters are unknown unless constant |
Unary -, ~, ! |
Yes | Type-aware |
| Binary arithmetic, comparison, bitwise, shifts | Yes | Wrapping at the operand width |
&&, \|\| |
Yes | Short-circuit |
if |
Yes | Only the selected branch |
| Block expression | Yes | Final expression is the value |
cast, trunc |
Yes | Fixed-width conversion |
| Calls to non-generic functions with known arguments | Preconditions checked; pure body evaluated | Impure body not evaluated |
| Field access, indexing | No | Runtime check |
| Record literal, list literal | No | Runtime check |
match, is |
No | Runtime check |
| Interpolation, lambda | No | Runtime check |
| Generic function body | Checked per instantiation | Only bounded bodies are checked at declaration |
The evaluator has a recursion depth limit of 512 and a loop iteration limit of 100,000. A computation beyond those limits remains a runtime check.
Decision Matrix#
| Contract | Compile-time result | What happens |
|---|---|---|
| Precondition at a call site | All arguments known and true | Accepted |
| Precondition at a call site | All arguments known and false | Compile error at the call site |
| Precondition at a call site | Not evaluable | Runtime check before the body |
| Precondition at definition | Constant false independent of parameters | Compile error |
| Precondition at definition | Constant true independent of parameters | Clause removed |
| Postcondition on a pure function | Body and result known and false | Compile error |
| Postcondition on a pure function | Not evaluable | Runtime check at each return |
| Postcondition on an impure function | Never body-evaluated | Runtime check |
| Postcondition | Constant false independent of the result | Compile error |
| Normal construction | Constants and invariant false | Compile error |
| Normal construction | Constants and invariant true | Accepted, no runtime check |
| Normal construction | Runtime value | Compile error; use ? |
| Checked construction | Provable | Warning: redundant check |
| Checked construction | Known false | Compile error |
| Checked construction | Runtime value | Runtime check; error on failure |
| Loop invariant | Constant false | Compile error |
| Loop invariant | Constant true | Clause removed |
| Loop invariant | Not decidable | Runtime check at the loop header |
A clause that depends on parameters and survives folding may still be re-checked at runtime.
Runtime Failures#
A failed runtime precondition, postcondition, or loop invariant aborts. It does not return an error value. Expected failure belongs in Result.
Release Mode#
-release removes runtime preconditions, postconditions, loop invariants, and checked-construction invariant checks. It does not disable compile-time folding and does not remove index bounds checks.
| Check | Debug build | -release |
|---|---|---|
| Compile-time contract folding | Yes | Yes |
| Compile-time definition checks | Yes | Yes |
| Runtime preconditions | Yes | No |
| Runtime postconditions | Yes | No |
| Runtime loop invariants | Yes | No |
| Runtime checked-construction invariants | Yes | No |
| Index bounds checks | Yes | Yes |
assert in tests |
Yes | Yes |
Why the Evaluator Is Concrete#
The evaluator does not perform range analysis or symbolic reasoning. A contract such as r >= 0 && r <= n is checked by evaluating r and n when both are known. It is not used to prove that a multiplication cannot overflow. A contract should state a property the evaluator can decide when the inputs are constants, and remain meaningful as a runtime check otherwise.
Placement#
Place a precondition where the obligation belongs to the caller and a postcondition where the guarantee belongs to the callee. A precondition that is really a type invariant belongs in a refinement type. A postcondition that is really a property of a value belongs in a record invariant. A contract that restates the return type adds noise.
Debugging#
A compile-time failure points at the clause and, when relevant, the call site. The message names the failing comparison and the values that made it false. A runtime failure names the kind of check. The usual causes are an argument outside the documented domain, a parameter mutated after the precondition was checked, or a postcondition that refers to a field the implementation did not set as expected.
If the caller can legitimately pass the value, the precondition is too strong. If the callee can legitimately return the value, the postcondition is too strong. If neither is true, the implementation is wrong.