Issue #2826: a list grown by a for loop loses len >= 1 at the return of a…

Open and answer this issue on GitHub.

wip, bugs · opened 2026-11-23 · updated 2026-12-01 · 2 comments

Filed by the driver from the Found: of the run of issue-2782 (#2782) (owner, 2026-11-06).

Found

a list grown by a for loop loses len >= 1 at the return of a found-returns callee, for a struct field and a returned list alike (sv_sat 668 learned.lits[0], analyze builds lits = [0] and pushes in loops); probe loop2.nika: pub struct S { pub xs: Vec[i64] } pub fn l1(a: i64) -> S { let mut xs: Vec[i64] = [0] for k in 0..<a { xs.push(k) } return S { xs: xs } } pub fn l3(a: i64) -> Vec[i64] { let mut xs: Vec[i64] = [0] for k in 0..<a { xs.push(k) } return xs } pub fn r1(a: i64) -> i64 { let s = l1(a) return s.xs[0] } pub fn r3(a: i64) -> i64 { let s = l3(a) return s[0] } fn main() throws { println(r1(2) + r3(1)) }; nikaia check --bounds loop2.nika prints s.xs[0] checked and s[0] checked, should be proved since a push never shortens a list (Part I: push appends), and the same loop then xs[0] inside one function prints proved; without the loop (xs.push(a)) the caller’s read is proved; cause unknown, suspected: the main walk of bounds_rounds_settled / bounding_pass in bounds_walk.nika forgets the length at a loop’s exit (check: –bounds-trace on the return line of l1)

To build

Done when

The probe or check gives the expected output the first item names from its ADR or Part I-III section, and a tests/compiler/tests/ test (or the named check) that failed on the unfixed tree is green; or the probe already passes on develop and a test pins it, naming the commit that fixed it; or the issue is closed as by design, naming the function or ADR.


Generated by Claude Code

Comments

keywan-ghadami, 2026-12-01

Brief queued: issue-2826


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

Issue #2826

Your work order is issue #2826, 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/2826/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/bugs/issue-2826.md.report.md)

Fields: Result, Files, Commit, Body (optional), Fixes, Pushed, Tests, Gate, Deviation, Found, Needs, Changelog, Issue, Owner (a question for the owner), Comment (#N <text>, what to say on another issue; the driver posts it, one line each), Friction, Disk freed, and last Attribution: with your commit trailer lines below it.


Generated by Claude Code

keywan-ghadami, 2026-12-01

Run: issue-2826, Result: done

Tokens: input 342, cache read 10150319, cache write 238417, output 73549 Cost: 5.71 USD, time 0 min 55 s, turns 171

Report

Result: done Files: crates/nikaia-std/src/compiler/bounds_walk.nika crates/nikaia-std/src/compiler/lowered/bounds_walk.rs crates/nikaia-std/src/compiler/lowered.rs crates/nikaia-std/src/compiler/lowered/cexport.rs crates/nikaia-std/src/compiler/lowered/project_features.rs tests/compiler/tests/a_list_started_from_a_literal_is_not_empty_at_the_call.nika tests/compiler/tests/a_list_started_from_a_literal_is_not_empty_at_the_call.stdout tests/compiler/tests/a_list_started_from_a_literal_is_not_empty_at_the_call.bounds tests/compiler/tests/a_list_not_empty_is_derived.nika tests/compiler/tests/a_list_not_empty_is_derived.bounds tests/compiler/tests/prove_the_values_a_call_returns_bound_a_read.bounds Commit: A list a callee returns started from a literal and only pushed to is not empty at the call, as a returned list and as a struct’s list field (–bounds) Body: found returns get a filled measure set (len() >= 1 proved at every return, in an unguarded walk where the measuring walk took parameters at zero or more), stated unguarded at the call; a for over a range that may run no turn now keeps what held of its pushed lists’ lengths where it was reached (len >= reached), and a struct literal field given a name is as long as that name (S { xs: xs }). Four more reads of std are proved now (lowered cexport.rs, project_features.rs: crossing/core read inside), and two tests’ reads turn proved (a_list_not_empty_is_derived line 24, prove_the_values_a_call_returns_bound_a_read line 49), both sound by their callees. Fixes: #2826 Pushed: local 69e21888d56386c5ff94d0d10cc3e648d4e64ffc Tests: new a_list_started_from_a_literal_is_not_empty_at_the_call (fails on unfixed 840fd710e: all four reads checked; now 3 proved, the may-be-empty callee’s read checked); nikaia test –both-settings of it, a_list_not_empty_is_derived, prove_the_values_a_call_returns_bound_a_read, a_returned_list_lengths_are_found_facts: 4 ok; every tests/compiler/tests/*.bounds compared to check –bounds: only the two updated differ; lower-std fixpoint clean; cargo fmt, clippy, pre-push Gate: green Deviation: none Found: none Needs: none Changelog: A list a function returns that starts from a literal and is only pushed to, by a loop or not, is known not empty at the call: --bounds proves xs[0] of it, and of such a list field of a returned struct (#2826). Issue: wrong I7: without the loop the caller’s read was checked too (return [0, 1] drew a checked s[0] on 840fd710e): returned-length facts only related lengths to parameters, and a struct field given a name (S { xs: xs }) held nothing of its length Owner: none Comment: none Friction: none Disk freed: none (cache target/nikaia-project-tests 2.0G at its limit, / 11G free; no new cache entry over 100 MB) Reviewed: f0d3309ae373c4ff044332c94c098bcb1ffc89428d2419f859eaabdb605b1913 Attribution: Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com Claude-Session: https://claude.ai/code/session_01UVxJDutPTwP5Xj5HkGuidk

Debrief

  1. Most turns: the signed-count loop, for k in 0..<a with a: i64. Several 5–10 minute debug builds went into finding why the list’s pre-loop length facts were lost:
    • the loop change dropped them;
    • the fresh name xs.len()#N sat under the list’s name, so the element check rejected it.

    The struct form then needed one more debug build. It turned out a struct literal field given a name stated nothing about its length.

  2. Looked up:
    • how found returns are measured (BoundsReturns, the measuring walk, guards);
    • bounding_built / bounding_for in bounds_walk.nika;
    • TermArena.mentions (an exact name match);
    • the .bounds file format;
    • that eprintln inside the compiler needs catch {}.

    A pointer to bounding_return_lengths and to bounding_measured (the call side) would have saved several reads.

  3. Checks:
    • Useful:
      • probes per shape (st.nika, c.nika);
      • the debug prints of the return’s facts, which found both causes;
      • comparing every .bounds against check --bounds;
      • the unfixed-binary run;
      • the 4 touched tests at both settings;
      • fmt, clippy and pre-push.
    • Wasted:
      • one build lost to eprintln without catch;
      • one build of the wrong others hypothesis (I wrongly assumed mentions matches by prefix).
  4. Change that would have saved the most: builder-facts should say how to trace a prover walk’s facts at a return (the facts held there, through walk.arena.text). --bounds-trace LINE shows only the facts at the caller’s read.
  5. What the issue lacked:
    • wrong I7: it says the read is proved without the loop; on 840fd710e return [0, 1] also drew a checked s[0] at the caller.
    • broke I7: it named no suspected function (bounding_return_lengths, bounding_built).
  6. Traps:
    • Fact: Ledger and bounds proofs A fresh prover name made of a list’s length plus a suffix (xs.len()#N) is treated as one of that list’s places, so the loop’s element check drops facts stated in it; name it apart (#reachedN).
    • Fact: Core eprintln in a compiler .nika draws NK2605 at lower-std unless written eprintln(...) catch {}.
    • Fact: Ledger and bounds proofs --bounds-trace LINE shows only the facts at the read on LINE, never a callee’s return walk; debug those with prints in bounding_return_lengths.
  7. For the next builder in this area:
    • Fact: Ledger and bounds proofs Found returns: bounding_returns_round (measuring and plain walk), bounding_return_lengths (callee side), bounding_measured (call side) in bounds_walk.nika; a struct literal field’s length comes from bounding_built.
    • Fact: Ledger and bounds proofs Compare all expectations quickly: run check --bounds on each tests/compiler/tests/*.nika that has a .bounds, from tests/compiler, and diff it with the suffix ` - written without their check: …` stripped (195 files, a few minutes, 2026-12-01, commit 69e21888d).

Generated by Claude Code

Back to Open Issues