Repository navigation
Mark inferred loop facts in errors, and hint to state a loop invariant - #560
CatarinaGamboa wants to merge 2 commits into
Conversation
A refinement error now lists, for clients, the facts of Found that loop invariant inference added, with the value they are about, the loop and where in it they hold (start of an iteration, end of the body, after the loop). The message text does not change. A prototype hint is added to an error in a loop whose counterexample is about an unrefined int local the loop changes, when expected holds on the first iteration: it says the loop only keeps what holds on every iteration, and suggests expected as a refinement of the declaration when it can be written for the variable and holds before the loop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex review, round 1Codex (
|
Clean-code reviewAudit with the clean-code-principles skill, on
No SRP or layering issues: inference records the facts, |
Codex review, round 2Codex reviewed the whole diff again, including the new linking filter in |
Closes #554. Part of #548 (PM decisions in its comments) and the loop invariant plan #542.
Stacked on #556 (base
perf/547-loop-cost). Retarget tomainonce #556 merges.Part 1: inferred facts are marked in the diagnostic data
The terminal/hover message does not change. Inferred facts stay in Found as ordinary facts.
RefinementError.getInferredFacts()returns oneInferredFactsper value in Found's original chain that inference gave facts to:variableis the internal name of the binder, e.g.#i_5.error.getDisplayNames().get(variable)givesi@46#1, the name the panel already shows.factsare the conjuncts of that binder's refinement that inference added, e.g.i@46#1 >= i@45. Each one is equal to a conjunct of the binder's refinement in the Original step, so the panel can match them.loopis the loop's position, which gives its line and lets the panel link to it.pointsays where in the loop the facts hold:ITERATION_START: at the loop head, on every iteration.ITERATION_END: where the body'scontinues join.AFTER_LOOP: the invariants plus the negated condition, after a loop withoutbreak(Know the invariants and the negated condition after a loop without break #552).Facts are recorded on the
VariableInstancewhenLoopInvariantsadds them.VCCheckerthen keeps those that are still conjuncts of a binder in the error's original chain, for binders linked to the terms of expected (the same linking as Found's counterexample filter). Invariants of unrelated loop variables are not listed.Why this shape suits vscode-liquidjava#209.
loopandpointand render one step before the simplification steps, e.g. "Inferred for the loop at line 46 (holds on every iteration):i@46#1 >= i@45".AFTER_LOOPwould read "holds after the loop".i@46#1) to its loop line and point. That is what the panel needs to explain a loop-start name, e.g. "the value ofiat the start of an iteration of the loop at line 46".The DTO is left to #209. A sketch:
Part 2: hint to state a loop invariant (prototype)
The hint is added when an error in a loop body is finally reported (not on trial checks) and all of these hold:
intlocal declared without@Refinement. As with inference, fields and parameters are out of scope.Text:
`left` changes in the loop at line 8, and LiquidJava only keeps what holds for it on every iteration. If `left >= 0` should hold on every iteration, state it on the declaration: `@Refinement("_ >= 0") int left = 10;`. Afor-init variable getsfor (@Refinement("_ >= 0") int i = 0; ...). The hint's kind isLOOP_INVARIANT, and "line N" is aSourceReferenceto the loop.Before / after
The message, snippet and counterexample are identical before and after. The only change is the added hint, which starts with "
Xchanges in the loop at line N, and LiquidJava only keeps what holds for it on every iteration." The table shows the rest of the hint. "After following the hint" is the verifier's result once the suggested refinement is added.for (int i = 0; i < 10; i++) use(i), thewhile (total < 1024)accumulator, and the buffer loop withif (n < 0) break;n < 0check (total += n,read(buf, total, ...)needsoff >= 0)found `total < limit` but expected `total >= 0`· cextotal == -1 && limit == 0If total >= 0 should hold…: @Refinement("_ >= 0") int total = 0;total += nnmay be negative)left = left - 3; wait(left)(_ >= 0), from 10found `left@8#1 <= 10 && left@8#1 > 0 && left == left@8#1 - 3` but expected `left >= 0`· cexleft == -1 && left@8#1 == 2If left >= 0 …: @Refinement("_ >= 0") int left = 10;left = left - 3sleep(delay)(_ > 0),delay = delay > 60 ? 1 : delay * 2expected `delay > 0` but could not prove it· cexdelay == 0If delay > 0 …: @Refinement("_ > 0") int delay = 8;delay >= 8)left--; done++; progress(n, done)(done <= n)found `done@9 >= 0 && n >= 0 && left <= n && left >= 0 && left > 0 && done == done@9 + 1` but expected `done <= n`· cexn == 1 && done == 2 && done@9 == 1 && left == 1If done <= n …: @Refinement("_ <= n") int done = 0;done++left + done == n, which no single declaration can state. The suggestion is a dead endtotal = total + read()(unrefinedread),report(total)found `total == total@10 + #read_result` but expected `total >= 0`· cextotal == -1 && total@10 == -1 && #read_result == 0read's refinementfor (i = 0; i <= size; i++) get(size, i)(_ < size)found `size >= 0 && i <= size && i >= 0` but expected `i >= 0 && i < size`· cexsize == 0 && i == 0budget = budget - price()(unrefined),pay(budget)found `budget@10#1 > 0 && budget == budget@10#1 - #price_result` but expected `budget >= 0`· cexbudget == -1 && budget@10#1 == 1 && #price_result == 2int shifted = i - 1; percent(shifted)inwhile (i < n)found `i >= 0 && i < n && shifted == i - 1` but expected `shifted >= 0 && shifted <= 100`while (p < 100) p = p + 30; percent(p)after the loopfound `p >= 0 && !(p < 100)` but expected `p >= 0 && p <= 100`AFTER_LOOPErrorLoopUnprovedInvarianttotal = total + read(); nonNeg(total)found `total == total@16 + #read_result` but expected `total >= 0`ErrorLoopInvariantsdecrementing,for (int i = 10; i > -5; i--) use(i)found `i > -5 && i <= 10 && i >= -5` but expected `i >= 0`· cexi == -1If i >= 0 …: for (@Refinement("_ >= 0") int i = 10; ...)i--i--show it does not holdErrorLoopInvariantsresetInBody / decreasedOnSomePath / decreasedBeforeContinue / decreasedBeforeContinueInFor / decreasedInInnerLoop (lines 25, 35, 55, 71, 78)found `i < 10 && i <= 10` but expected `i >= 0`(35:found `i < 10`) · cexi == -1If i >= 0 …: @Refinement("_ >= 0") int i = 0;(71: thefor (…; ...)form)i = -1,i = i - 2, …)ErrorLoopInvariantsboundNotKepti = i + 3; atMostTen(i)found `i@46#1 >= 0 && i@46#1 < 10 && i == i@46#1 + 3` but expected `i <= 10`· cexi == 11 && i@46#1 == 8If i <= 10 …: @Refinement("_ <= 10") int i = 0;i = i + 3ErrorLoopConditionincrementInBodyi++; lessThanTen(i)found `i@77#1 >= 0 && i@77#1 <= 10 && i@77#1 < 10 && i == i@77#1 + 1` but expected `i < 10`If i < 10 …: @Refinement("_ < 10") int i = 0;i++ErrorLoopConditionlaterIteration (int k = read(),k > 0needed) / innerLoopInDoWhile (int n = 0,n > 0needed)ErrorLoopConditionlaterIterationFromKnownStart (@Refinement("_ > 0") int y = x - 1)y, notxfor-init variable already refined,for (@Refinement("_ >= 0") int i = 10; …; i--)ErrorEmptyLoopBody,ErrorLoopExitFacts, theErrorLoopState*fixtures,ErrorLoopPostState,ErrorLoopFreshReceivers, and theclasses/*fixtures with loops)Honest verdict on the hint
left/donecounters). The suggestion is a dead end there, and I found no cheap way to detect that.k = read(),n = 0in the do-while, they/xcase).On balance it helps more than it misleads on these examples. Still, its main value on real bugs is to localise them, not to fix them. Your call whether that is worth a hint.
Tests
TestLoopInvariantDiagnostics:ITERATION_STARTfacts and their loop (ErrorLoopInvariants:48).AFTER_LOOPfacts (newpastTheBoundcase inErrorLoopExitFacts).ErrorLoopInvariants:25).ErrorLoopUnprovedInvariant).ErrorLoopUnprovedInvariant).verify-cur-redesign(55 variants): verdicts and messages are identical. No shipped task gets the hint.🤖 Generated with Claude Code