Skip to content

Commit 233721d

Browse files
Forget a variable in a catch block whose earlier values are no longer known
A catch block starts from any value a variable had in the try block, including values recorded inside a loop body, which may refer to instances of a check of the body that was dropped. With facts after the loop, those references were no longer hidden by an unknown alternative and the check failed with "Variable 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. Also reuse LoopInvariants.condition for the loop exit check. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3fed0e9 commit 233721d

4 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎liquidjava-example/src/main/java/testSuite/CorrectLoopExitFacts.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,20 @@ static void countUpWithContinue() {
2929
}
3030
ten(i);
3131
}
32+
33+
// the catch block may start from any value i had in the loop
34+
static void loopInTry(boolean fail) {
35+
int i = 0;
36+
try {
37+
while (i < 10) {
38+
if (fail)
39+
throw new RuntimeException();
40+
i++;
41+
}
42+
} catch (RuntimeException e) {
43+
}
44+
if (i != 10)
45+
return;
46+
ten(i);
47+
}
3248
}

‎liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/LoopInvariants.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ private Predicate onLastInstances(Predicate fact) {
190190
return result;
191191
}
192192

193-
private static CtExpression<Boolean> condition(CtLoop loop) {
193+
/** The condition of a loop, or null if it has none */
194+
static CtExpression<Boolean> condition(CtLoop loop) {
194195
if (loop instanceof CtWhile whileLoop)
195196
return whileLoop.getLoopingExpression();
196197
if (loop instanceof CtDo doLoop)

‎liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/RefinementTypeChecker.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,8 +1116,7 @@ private void scanBody(CtLoop loop, LoopTypestates states) {
11161116
* do-while, which exits after its body, no break leaves it, and its condition writes no variable
11171117
*/
11181118
private static boolean exitsAtHeadThroughCondition(CtLoop loop) {
1119-
CtExpression<Boolean> condition = loop instanceof CtWhile whileLoop ? whileLoop.getLoopingExpression()
1120-
: loop instanceof CtFor forLoop ? forLoop.getExpression() : null;
1119+
CtExpression<Boolean> condition = LoopInvariants.condition(loop);
11211120
return !(loop instanceof CtDo)
11221121
&& loop.getElements(new TypeFilter<>(CtBreak.class)).stream()
11231122
.noneMatch(jump -> jumpTarget(jump) == loop)

‎liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/TryChecker.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ private void addAnyOfInstances(Map<Variable, VariableInstance> before,
186186
for (VariableInstance vi : instances)
187187
anyOf = Predicate.createDisjunction(anyOf, vi.getRenamedRefinements(name));
188188
}
189+
// a recorded instance may refer to values of a block that ended (e.g. a loop body checked again), which
190+
// are no longer known: the variable is then unknown
191+
if (anyOf.getVariableNames().stream().anyMatch(n -> !n.equals(name) && !context.hasVariable(n)))
192+
anyOf = new Predicate();
189193
context.addInstanceToContext(name, v.getType(), anyOf, element);
190194
context.addRefinementInstanceToVariable(v.getName(), name);
191195
});

0 commit comments

Comments
 (0)