Skip to content

fix(verify): diagnostic for x=+x 1 mutation pattern in @ loops (ILO-450)#750

Merged
danieljohnmorris merged 1 commit into
mainfrom
fix/x-mutation-in-loop
May 23, 2026
Merged

fix(verify): diagnostic for x=+x 1 mutation pattern in @ loops (ILO-450)#750
danieljohnmorris merged 1 commit into
mainfrom
fix/x-mutation-in-loop

Conversation

@danieljohnmorris
Copy link
Copy Markdown
Collaborator

Summary

  • Adds ILO-T047 warning when a Stmt::Let inside a @binding loop rebinds the loop's own iterator variable (e.g. x=+x 1 inside @x xs or @x 0..10)
  • The loop engine resets the iterator from its own cursor each iteration, so the rebind is silently discarded — classic wrong-output footgun
  • Hint suggests the correct accumulator pattern (acc=0;@x ...{acc=+acc x}) or fold
  • Nested loops: only the innermost binding triggers the warning (outer-var rebind inside an inner loop is not flagged)

Test plan

  • loop_var_rebind_foreach_warns — fires on @x xs{x=+x 1}
  • loop_var_rebind_forrange_warns — fires on @i 0..10{i=+i 1}
  • loop_var_rebind_other_binding_no_warn — accumulator pattern acc=+acc x is clean
  • loop_var_rebind_nested_inner_warns — inner loop var warned, not outer
  • loop_var_rebind_outer_in_nested_no_warn — outer var rebind inside inner loop not flagged
  • All 3499 existing unit tests pass

Closes ILO-450

🤖 Generated with Claude Code

@danieljohnmorris danieljohnmorris added the mini Created by mini PC autonomous workflow label May 23, 2026
@danieljohnmorris
Copy link
Copy Markdown
Collaborator Author

mini pc is reviewing this

Reapplies the loop-rebind diagnostic from the original PR #750 on top of
current main. Renumbered T047 → T048 because main already uses T047 for
two earlier-merged diagnostics (silent brace-fallthrough and sum-variant
constructors in list literals).

`x = +x 1` inside `@x xs` is the canonical wrong-output footgun: the
loop engine resets the iterator from its cursor each iteration, so the
rebind is silently discarded. The verifier now warns at the innermost
loop binding only; outer-var rebinds inside an inner loop are fine.

Adds 5 unit tests covering foreach + range + nested + non-iterator
rebinds + accumulator pattern. Registry entry documents the canonical
fixes (accumulator name, fld).

Closes ILO-450.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@danieljohnmorris danieljohnmorris force-pushed the fix/x-mutation-in-loop branch from 0788fab to 35ac265 Compare May 23, 2026 22:10
@danieljohnmorris danieljohnmorris merged commit 1a18f22 into main May 23, 2026
7 of 10 checks passed
@danieljohnmorris danieljohnmorris deleted the fix/x-mutation-in-loop branch May 23, 2026 22:11
@codecov
Copy link
Copy Markdown

codecov Bot commented May 23, 2026

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
3808 1 3807 0
View the top 1 failed test(s) by shortest run time
ilo::coverage_interpreter::mapr_short_circuits_on_err
Stack Traces | 0.014s run time
thread 'mapr_short_circuits_on_err' (42149) panicked at tests/coverage_interpreter.rs:950:5:
stderr={"code":"ILO-P024","labels":[{"col":49,"end":52,"line":1,"message":"here","primary":true,"start":48}],"message":"fn declarations are top-level only; this one is inside another function's body","notes":[],"severity":"error","suggestion":"use an inline lambda for a one-off helper that captures locals (e.g. `proc = (x:n>n; +x rows)` or `proc = {x> +x rows}`), or lift the helper to the top level and pass the captured value as an explicit parameter"}
{"code":"ILO-T005","labels":[{"col":64,"end":79,"line":1,"message":"","primary":true,"start":63}],"message":"undefined function 'f': its definition failed to parse","notes":["in function 'main'"],"severity":"error","suggestion":"fix the parse error first (see ILO-P024 reported earlier in this file); other references to 'f' are suppressed until then"}

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mini Created by mini PC autonomous workflow

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant