Repository navigation
Commit 84dd629
Know the invariants and the negated condition after a loop without break (#552)
Stacked on #551; review/merge after it.
Fixes #545
Part of the loop invariant inference plan (#542).
## What and why
After a loop, the variables it changes were havocked again: only their
declared refinements were known, so after `int i = 0; while (i < n)
i++;` nothing said `i >= n`. Now, after a loop that can only exit at its
head through its condition, the instances after the loop get the
invariants inferred in #551 and the negated condition:
- `exitsAtHeadThroughCondition`: a `while`, `for` or for-each loop (not
a `do`-`while`), with no `break` whose target is the loop (labelled or
not), and a condition that writes no variable.
- `InvariantCandidates.condition` translates the condition when it is
built of `&&`, `||`, `!` and comparisons (`<`, `<=`, `>`, `>=`, `==`,
`!=`) of the terms a candidate may mention (int literals, int locals and
parameters, `+`, `-`, `*`); otherwise only the invariants are added.
- `LoopInvariants.afterLoop` adds them to the post-loop instances (after
the existing havoc), like the head facts.
Also (found by the Codex review): `TryChecker` starts a catch block from
any value a variable had in the try block, including values recorded in
loop-body checks that name join instances of a dropped check. The
post-loop instance used to be `true`, which hid them; with facts after
the loop they surfaced as `Variable #i_11 could not be found` (already
the case on main for a refined variable). The variable is now unknown in
the catch block when such a value is among them.
## Soundness argument
- The invariants hold at the loop head on every visit (#551), including
the last one, where the condition is evaluated to false and the loop
exits.
- For `while`/`for` without `break`, the code after the loop is reached
only from that point (a `return`, `throw` or labelled jump to an outer
statement does not reach it), and the condition writes nothing, so the
state after the loop is the head state where the condition was false:
the invariants and the negated condition describe it.
- A `do`-`while` exits after its body and condition, where the head
invariants need not hold (`do { i = i + 5; } while (i < 3);` keeps `i <=
3` at the head but exits with `i == 5`), so it is excluded. A condition
with a side effect (`while (i++ < 10)`) exits in a state after the head,
so it is excluded too.
- The negated condition is built from the same Java expression with pure
terms only, so it means what the condition meant at the exit.
- Making a variable unknown in a catch block only removes facts.
## Before / after
`int i = 0; while (i < n) i++; atLeast(i, n);` (`@Refinement("_ <= i")
int n`): before `Refinement Error` (only `i`'s declared refinement
known), after `Correct`.
`for (; i < 10; i++) { if (next()) continue; } ten(i);` (`_ == 10`):
before `Refinement Error`, after `Correct` (invariant `i <= 10` and `!(i
< 10)`).
## Tests
- `CorrectLoopExitFacts` (the first two fail on #551 alone): `i >= n`
after counting up with `while`; `i == 10` after a `for` whose body has a
continue; a loop in a try with a catch (spurious `Not Found Error`
without the `TryChecker` change).
- `ErrorLoopExitFacts`: no negated condition after a loop with `break`;
none after a `do`-`while` (the example above); none after a condition
that increments (`while (i++ < 10)` exits with `i == 11`).
- `InvariantCandidatesTest.translatesConditionsOverTheSameTerms`: `!`,
`||`, `&&`, `==`, `!=` translated; conditions with calls or a literal
`true` not.
Full suite passes locally.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>1 parent 3e46ea2 commit 84dd629
7 files changed
Lines changed: 161 additions & 1 deletion
File tree
- liquidjava-example/src/main/java/testSuite
- liquidjava-verifier/src
- main/java/liquidjava/processor/refinement_checker
- test/java/liquidjava/processor/refinement_checker
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
99 | 129 | | |
100 | 130 | | |
101 | 131 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
135 | 145 | | |
136 | 146 | | |
137 | 147 | | |
| |||
180 | 190 | | |
181 | 191 | | |
182 | 192 | | |
183 | | - | |
| 193 | + | |
| 194 | + | |
184 | 195 | | |
185 | 196 | | |
186 | 197 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1015 | 1015 | | |
1016 | 1016 | | |
1017 | 1017 | | |
| 1018 | + | |
| 1019 | + | |
1018 | 1020 | | |
1019 | 1021 | | |
1020 | 1022 | | |
| |||
1109 | 1111 | | |
1110 | 1112 | | |
1111 | 1113 | | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
1112 | 1126 | | |
1113 | 1127 | | |
1114 | 1128 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
189 | 193 | | |
190 | 194 | | |
191 | 195 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
70 | 79 | | |
71 | 80 | | |
72 | 81 | | |
| |||
0 commit comments