Issue #1578: Decision: refuse a divisor or an overflow reached by outside data the way an index is refused (ADR-396 §4), and list / and % in --bounds

Open and answer this issue on GitHub.

ready · opened 2026-11-12 · updated 2026-11-15 · 4 comments

Where this comes from

An outside consultancy made suggestions to the owner on 2026-11-12. This is one of them: safe arithmetic, with division by zero and overflow either proved impossible or handled explicitly, never silently wrapping and never x / 0 = 0. Most of this is already decided in Nikaia. This issue files only the part that is missing.

The question, from zero

Integer arithmetic has two run-time failures: overflow, where the result does not fit the type, and division by zero, which covers / and %. Languages pick among wrapping silently (C for unsigned values, Go), defining x / 0 as 0 (Pony, some verification languages), stopping the program, or proving at build time that the failure cannot happen.

Nikaia already stops the program on both, and drops the check where it is proved. For indexes, ADR-396 goes one step further: an index that data from outside the program reaches, and that the build does not prove, is refused (NK1302, #1572). ADR-396 §4 leaves open whether a divisor and an overflow reached by outside data are refused the same way. It says this “waits on the count”.

A second, smaller gap is the report. nikaia build --bounds (#389) lists each index and each +, - and * as proved or checked. It does not list / or %, so an author cannot see which divisions still carry a check.

The question: should a divisor or an arithmetic operation that outside data reaches, and that the build does not prove safe, be refused like an index? And should --bounds list / and %?

What is already decided (and is not reopened here)

How others do it

These facts are from memory, not checked.

Language Overflow Division by zero
Rust panics in debug builds, wraps in release builds unless configured panics
Swift traps; &+ wraps traps
Zig undefined behaviour in release-fast builds, checked in safe builds the same
SPARK proved absent at the “silver” level, or refused proved absent, or refused
Dafny mathematical integers with no overflow; machine types need proofs the divisor must be proved non-zero (a precondition of /)
Pony wraps x / 0 = 0
Vera, AILANG, Boruna not known to me  

Options

  1. Do nothing. Divisions and overflows stay checked at run time where they are not proved, and --bounds keeps silent on / and %.
    • For: no new refusals.
    • Against: outside data can stop the program through a division where it cannot through an index, and the report hides it.
  2. Report only. --bounds lists / and % as proved or checked, with what reaches the divisor, as it does for +, - and *. Nothing new is refused.
    • For: small, and it gives ADR-396 §4 its count.
    • Against: closes only the visibility gap.
  3. Report, then refuse on outside data (ADR-396’s rule extended). As option 2. After a count over crates/nikaia-std/src/tools, packages/ and examples/, an unproved divisor or overflow that outside data reaches is refused with a code next to NK1302, under unproved-claims. The author fixes it with a guard, a checked_* call or a precondition.
    • For: one rule for all three run-time failures on outside data. It is exactly the consultancy’s “proved or handled”.
    • Against: overflow on outside data is common, for example sizes from a file or sums of read numbers, so the count may be large. It needs the range analysis to be good enough.
  4. Refuse divisors only, report overflow. A divisor is rarely legitimately unguarded, while overflow is everywhere.
    • For: a smaller blast radius.
    • Against: two rules.

Recommendation

Option 2 now. It is small: bounds_report_checked_op in crates/nikaia-std/src/tools/bounds_report.nika returns true only for Add, Sub and Mul, so it can take Div and Rem as well, and Part III’s --bounds text needs the matching change. Then decide between 3 and 4 on the count that option 2 gives, as ADR-396 D4 did for indexes.

What it touches

Comments

keywan-ghadami, 2026-11-14

Decision (owner, 2026-11-14)

This is a question of correctness, not of a count. The rule for indexes in ADR-396 applies the same way to /, % and overflow of +, - and *. An operation that outside data reaches, and that the build does not prove safe, is refused, with a code next to NK1302 under unproved-claims.

Overflow: the rule applies only to the plain operators. Where the author has chosen how overflow is handled, for example with wrapping_*, saturating_* or checked_*, that choice is the handling and nothing is refused. Only a plain +, - or * that is not proved safe on outside data is refused.

Rollout, done carefully, because the prover is not yet strong enough in many places:

  1. The rule is fixed. ADR-396 §4 is amended to say so.
  2. --bounds lists / and % as proved or checked, as it already does for +, - and * (bounds_report_checked_op in crates/nikaia-std/src/tools/bounds_report.nika, and Part III 30-nikaia-tooling.md).
  3. Where the prover is too weak or facts are missing, the facts are added (std contracts, ledger entries) or the prover is improved. No guard goes into our own code only so that the prover gets through.
  4. The refusal is switched on once our own code (crates/nikaia-std/src/tools, packages/, examples/) builds with it.

To build


Generated by Claude Code

keywan-ghadami, 2026-11-15

Brief queued: issue-1578


agent: builder model: opus max_turns: 200 budget_usd: 30 effort: medium —

Issue #1578

Your work order is issue #1578, inlined below with its comments. There is no brief written around it: plan your own steps from its To build list, build them in order, commit after each finished step, and end when its Done when holds (or at the limits of builder.md, reporting what is left). The facts in your prompt are Core of docs/builder-facts.md; more on a topic: nikaia run scripts/syntax.nika -- KEYWORD, a probe: nikaia run scripts/probe.nika -- SNIPPET.

Measure the issue against docs/issues.md while you work: what it lacked, what was wrong in it. That answer goes into the report’s Issue: field. A finding, a changed plan or a finished item also goes into the issue as a comment (gh api repos/Nikaia-Foundation/Nikaia/issues/1578/comments -F body=@file, ending with the Claude Code footer), rule I13.

Rules (CLAUDE.md; shared checkout)

Report (at most 15 lines, to exactly /home/user/Nikaia/docs/briefs/safety/issue-1578.md.report.md)

Fields: Result, Files, Commit, Body (optional), Fixes, Pushed, Tests, Gate, Deviation, Found, Needs, Changelog, Issue, Owner (a question for the owner), Disk freed, and last Attribution: with your commit trailer lines below it.


Generated by Claude Code

keywan-ghadami, 2026-11-15

Run: issue-1578, Result: partial: step 2 built (–bounds lists / and % proved or checked); left: step 3 (the prover and fact gaps behind std’s checked divisions, each its own issue) and step 4 (NK1303 refusal under unproved-claims, switched on once our code builds with it)

Tokens: input 184, cache read 6223045, cache write 138487, output 38881 Cost: 6.59 USD, time 0 min 14 s, turns 92

Report

Result: partial: step 2 built (–bounds lists / and % proved or checked); left: step 3 (the prover and fact gaps behind std’s checked divisions, each its own issue) and step 4 (NK1303 refusal under unproved-claims, switched on once our code builds with it) Files: crates/nikaia-std/src/tools/check_facts.nika crates/nikaia-std/src/tools/check_core_ops.nika crates/nikaia-std/src/tools/check_core_stmt.nika crates/nikaia-std/src/tools/bounds_walk.nika crates/nikaia-std/src/tools/bounds_frame.nika crates/nikaia-std/src/tools/bounds_proven.nika crates/nikaia-std/src/tools/bounds_report.nika crates/nikaia-std/src/tools/lowered/check_facts.rs crates/nikaia-std/src/tools/lowered/check_core_ops.rs crates/nikaia-std/src/tools/lowered/check_core_stmt.rs crates/nikaia-std/src/tools/lowered/bounds_walk.rs crates/nikaia-std/src/tools/lowered/bounds_frame.rs crates/nikaia-std/src/tools/lowered/bounds_proven.rs crates/nikaia-std/src/tools/lowered/bounds_report.rs crates/nikaia/src/bounds_report.rs docs/specification/30-nikaia-tooling.md tests/compiler/tests/a_divisor_is_listed_proved_or_checked.nika tests/compiler/tests/a_divisor_is_listed_proved_or_checked.bounds tests/compiler/tests/a_divisor_is_listed_proved_or_checked.stdout Commit: –bounds lists / and % as proved or checked (ADR-396 D7) Body: The checker records each /, %, /=, %= on whole numbers with a non-literal divisor in a new fact CheckFacts.divisions (kept apart from arithmetic, which drives unchecked emission: a divisor’s check is never dropped, ADR-395 D3). The bounds walk proves it with goal d != 0 and, for signed types, d != -1 || dividend > MIN (fallback: the divisor’s range), recording the site in proven.arithmetic; every reader there already gates / and % apart, so nothing emitted changes. bounds_report.nika lists the sites with / and %. Rust added: one plumbing line in crates/nikaia/src/bounds_report.rs (divisions into BoundsReportFacts), for #557/#125. Spec 30 --bounds names the divisions. Part: #1578 Pushed: local a9337462a066eb169bb764a063342b83d2ba95dd Tests: new tests/compiler a_divisor_is_listed_proved_or_checked (–both-settings ok; FAILED on the unfixed compiler, which lists no division); .bounds tests with divisions (a_name_set_only_to_the_for_binding_lies_below_it, prove_an_early_return_on_a_comparison_bounds_what_follows) ok unchanged; it bounds_report:: facts_of_the_walk:: field_paths:: prove_through_loops:: fold_counters:: expression_functions:: 40 ok; lower-std fixpoint clean; clippy, fmt clean; std tools lower-std –bounds: 19 divisions proved, 43 checked (step 3 input), none refused Gate: green Deviation: none Found: lowering defect (views lane): a let bound to an if choice of two owned String locals that are read afterwards, then returned, passes check and fails in rustc (E0515, NK1343 internal compiler error); probe: fn make(s: String) -> String { let mut g = String() g.push_str(s) return g } / fn pick(x: i64) -> String { let a = make("a") let b = make("b") let named = if a != "" { a } else { b } if x > 1 || a == b { return "" } return named } / fn main() throws { println(pick(0)) }; without the later a == b it runs Needs: none Changelog: –bounds lists every / and % whose divisor is not a literal, as proved (divisor nonzero and, signed, not -1 against MIN) or checked with the reason (ADR-396 D7, #1578); the division’s check stays either way (ADR-395 D3). Issue: broke I8: no Done when; rules-gap: step 1 (ADR-396 §4 text) and the NK1303 code were already on develop (b7abcc78), the issue does not say so Owner: none Disk freed: none (cache 7.4G kept, 5.9G free on /, above the 3G floor); new cache entries over 100 MB: rlib/df8bcfe6 288M, run-bundle/88f74747 308M, build-time-bundle/ac07d071 515M, rust-library-splice/0422fba8 692M; scratch old binary deleted Attribution: Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com Claude-Session: https://claude.ai/code/session_01UVxJDv4ha75BSeuJhmqFAL

Debrief

  1. What cost the most: three full cycles of lower-std plus build. Any change to a bounds_*.nika re-proves std, so each cycle took 10+ minutes. Two of them were lost to slips: a ref String passed to insert (NK1102), and a let bound to an if choice of two locals that the lowering turned into an E0515 rustc error. The context ran out once along the way.
  2. What I had to look up: which readers of proven.arithmetic gate / and % apart (emit_contracts, panics.nika, emit_order). I also had to learn that the arithmetic fact drives unchecked emission, so divisions needed their own fact. BoundsReportFacts is built in Rust (crates/nikaia/src/bounds_report.rs), and --bounds is in spec 30 at about line 476. The brief could have named all of these.
  3. Checks run: the probe on the old and new binaries; the new test with --bless, --both-settings, and against the old binary; the two existing .bounds tests that have divisions; six it modules; fmt, clippy and pre-push; and std’s lower-std --bounds count.
    • Wasted: the cargo test --lib bounds_report:: run, which matched 0 tests. The std count was useful, but only as input for step 3.
  4. Change that would save the most: builder.md should say “in a tools/*.nika, never bind let x = if c { a } else { b } over owned locals you read again; the lowering may borrow them”. Also: before the first cycle, run nikaia check on the edited tools file alone with the old binary. That catches NK1102-type slips in seconds instead of minutes.
  5. What the issue lacked:
    • broke I8: no Done when.
    • rules-gap: step 1 (the ADR-396 text) and NK1303 were already on develop (b7abcc78), and the issue doesn’t say so.
    • broke I11: it names no files, though the walk, the checker fact, bounds_report.nika and spec 30 are the obvious set.
    • rules-gap: no note that arithmetic drives unchecked emission, so / and % must not be added to it.
  6. Traps:
    • Fact: Lowering let x = if c { a } else { b } over owned String locals that are read again later passes check but lowers to a borrow of a temporary (E0515, NK1343); the fix in the compiler is still open.
    • Fact: Core CheckFacts.arithmetic is read by the emitter to drop checks, so a new kind of site the bounds walk should list (like divisions) needs its own fact, not an entry there.

Generated by Claude Code

keywan-ghadami, 2026-11-15

Supervisor, 2026-11-15: To build items 1 and 2 are done (the ADR-396 D7 text and NK1303 in b7abcc78; --bounds lists / and % in a9337462, pushed in 9a56560f). The rest is split:

This issue stays open until both are built.


Generated by Claude Code

Back to Open Issues