Skip to content

Commit b7ad615

Browse files
Handle unknown invocations on non-variable targets and interface methods
Invocations whose target is not a variable (chained or static calls such as s.trim().length() or Math.max(a, b)) used as operands now also get an unconstrained fresh variable instead of `true`, which crashed Z3 with a BoolExpr/ArithExpr cast. Methods declared in interfaces no longer crash with a ClassCastException when looking up the declaring type. Add negative tests mirroring the positive ones, including branch conditions over unknown method results and typestate preservation. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8e8ed29 commit b7ad615

5 files changed

Lines changed: 199 additions & 4 deletions

File tree

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
package testSuite;
22

33
import java.util.List;
4+
import java.util.Map;
5+
6+
import liquidjava.specification.Refinement;
47

58
// Results of methods without refinements can be used in operations (issue #300)
69
@SuppressWarnings("unused")
710
public class CorrectUnknownMethodInComparison {
11+
interface Shape {
12+
int area();
13+
}
14+
15+
int helper() {
16+
return 1;
17+
}
18+
819
public void lengthInComparison(String s) {
920
if (s.length() < 3) {
10-
System.out.println("short");
21+
@Refinement("_ > 0")
22+
int y = 1;
23+
} else {
24+
@Refinement("_ > 0")
25+
int z = 1;
1126
}
1227
}
1328

@@ -20,4 +35,46 @@ public void sizeInLoopBound(List<String> list) {
2035
System.out.println(list.get(i));
2136
}
2237
}
38+
39+
public void boxedResult(List<Integer> list) {
40+
if (list.get(0) > 3) {
41+
@Refinement("_ > 0")
42+
int y = 1;
43+
}
44+
}
45+
46+
public void booleanResultInConjunction(Map<String, Integer> map, String k, int n) {
47+
if (map.containsKey(k) && n > 0) {
48+
@Refinement("_ > 0")
49+
int y = n;
50+
}
51+
}
52+
53+
public void staticCall(int a, int b) {
54+
if (Math.max(a, b) > 0) {
55+
@Refinement("_ > 0")
56+
int y = 1;
57+
}
58+
}
59+
60+
public void chainedCall(String s) {
61+
if (s.trim().length() > 0) {
62+
@Refinement("_ > 0")
63+
int y = 1;
64+
}
65+
}
66+
67+
public void implicitThisCall() {
68+
if (helper() > 0) {
69+
@Refinement("_ > 0")
70+
int y = 1;
71+
}
72+
}
73+
74+
public void interfaceMethod(Shape shape) {
75+
if (shape.area() > 0) {
76+
@Refinement("_ > 0")
77+
int y = 1;
78+
}
79+
}
2380
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.StateRefinement;
4+
import liquidjava.specification.StateSet;
5+
6+
// Calling a method without refinements in a condition keeps the object's state (issue #300)
7+
@StateSet({"open", "closed"})
8+
public class CorrectUnknownMethodInComparisonState {
9+
@StateRefinement(to = "open(this)")
10+
public CorrectUnknownMethodInComparisonState() {}
11+
12+
@StateRefinement(from = "open(this)", to = "closed(this)")
13+
public void close() {}
14+
15+
public int count() {
16+
return 0;
17+
}
18+
19+
public static void main(String[] args) {
20+
CorrectUnknownMethodInComparisonState r = new CorrectUnknownMethodInComparisonState();
21+
if (r.count() > 0) {
22+
System.out.println("non-empty");
23+
}
24+
r.close();
25+
}
26+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package testSuite;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import liquidjava.specification.Refinement;
7+
8+
// Results of methods without refinements are unconstrained, so no branch is dead (issue #300)
9+
@SuppressWarnings("unused")
10+
public class ErrorUnknownMethodInComparison {
11+
interface Shape {
12+
int area();
13+
}
14+
15+
int helper() {
16+
return 1;
17+
}
18+
19+
public void thenBranch(String s) {
20+
if (s.length() < 3) {
21+
@Refinement("_ > 0")
22+
int y = -1; // Expect: Refinement Error
23+
}
24+
}
25+
26+
public void elseBranch(String s) {
27+
if (s.length() < 3) {
28+
} else {
29+
@Refinement("_ > 0")
30+
int z = -1; // Expect: Refinement Error
31+
}
32+
}
33+
34+
public void equalsInDisjunction(String s) {
35+
if (s.equals("a") || s.equals("b")) {
36+
@Refinement("_ > 0")
37+
int y = -1; // Expect: Refinement Error
38+
}
39+
}
40+
41+
public void boxedResult(List<Integer> list) {
42+
if (list.get(0) > 3) {
43+
} else {
44+
@Refinement("_ > 0")
45+
int y = -1; // Expect: Refinement Error
46+
}
47+
}
48+
49+
public void booleanResultInConjunction(Map<String, Integer> map, String k, int n) {
50+
if (map.containsKey(k) && n > 0) {
51+
@Refinement("_ > 0")
52+
int y = n - 1; // Expect: Refinement Error
53+
}
54+
}
55+
56+
public void staticCall(int a, int b) {
57+
if (Math.max(a, b) > 0) {
58+
} else {
59+
@Refinement("_ > 0")
60+
int y = -1; // Expect: Refinement Error
61+
}
62+
}
63+
64+
public void chainedCall(String s) {
65+
if (s.trim().length() > 0) {
66+
@Refinement("_ > 0")
67+
int y = -1; // Expect: Refinement Error
68+
}
69+
}
70+
71+
public void implicitThisCall() {
72+
if (helper() > 0) {
73+
} else {
74+
@Refinement("_ > 0")
75+
int y = -1; // Expect: Refinement Error
76+
}
77+
}
78+
79+
public void interfaceMethod(Shape shape) {
80+
if (shape.area() > 0) {
81+
@Refinement("_ > 0")
82+
int y = -1; // Expect: Refinement Error
83+
}
84+
}
85+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.StateRefinement;
4+
import liquidjava.specification.StateSet;
5+
6+
// Calling a method without refinements in a condition keeps the object's state (issue #300)
7+
@StateSet({"open", "closed"})
8+
public class ErrorUnknownMethodInComparisonState {
9+
@StateRefinement(to = "open(this)")
10+
public ErrorUnknownMethodInComparisonState() {}
11+
12+
@StateRefinement(from = "open(this)", to = "closed(this)")
13+
public void close() {}
14+
15+
public int count() {
16+
return 0;
17+
}
18+
19+
public static void main(String[] args) {
20+
ErrorUnknownMethodInComparisonState r = new ErrorUnknownMethodInComparisonState();
21+
r.close();
22+
if (r.count() > 0) {
23+
System.out.println("non-empty");
24+
}
25+
r.close(); // Expect: State Refinement Error
26+
}
27+
}

‎liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/general_checkers/OperationsChecker.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
import spoon.reflect.code.CtVariableWrite;
3838
import spoon.reflect.code.UnaryOperatorKind;
3939
import spoon.reflect.declaration.CtAnnotation;
40-
import spoon.reflect.declaration.CtClass;
4140
import spoon.reflect.declaration.CtElement;
4241
import spoon.reflect.declaration.CtExecutable;
42+
import spoon.reflect.declaration.CtType;
4343
import spoon.reflect.declaration.ParentNotInitializedException;
4444
import spoon.reflect.reference.CtVariableReference;
4545
import spoon.support.reflect.code.CtIfImpl;
@@ -251,7 +251,7 @@ private Predicate getOperationRefinements(CtBinaryOperator<?> operator, CtVariab
251251
return getOperationRefinementFromExternalLib(inv);
252252

253253
// Get function refinements with non_used variables
254-
String met = ((CtClass<?>) method.getParent()).getQualifiedName(); // TODO check
254+
String met = method.getParent(CtType.class).getQualifiedName(); // TODO check
255255
RefinedFunction fi = rtc.getContext().getFunction(method.getSimpleName(), met, inv.getArguments().size());
256256
if (fi == null)
257257
return getUnconstrainedInvocationVariable(inv);
@@ -310,7 +310,7 @@ private Predicate getOperationRefinementFromExternalLib(CtInvocation<?> inv) thr
310310
rtc.getContext().addVarToContext(newName, fi.getType(), innerRefs, inv);
311311
return new Predicate(newName, inv); // Return variable that represents the invocation
312312
}
313-
return new Predicate();
313+
return getUnconstrainedInvocationVariable(inv);
314314
}
315315

316316
/**

0 commit comments

Comments
 (0)