Skip to content

Commit 5bf4604

Browse files
Treat null literals as carrying no information instead of failing
Refs #301: null literals and comparisons with null no longer raise "Null literals are not supported"; they produce a true predicate. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3a2df08 commit 5bf4604

2 files changed

Lines changed: 56 additions & 6 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package testSuite;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
6+
import liquidjava.specification.Refinement;
7+
8+
@SuppressWarnings("unused")
9+
public class CorrectNullLiterals {
10+
11+
static void describe(String label, Object value) {
12+
}
13+
14+
public static void main(String[] args) throws IOException {
15+
String name = null;
16+
if (name == null) {
17+
name = "default";
18+
}
19+
if (name != null) {
20+
describe(name, null);
21+
}
22+
23+
ByteArrayOutputStream out = null;
24+
try {
25+
out = new ByteArrayOutputStream();
26+
out.write(1);
27+
} finally {
28+
if (out != null) {
29+
out.close();
30+
}
31+
}
32+
33+
// refinements unrelated to the null literals are still checked
34+
@Refinement("x > 0")
35+
int x = 1;
36+
if (name != null) {
37+
@Refinement("y > 1")
38+
int y = x + 1;
39+
}
40+
}
41+
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.List;
55
import java.util.Optional;
66

7-
import liquidjava.diagnostics.errors.CustomError;
87
import liquidjava.diagnostics.errors.LJError;
98
import liquidjava.processor.context.RefinedFunction;
109
import liquidjava.processor.context.RefinedVariable;
@@ -80,6 +79,8 @@ public <T> void getBinaryOpRefinements(CtBinaryOperator<T> operator) throws LJEr
8079
&& ((CtAssignment<?, ?>) parent).getAssigned()instanceof CtVariableWrite<?> parentVar) {
8180
oper = getOperationRefinements(operator, parentVar, operator);
8281

82+
} else if (hasNullOperand(operator)) {
83+
oper = new Predicate(); // comparisons with null are not supported yet, carry no information
8384
} else {
8485
Predicate varLeft = getOperationRefinements(operator, left);
8586
Predicate varRight = getOperationRefinements(operator, right);
@@ -224,6 +225,8 @@ private Predicate getOperationRefinements(CtBinaryOperator<?> operator, CtVariab
224225
rtc.getContext().addVarToContext(elemName, elemVar.getType(), e, elemVar);
225226
return Predicate.createVar(returnName);
226227
} else if (element instanceof CtBinaryOperator<?> binop) {
228+
if (hasNullOperand(binop))
229+
return new Predicate();
227230
Predicate right = getOperationRefinements(operator, parentVar, binop.getRightHandOperand());
228231
Predicate left = getOperationRefinements(operator, parentVar, binop.getLeftHandOperand());
229232
return Predicate.createOperation(left, getOperatorFromKind(binop.getKind()), right);
@@ -235,12 +238,10 @@ private Predicate getOperationRefinements(CtBinaryOperator<?> operator, CtVariab
235238
return new Predicate(String.format("(%s)", s), element);
236239

237240
} else if (element instanceof CtLiteral<?> l) {
238-
if (l.getType().getQualifiedName().equals("java.lang.String")) {
239-
// skip strings
241+
if (l.getType().getQualifiedName().equals("java.lang.String") || l.getValue() == null) {
242+
// skip strings and null literals (not supported yet, carry no information)
240243
return new Predicate();
241244
}
242-
if (l.getValue() == null)
243-
throw new CustomError("Null literals are not supported", l.getPosition());
244245

245246
return new Predicate(l.getValue().toString(), element);
246247

@@ -299,6 +300,14 @@ private Predicate getOperationRefinementFromExternalLib(CtInvocation<?> inv) thr
299300
return new Predicate();
300301
}
301302

303+
private static boolean hasNullOperand(CtBinaryOperator<?> binop) {
304+
return isNullLiteral(binop.getLeftHandOperand()) || isNullLiteral(binop.getRightHandOperand());
305+
}
306+
307+
private static boolean isNullLiteral(CtExpression<?> e) {
308+
return e instanceof CtLiteral<?> l && l.getValue() == null;
309+
}
310+
302311
/**
303312
* Returns the latest symbolic value for a variable
304313
*/
@@ -327,7 +336,7 @@ private Predicate getOperatorAssignmentRefinement(CtExpression<?> element) throw
327336
return Predicate.createITE(condition, thenExpression, elseExpression);
328337
} else if (element instanceof CtLiteral<?> literal) {
329338
if (literal.getValue() == null)
330-
throw new CustomError("Null literals are not supported", literal.getPosition());
339+
return new Predicate(); // null literals are not supported yet, carry no information
331340
return new Predicate(literal.getValue().toString(), element);
332341
} else if (element instanceof CtInvocation<?>) {
333342
VariableInstance invocationValue = (VariableInstance) element.getMetadata(Keys.TARGET);

0 commit comments

Comments
 (0)