ADR-396: An Index Reached by Data From Outside the Program Is Proved or Refused

Note (#1600, 2026-11-15): crates/nikaia-std/src/tools/ has moved to crates/nikaia-std/src/compiler/ (its generated Rust to compiler/lowered/). The text below keeps the old path; the Rust module is still nikaia_std::tools.

Status: Accepted; amended November 14, 2026 (D6-D8, owner’s decisions on #1576 and #1578) Date: November 11, 2026 (owner’s decision: “ein Indexzugriff über Daten von außen braucht einen Beweis, sonst ist es ein Compilerfehler; was jetzt nicht geht, wird repariert oder zusätzlich mit einem sichtbaren Guard geschützt”) Answers: whether an index the compiler does not prove keeps its run time check when its value comes from someone other than the program’s operator Changes: ADR-380 D1 (an index the compiler does not prove keeps its check) and ADR-391 D1 (a derived precondition is never a refusal), both for data from outside the program only Related: ADR-010 D2, ADR-365 D1 (which sources are outside the program); ADR-269 D6 (NK1202, a guard where outside data arrives); ADR-306 D15 and ADR-395 (NK1207, an index shown to fail); ADR-380 D2 (unproved-claims); #1571 (a guard at the call does not yet prove an index in a private function)

1. Question

fn month_name(month: int) -> String {
    let names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    return names[month]
}

fn handle(req: Request) -> String {
    let month = text::parse_i64(req.query("month") ?? "") ?? 1
    return "" if !(month >= 0 && month <= 12)      // one too many
    return month_name(month)
}

The guard is wrong by one, and a request with month=12 stops the program at names[month]. Today this builds. The index keeps a check (ADR-380 D1), the derived precondition month < 12 is dropped because the caller does not prove it (ADR-391 D1), and the compiler says nothing. NK1207 does not apply either: no value the program reaches is shown to fail. The fault depends on what a stranger sends.

ADR-391 was written to remove run time checks; a check that stays was counted as cost, not as a fault. The owner’s direction (2026-11-11) puts preventing faults first and performance second. Indexing a 12-element list is an implicit claim that the index is below 12. When someone outside the program chooses the index, that claim is exactly what ADR-269 D6 says no program can make about outside data without a guard.

2. How others do it

Nikaia already knows which values are outside (ADR-010, ADR-365) and already proves indexes. This record joins the two.

3. Decisions

D1 — An index whose value depends on data from outside the program is proved, or the program is refused, NK1302.

An index into a list, a string or a slice, and a slice’s bounds, whose value depends on data from outside the program (ADR-010 D2, ADR-365 D1: a request, a socket, a database row, standard input, a file not marked trusted: true) is proved in bounds while the program is built, or the build refuses it, NK1302. “Depends” is the flow the compiler already follows for NK1202: the value, or a value computed from it, reaches the index.

The walk follows the value through every call whose body it reads in the package. A private function’s index on a parameter that a caller fills with outside data is asked at that call, as the derived precondition of ADR-391 D1. For that caller the precondition is an obligation, not a fact to drop. The message names the call, the index, and the case the walk could not exclude (“Here month can be 12; names has 12 elements”).

An index that no outside data reaches is not asked. It is proved or checked as before (ADR-380 D1).

D2 — The way out is a guard where the data arrives.

As with NK1202, the message names the form that works: a guard where the value arrives, throw BadInput(month) if !(month >= 0 && month < 12), or the early return that rejects it, after which the index is proved from the guard and costs nothing. A guard that does not prove the index (one too many, as in §1) leaves the refusal in place. That is the point of the rule. An assert is not a way out: it is a claim about outside data (NK1202).

A guard the compiler should read and does not is a defect of the walk (#1571, #1482). It is fixed in the walk, not written around.

D3 — unproved-claims decides the severity, as for NK1300.

NK1302 is a claim the build did not prove, so it follows [build] unproved-claims (ADR-380 D2): at error, the default, the program is refused. At warn it builds, each site a warning and a check where it is reached. The switch exists for the day a better or a different prover disagrees with an older one, not as a stage on the way in.

D4 — It is switched on when our own code builds under it.

There is no intermediate warning stage by default. The build that adds NK1302 also makes every site it finds in crates/nikaia-std/src/tools, packages/ and examples/ build. Each site is one of two things:

  1. a correct guard the walk does not read: the walk is fixed (#1571, #1482);
  2. a missing or wrong guard: a guard is added where the data arrives, visible in the code.

Until those are done, the refusal is not merged.

D5 — --bounds shows it.

--bounds lists an index that outside data reaches as such, with the source of the value. It shows it as proved, with the guard that proves it, or as refused (NK1302). At warn it shows the index as checked because of the switch.

D6 — A pub function states what its body needs (owner, 2026-11-14, #1576).

A pub function cannot see who calls it, so every call into it may carry data from outside. An index, a divisor or an overflow on a parameter that its body does not prove from its own guards needs one of two things: a written precondition on that parameter (an assert, published as requires, ADR-269 D18, D20) or a guard in the body that handles the case. Otherwise it is refused, NK1302 for an index and NK1303 for a divisor or an overflow (D7), under unproved-claims (D3). Postconditions stay optional: a caller that needs a fact about a result gets the hint NK1263 (ADR-359 D4). A derived precondition is still never published (ADR-391 D4).

D7 — A divisor and an overflow on outside data are refused like an index (owner, 2026-11-14, #1578).

The rule of D1 applies the same way to / and % and to the plain +, - and *. An operation that data from outside reaches, and that the build does not prove safe (a divisor not zero, and for / and % on signed integers not the minimum by -1; a result inside its type), is refused, NK1303, under unproved-claims (D3). Where the author chose how overflow is handled, with wrapping_*, saturating_* or checked_*, that choice is the handling and nothing is refused. --bounds lists / and % as proved or checked, as it already does for +, - and * (D5).

D8 — Both are switched on the way D4 says.

D6 and D7 are fixed rules; the order of the work does not reopen them:

  1. the sites in crates/nikaia-std/src/tools, packages/ and examples/ that they would refuse are listed;
  2. where the prover is too weak or a fact is missing, the fact is added (a std contract, a ledger entry) or the prover is improved. No guard goes into our own code only so that the prover gets through. An i64 or u64 +, -, * gets its goal within the bounds walk’s window ±2^62 (as #444 and the division goal); a proof there is final, and NK1207 is raised only when the refuting values, computed with checked_*, really leave the type (ADR-270 D11, #1810);
  3. the refusal is switched on once our own code builds under it.

4. Not decided here

Nothing. The two points this section first left open, the package boundary and the divisor and overflow, are decided in D6 and D7 (owner, 2026-11-14).

5. Considered and rejected

A warning first, an error later. The owner rejected it (2026-11-11): code that builds with a fault a stranger can trigger is what the rule is there to stop, and the sites in our own code are repaired before the rule is merged (D4).

Refusing every index the compiler does not prove. That is SPARK’s silver level. Most unproved indexes in our code are reached by no outside data, and the prover is incomplete (ADR-269 D10). Outside data is where a stranger chooses the value.

Accepting an assert as the way out. An assert about outside data would claim what the program cannot know (ADR-269 D6). Only a guard handles the case.

6. Consequences

This site is open source. Improve this page.