ADR-prover-checks-v1: An Index or Overflow Check a Proof Shows Unneeded Is Not Emitted — remove-bounds-checks, remove-overflow-checks and What a Dropped Check Lowers To

Status: Accepted Date: November 29, 2026 (the consolidation, #1790; the decisions themselves October 3 to November 8, 2026) History: the records this one replaces are archived in docs/history/adr; read them only for how the decision was reached Answers: ADR-269 §6’s open question for two of the checks nobody writes - whether a proved check is not emitted - for an index out of bounds and for an arithmetic overflow: who decides that the compiler tries, what a dropped check lowers to, and what the build defaults to Supersedes / Related: takes the decisions on the options and the lowering of the checks nobody writes from the numbered record now in the history, with new numbers; how the facts are walked is ADR-prover-walk-v1, the facts the compiler finds about data are ADR-prover-found-facts-v1, and what a build refuses is ADR-prover-refusals-v1 · ADR-269 D4 (a claim the compiler proves has no check emitted, and means the same either way), D9-D10 (what the prover reads), ADR-270 D1 (Fourier-Motzkin in the logic layer) and D5 (a check is dropped only on a certificate its checker accepts), ADR-285 D1 (an index is an i64) and D5 (an overflow stops the program at every build), ADR-218 D1 (every unsafe topic is a small crate of its own), ADR-258 (the four weights), Part III A.2 (an index out of bounds, an overflow and a conversion that does not fit stop the program at every build) and 13.3 ([build])

1. Question

xs[i] stops the program where i is outside xs, and a + b stops it where the sum leaves its type (Part III A.2). Each check is a compare and a branch, nothing alone; in a loop it is what keeps the code generator from unrolling and vectorising. In the big-integer kernel of benches/solver-kernels.nika the index checks in out[i + j] are 34 % of the instructions.

The checks are not optional: reading past a list’s end or overflowing is a defect, and the stop is how it is seen. But a check that cannot fail is a cost, not a check. ADR-269 D4 drops a written assert the prover proves; the same question stands for the checks nobody wrote.

May the compiler drop an index or an overflow check, when, who decides that it tries, and what does a dropped check lower to? What a list holds, which makes most of the costly checks provable, is ADR-prover-found-facts-v1’s question.

2. How others do it

Language / tool Its answer Who writes the bound Source
Rust always checked; LLVM removes what it proves from the code it sees; get_unchecked in unsafe for the rest - the language, from memory
Swift checked; -Ounchecked removes the checks without proof, so a wrong index is undefined behaviour - from memory, not checked
HotSpot C1, .NET RyuJIT range-check elimination from induction variables, conditions, lengths and a type’s own range; never from what an array holds - c1_RangeCheckElimination.cpp, RyuJIT rangecheck.cpp
Ada / SPARK range subtypes, also as component types; checked at each store, or GNATprove proves the absence of run-time errors the programmer GNATprove output
Wuffs refinement types, also as element types (array[4096] base.u16[..= 4095]); every index and overflow proved at compile time, by interval arithmetic and earlier ifs; what is not proved is refused the programmer, in the type bounds-checking note
Pascal subrange types; with {$R+} stores are checked at run time the programmer range and overflow checks
Liquid Haskell, Dafny refinements in type arguments ([{v:Int \| v < n}]), quantified invariants or subset types; an index is a proof obligation the programmer, partly inferred ICFP’14 paper, Dafny types
Astrée array smashing: an array is one abstract cell, an interval every write joins inferred snippet only, the paper was not reachable
Clousot (FunArray) an array split into segments with symbolic bounds, one interval each inferred Cousot, Cousot, Logozzo, POPL 2011
LLVM !range metadata on a load; rustc sets it for bool, char, enum tags, NonZero the frontend, as an assumption rustc’s builder.rs
Rust pattern types, Zig ranged integers exist only inside the compiler (Rust) or as an open proposal (Zig) would be the programmer rust#123646, zig#3806

What transfers. SPARK’s and Dafny’s order - prove, then drop - and the JIT’s range-check elimination for the loop shape that is most of the cost. For what a list holds, two families: the programmer writes a bound in the element type (Wuffs, Ada, Liquid Haskell, Dafny), or the tool infers one interval per array from every write (Astrée, Clousot). Both put the work at the write, which runs less often than the read.

What does not. Swift’s -Ounchecked and LLVM’s !range used as a guess: a build option that turns a stop into undefined behaviour changes what a program means, and Part I 1.2 lets no build option do that. Wuffs refuses what it cannot prove; Nikaia checks it, so a proof not found costs speed, never a refusal. Astrée’s and Clousot’s precision comes from a fixpoint over loops; the walk here is a single forward pass that forgets at a loop’s head.

3. Options, weighed

  What it is ADR-258 D1 the special case pays D2 the compiler works D3 no cost for the common case D4 consistent If it is wrong
A leave it to LLVM every check emitted yes no: the author rewrites the loop until LLVM sees it yes yes slow, never wrong
B an unchecked mode -Ounchecked, or !range assumed yes yes yes no: a flag changes meaning memory read past the end, silently
C drop what is proved, always ADR-269 D4’s rule as it stands for an assert no: every build proves yes no yes a wrong proof, everywhere, unasked
D drop what is proved, at a level the build asks for --optimization=…:basic\|aggressive; values’ bounds inferred from every write yes: only a build that asks proves yes: nothing is written yes: off by default yes: meaning never changes a wrong proof, caught by the certificate’s checker before a check is dropped; a bound not found costs speed only
E the bound written in the element type Vec[Vec[u32 in 0..<32000]] (illustration only), checked or proved at each write yes no: the author writes and keeps ranges yes no: a range type for list elements alone, which the language has nowhere else a new place for the program to stop

D is chosen. B and C lose on the promise to the program: B changes what it means, C makes every build pay and lets a growing prover change every program’s emitted code unasked. A leaves the work to the developer. E reaches what D cannot - a bound across a call, a list behind a parameter - but is a new kind of type, and ADR-258 D2 says the compiler works first.

The number that decided it: on the solver’s watch kernel the index proof alone gives 10.44 M instructions against 10.69 M for Rust by hand, and 9.41 M with the bound on a list’s values.

4. Decision

D1 — One option, --optimization=NAME:LEVEL, and its first name, remove-bounds-checks.

--optimization=remove-bounds-checks:LEVEL on the command line, for every command that lowers (lower, build, run, test), or optimization = "remove-bounds-checks:LEVEL" in [build]. The levels are off (the default until D11), basic and aggressive. Words are comma-separated, and the command line may give the flag more than once; a later word for a name wins. A name or a level the compiler does not know stops the build before anything is read, naming the words there are.

Unlike Part I 1.2’s options it changes nothing a program means (D2), so a single build may override it, as it may --target. It is part of the build’s cache key, and reaches the wrapper Cargo runs as the other options do. std is lowered at off: the toolchain’s own lowering does not follow an option of the program it serves (Part III 13.2).

D2 — No level drops a check that is not proved.

An index check is dropped only where 0 <= i < xs.len() holds every time the index is reached. The program means the same at every level: where the proof holds, the check could not have stopped it; where it does not, the check stays. A level says how hard the compiler tries, not what the program may do, and a program that stops at an index at off stops at it at aggressive. The same holds for every name of D1’s option.

D3 — basic: the loop over a list’s own length.

xs[k] where k is the binding of an enclosing for k in lo..<xs.len(), lo is a literal that is not negative or the binding of such a loop, and nothing in the loop’s body can change xs’s length or k: xs is not assigned or bound again, not handed to a call, not captured by a lambda, and every method called on it is one a fixed list knows to keep the length (len, contains, iter, sort, swap, …). An index inside a lambda in the loop is not proved: the lambda may run after the loop. No solver is asked; the pass is linear in the body.

D4 — A dropped index check lowers to proven::read and proven::write.

The checker records which indexes are a position into a Vec or an Array (not a range, not a map’s key); the emitter writes a proved one of those as

*unsafe { nikaia_std::proven::read(&xs, (i) as usize) }
unsafe { nikaia_std::proven::write(&mut xs, (i) as usize, value) }

proven is crates/unsafe/proven-index (ADR-218 D1): three functions over get_unchecked, each with a debug assertion, its README listing every unsafe and its argument, checked by scripts/check-unsafe-crates.sh like the others. nikaia-std re-exports it and stays #![forbid(unsafe_code)]. The unsafe the emitted code writes stands on the proof, which is the argument; the crate holds the one operation the proof licenses.

D5 — A compound assignment is the read, the operation and the write it means.

xs[i] += d on a list of numbers (or of bools) at a position ADR-prover-walk-v1 D1 or ADR-prover-found-facts-v1 D1 proves is lowered as the value, then the position, then one proven::read, the operation and one proven::write - the element read once, as += reads it. A slot that is not a copy, such as text, keeps its checked index, because reading it out would move it. x += d on a name of one whole-number type is recorded by the statement’s first byte and proved as x + d with what is known of x before the statement; a proved one is x = <T>::wrapping_add(x, d) (D7) (#387).

D6 — remove-overflow-checks:aggressive, the second name of D1’s option.

--optimization=remove-overflow-checks:aggressive, or the same word in [build] optimization, with the levels off (the default) and aggressive. There is no basic: the one shape a walk without a solver would prove is the one LLVM already proves. The name is independent of remove-bounds-checks and part of the cache key like it. D2 holds: an overflow check that is not proved stays.

D7 — A dropped overflow check lowers to <T>::wrapping_*.

Its lowering is replaced by D10.

The checker records each +, - and * whose two sides are of one whole-number type, by the operator’s span, with that type. A proved one lowers to <T>::wrapping_add(a, b) (_sub, _mul). With no overflow, that is the same number with no check, and no unsafe is needed. Naming T ties the proof’s type to the emitted one: if they ever disagreed, rustc would refuse the program instead of computing something else.

D8 — Every level is held to the same output and the same stop.

Every program the tests run prints the same at every level of every name, and a program that stops at an index or an overflow at off stops at the same one at aggressive, with the language’s message - never with proven-index’s debug assertion. The tests that hold a level to this are compiled without -O, so that a wrong proof stops there instead of reading past the end. A level is kept only where, on the solver’s kernels, it is at or below off with LTO.

D9 — How hard the compiler proves and what it does with a proof are two settings.

Added October 4, 2026 (#390). It replaces D1’s levels, D3’s and D6’s names for them, and the question of the default it left open.

proving says how hard the compiler proves: a level that maps to a step budget for the solver (ADR-270), counting only its expensive steps - a case, an elimination - which costs less than reading a clock and answers the same everywhere. It reaches both clients: the claims a program writes (ADR-269) and the checks it does not write, index and overflow. remove-bounds-checks and remove-overflow-checks say only what happens to a check that was proved: on writes it without its check, off (the default) keeps it.

Why two. One switch meant both search harder and drop what was found. A program that wanted more of its defects found at build time had to take the dropped checks with it, and a program that wanted neither had no budget to set. The two buy different things: more proving buys safety, because more defects are found before the program runs (ADR-prover-refusals-v1 D1); dropping checks buys speed, at a risk proving alone does not carry.

Why dropping stays off by default. A proof is checked by its certificate (ADR-270 D5), but the certificate covers the solver’s arithmetic, not whether the query the walk built says what the program does. A wrong walk is a defect of the compiler, as a wrong lowering is - and where it drops a check, it turns a defect of the program that would have stopped cleanly into a read past a list’s end (get_unchecked, D4, with its debug assertion off in the profile a program is built with) or a wrong number (wrapping_*, D7). A wrong refutation is a visible error with a bypass; a wrong drop is invisible.

Why changing one does not redo the other. The proofs depend on proving and the sources; remove-*-checks only chooses what is written. A build that switches dropping on or off proves nothing again and caches nothing twice.

As built: remove-bounds-checks and remove-overflow-checks take on and off, and the walk runs at every build, D3’s shape and ADR-prover-walk-v1 D1’s solver together, whatever they say. basic is no longer a word of the option: its one shape is proved at every build, and whether a level without the solver is worth offering is proving’s question (§6). The old words are refused with the word they now are.

D10 — A dropped overflow check lowers to proven::add, sub and mul.

Replaces D7’s lowering, October 22, 2026.

A proved +, - or * lowers to unsafe { nikaia_std::proven::add::<T>(a, b) } (sub, mul), from proven-index, as a proved index lowers to proven::read: <T>::unchecked_add behind a debug_assert of checked_add. T stays named, so D7’s tie between the proof’s type and the emitted one holds. The operation tells the optimiser that it does not overflow, which wrapping_add does not: in 1BRC’s fast.nika that saves one instruction a row at x86-64-v3, where wrapping_add lost what the check it replaced had told the optimiser. A wrong proof stops at the assertion in every build that keeps debug assertions, D8’s tests among them, and is undefined behaviour in one that does not, as a wrong proved index already is and a proved shift already is (ADR-prover-walk-v1 D6).

Rejected: keeping wrapping_* (D7). It turns a wrong proof into a wrong number rather than undefined behaviour, but the index and the shift beside it do not, so the operation was the one place that paid for a guarantee the program as a whole does not have. <T>::unchecked_add written into the program directly: the same code, but an unsafe outside the listed crates (ADR-218) and no assertion in a debug build.

D11 — A proved check is dropped by default: remove-bounds-checks and remove-overflow-checks are on unless the build says off (owner, 2026-11-08).

The owner (2026-11-08): the index and overflow checks a proof shows unneeded are removed by default. D1 made off the default so that the first builds of the optimization could be compared with the plain ones; D2 and D10 since make the drop safe to have on: no check that is not proved is dropped, and a dropped one is still asserted in a build that keeps debug assertions.

Rejected: on only in a release build. A check a proof drops is dropped in both; a debug build differs only in asserting it (D10), so one default for both keeps the two builds proving the same thing.

D12 — std and the toolchain are lowered with the D11 default (owner, 2026-11-08).

D1 lowered std at off so that the toolchain’s own lowering does not follow an option of the program it serves (Part III 13.2). That reason is about following a program’s word, not about the level: with D11 the default is on, and std takes the default, whatever the program names. Measured with lower-std --bounds (2026-11-08): 2543 of 4058 indexes and 1394 of 2490 operations are proved, in 227 of the 294 lowered files.

Decided by the supervisor as following from D11 under the language principles (docs/supervisor.md, Language decisions), after the owner asked why std should keep its proved checks; built in #1475.

5. Consequences

6. What this does not decide

This site is open source. Improve this page.