Issue #1585: A range check cannot be written as a chain: 0 <= i < xs.len() is refused

Open and answer this issue on GitHub.

compiler, ready · opened 2026-11-14 · updated 2026-11-22 · 0 comments

A range check is written today as two comparisons joined by &&:

assert(i >= 0 && i < xs.len())

The shorter form from mathematics, which the website already uses in its running text (website/fast/index.html, “where the compiler has proved 0 <= i < xs.len()”), is refused:

fn get(xs: Vec[int], i: int) -> int {
	assert(0 <= i < xs.len())
	xs[i]
}

fn main() {
	println(f"{get(vec[1, 2, 3], 1)}")
}

nikaia run c.nika (binary of 2026-11-14), expected 2, actual:

error: Expected `)` here, but found `<`.
  --> c.nika:2:16

The owner wants the chained form shown on the start page and in the README (2026-11-14): it is shorter and reads like the bound it states.

Decision (supervisor, 2026-11-14, an obvious extension by the language principles)

No ADR or part of the specification refuses a chain: the grammar takes at most one comparison (cmp_tail? in cmp_expr_bare, crates/nikaia/src/parser/mod.rs) only because nothing more was decided. The extension:

To build

  1. Write the ADR (next free number) from the decision above, and the sentence in Part I (docs/specification/10-nikaia-light.md, where the comparisons are described) that states D1-D3.
  2. Parser: cmp_tail* in place of cmp_tail?, and a chain node (or the && form with the middle operand bound once) that keeps D2. A mixed chain of D3 is refused with its own error code (the supervisor reserves it) and the && form in its help.
  3. Checker, prover, bounds walk and lowering read the chain as its && form (D4); the emitted Rust binds a middle operand that is not a plain name to a local, so it is evaluated once.
  4. Tests in tests/compiler/: the probe above (prints 2); a chain of three; a middle operand with a side effect run once; a false first comparison that does not evaluate the last operand; xs[i] proved after assert(0 <= i < xs.len()), with no bounds check left in the emitted Rust; each refused form of D3 with its .stderr.
  5. Start page (website/index.html, the quicksort asserts) and README.md: write the bounds as chains, assert(0 <= lo <= hi < a.len()), and update tests/page-examples/EXPECTED.txt.

Done when

assert(0 <= i < xs.len()) builds, runs and proves the index it guards; mixed or == chains are refused with an error that shows the && form; the start page and the README show the chained asserts.

Files and limits

crates/nikaia/src/parser/mod.rs (the grammar block: Rust where the code it changes is Rust), the checker, prover and lowering parts that read a binary &&, docs/specification/10-nikaia-light.md, the new ADR, the tests, website/index.html, README.md, tests/page-examples/EXPECTED.txt. The report lists the Rust it added.

Ruled out


Generated by Claude Code

Back to Open Issues