Issue #643: [v; n]: a list or an array of n copies of a value that copies (ADR-362 D1-D4)

Open and answer this issue on GitHub.

ready · opened 2026-10-17 · updated 2026-10-17 · 0 comments

How it came up

The owner wrote let safety_buffer = [0..181] for 181 numbers (the loop program of #640). That is a list of one range; printing an element is NK1201 (#638), indexing it with 110 aborts. Asking how to write 181 equal elements without a loop showed a gap, checked with the compiler on 2026-10-17:

Written Today
(0..<181).map(fn(_i) { 0 }).collect() Vec of 181 zeros
let mut xs = [] then xs.resize(181, 0) Vec of 181 zeros (lowered to vec[0; 181], solver-workload §8.1)
let xs: Array[i64, 181] = (0..<181).collect() NK1103
[0; 181] syntax error Expected ] here, but found ;

An Array[T, N] of more than a handful of elements cannot be written at all, though ADR-152 made it the container without allocation.

Decision (owner, 2026-10-17)

To build

  1. Parser: [expr; expr] as a list literal of its own kind (no other ; inside brackets). Test: [0; 3], [x + 1; n], nested [[0; 2]; 3]; [1, 2; 3] is a syntax error.
  2. Checker: the type from the use as every list literal (ADR-152 D4) — no use: Vec[T]; Array[T, N] wanted: n must be a comptime integer equal to N, else NK1157 naming both (also for an n the build does not know). For a Vec, n is any whole number type. Tests: let b: Array[u8, 4096] = [0; 4096] builds; [0; 4095] there is NK1157; let v = [0; n] with a parameter n is a Vec[i64].
  3. NK1280: v not a value that copies (String, Vec, a struct owning one); the help writes (0..<n).map(fn(_i) { v.clone() }).collect(). Test: [name; 3] with name: String refused with the help; [(1, 2); 3] and [Point { x: 0, y: 0 }; 3] (a value struct of numbers) build.
  4. Lowering: vec[v; n as usize] for a Vec (a negative n aborts as an index out of range does, ADR-293: check before the cast), [v; N] for an array. Test: the output of a program printing len() and elements; a negative length aborts with the index message.
  5. Prover: len() of [v; n] is n (and N for an array). Test: let xs = [0; 181]; assert(xs.len() == 181) proved; an index b with 0 <= b && b < 181 known needs no check.
  6. tools/*.nika: replace the resize workarounds where one builds a list of equal values, if any (grep -n 'resize('); not required.

The owner’s program then writes let safety_buffer = [0; 181].

Related: #640, #638; the warning for [0..181] (ADR-362 D5) is its own issue.

Back to Open Issues