Skip to content

Commit 8e8ed29

Browse files
Treat results of methods without refinements as unconstrained in operations
Fixes the crash when an invocation of a method with no refinements is an operand of a binary operation (#300). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3a2df08 commit 8e8ed29

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package testSuite;
2+
3+
import java.util.List;
4+
5+
// Results of methods without refinements can be used in operations (issue #300)
6+
@SuppressWarnings("unused")
7+
public class CorrectUnknownMethodInComparison {
8+
public void lengthInComparison(String s) {
9+
if (s.length() < 3) {
10+
System.out.println("short");
11+
}
12+
}
13+
14+
public void equalsInDisjunction(String s) {
15+
boolean known = s.equals("a") || s.equals("b");
16+
}
17+
18+
public void sizeInLoopBound(List<String> list) {
19+
for (int i = 0; i < list.size(); i++) {
20+
System.out.println(list.get(i));
21+
}
22+
}
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.Refinement;
4+
5+
// Results of methods without refinements carry no information (issue #300)
6+
@SuppressWarnings("unused")
7+
public class ErrorUnknownMethodInOperation {
8+
public void lengthPlusOne(String s) {
9+
@Refinement("_ > 0")
10+
int x = s.length() + 1; // Expect: Refinement Error
11+
}
12+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ private Predicate getOperationRefinements(CtBinaryOperator<?> operator, CtVariab
253253
// Get function refinements with non_used variables
254254
String met = ((CtClass<?>) method.getParent()).getQualifiedName(); // TODO check
255255
RefinedFunction fi = rtc.getContext().getFunction(method.getSimpleName(), met, inv.getArguments().size());
256+
if (fi == null)
257+
return getUnconstrainedInvocationVariable(inv);
256258
Predicate innerRefs = fi.getRenamedRefinements(rtc.getContext(), inv); // TODO REVIEW!!
257259

258260
// Substitute _ by the variable that we send
@@ -265,6 +267,16 @@ private Predicate getOperationRefinements(CtBinaryOperator<?> operator, CtVariab
265267
// TODO Maybe add cases
266268
}
267269

270+
/**
271+
* Creates a fresh variable with no information (refinement true) to represent the result of an invocation of a
272+
* method without refinements
273+
*/
274+
private Predicate getUnconstrainedInvocationVariable(CtInvocation<?> inv) {
275+
String newName = String.format(Formats.FRESH, rtc.getContext().getCounter());
276+
rtc.getContext().addVarToContext(newName, inv.getType(), new Predicate(), inv);
277+
return new Predicate(newName, inv);
278+
}
279+
268280
private Predicate getOperationRefinementFromExternalLib(CtInvocation<?> inv) throws LJError {
269281

270282
CtExpression<?> t = inv.getTarget();
@@ -279,6 +291,8 @@ private Predicate getOperationRefinementFromExternalLib(CtInvocation<?> inv) thr
279291
String methodInClassName = typeNotParametrized + "." + simpleName;
280292
RefinedFunction fi = rtc.getContext().getFunction(methodInClassName, typeNotParametrized,
281293
inv.getArguments().size());
294+
if (fi == null)
295+
return getUnconstrainedInvocationVariable(inv);
282296
Predicate innerRefs = fi.getRenamedRefinements(rtc.getContext(), inv); // TODO REVIEW!!
283297

284298
// Substitute _ by the variable that we send

0 commit comments

Comments
 (0)