Skip to content

Commit 9835c3a

Browse files
Keep a static final constant's name in refinement errors
Folding replaced every resolved constant with its value, so Found showed mode == 2 where the code wrote ImageWriteParam.MODE_EXPLICIT. A constant is now replaced by its value only when the expression around it folds (MODE_EXPLICIT + 1 becomes 3); otherwise it keeps its name. The SMT encoding is unchanged: the constant is still bound to its value. The counterexample filter compares a named constant in Found with its value in the model, so mode == 2 is not repeated as a counterexample. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9aa2467 commit 9835c3a

5 files changed

Lines changed: 94 additions & 11 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package testSuite;
2+
3+
import javax.imageio.ImageWriteParam;
4+
5+
import liquidjava.specification.Refinement;
6+
7+
@SuppressWarnings("unused")
8+
public class ErrorStaticFinalConstantName {
9+
10+
static void setProgressiveMode(@Refinement("_ != ImageWriteParam.MODE_EXPLICIT") int mode) {
11+
}
12+
13+
static void direct() {
14+
setProgressiveMode(ImageWriteParam.MODE_EXPLICIT); // Expect: Refinement Error
15+
}
16+
17+
static void throughLocal() {
18+
int mode = ImageWriteParam.MODE_EXPLICIT;
19+
setProgressiveMode(mode); // Expect: Refinement Error
20+
}
21+
}

‎liquidjava-verifier/src/main/java/liquidjava/diagnostics/errors/RefinementError.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,11 @@ private static boolean isKnown(Pair<String, String> assignment, List<Expression>
187187
|| states(binary.getSecondOperand(), binary.getFirstOperand(), assignment)));
188188
}
189189

190-
// An enum constant is written Format.JPG in found, either as such or as a variable, and JPG in the model
190+
// An enum constant is written Format.JPG in found, either as such or as a variable, and JPG in the model; a static
191+
// final constant keeps its name in found and is its value in the model
191192
private static boolean states(Expression term, Expression value, Pair<String, String> assignment) {
193+
if (value instanceof Enum constant && constant.getResolvedLiteral() != null)
194+
value = constant.getResolvedLiteral();
192195
String valueName = value instanceof Enum constant ? constant.getConstName()
193196
: value instanceof Var var ? Utils.getSimpleName(var.getName()) : value.toString();
194197
return assignment.first().equals(termName(term)) && assignment.second().equals(valueName);

‎liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/VCFolding.java‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ protected Expression simplify(Expression expression, Void context) {
2323
* Folds the first foldable expression found
2424
*/
2525
private Expression fold(Expression expression) {
26-
// enum constant -> literal
27-
if (expression instanceof Enum en && en.getResolvedLiteral() != null)
28-
return en.getResolvedLiteral().clone();
2926
if (expression instanceof BinaryExpression binary)
3027
return foldBinary(binary);
3128
if (expression instanceof UnaryExpression unary)
@@ -51,7 +48,7 @@ private Expression foldBinary(BinaryExpression binary) {
5148

5249
String op = binary.getOperator();
5350

54-
Expression foldedBinary = foldLiteralBinary(left, right, op);
51+
Expression foldedBinary = foldLiteralBinary(valueOf(left), valueOf(right), op);
5552
if (foldedBinary != null)
5653
return foldedBinary;
5754

@@ -72,17 +69,18 @@ private Expression foldUnary(UnaryExpression unary) {
7269
return new UnaryExpression(unary.getOp(), foldedOperand);
7370

7471
String op = unary.getOp();
72+
Expression value = valueOf(operand);
7573

7674
// !true -> false
7775
// !false -> true
78-
if ("!".equals(op) && operand instanceof LiteralBoolean literal)
76+
if ("!".equals(op) && value instanceof LiteralBoolean literal)
7977
return new LiteralBoolean(!literal.isBooleanTrue());
8078

8179
if ("-".equals(op)) {
8280
// -(x) -> -x
83-
if (operand instanceof LiteralInt literal)
81+
if (value instanceof LiteralInt literal)
8482
return new LiteralInt(-literal.getValue());
85-
if (operand instanceof LiteralReal literal)
83+
if (value instanceof LiteralReal literal)
8684
return new LiteralReal(-literal.getValue());
8785
}
8886

@@ -97,7 +95,7 @@ private Expression foldIte(Ite ite) {
9795

9896
// true ? x : y -> x
9997
// false ? x : y -> y
100-
if (condition instanceof LiteralBoolean literal)
98+
if (valueOf(condition)instanceof LiteralBoolean literal)
10199
return literal.isBooleanTrue() ? ite.getThen().clone() : ite.getElse().clone();
102100

103101
Expression foldedCondition = fold(condition);
@@ -121,6 +119,14 @@ private Expression foldIte(Ite ite) {
121119
return new Ite(condition, thenExpression, elseExpression);
122120
}
123121

122+
/**
123+
* The value of a static final constant, which only replaces its name when the expression around it folds, so a
124+
* constant compared with a variable keeps the name the code or the spec wrote
125+
*/
126+
private Expression valueOf(Expression expression) {
127+
return expression instanceof Enum en && en.getResolvedLiteral() != null ? en.getResolvedLiteral() : expression;
128+
}
129+
124130
/**
125131
* Folds a binary expression whose operands are both literals
126132
*/
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package liquidjava.api.tests;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.Map;
6+
import java.util.TreeMap;
7+
8+
import org.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.api.Test;
10+
11+
import liquidjava.api.CommandLineLauncher;
12+
import liquidjava.diagnostics.Diagnostics;
13+
import liquidjava.diagnostics.errors.LJError;
14+
import liquidjava.diagnostics.errors.RefinementError;
15+
16+
/** Checks that a static final constant the code passes is shown by its name, as the spec writes it */
17+
class TestStaticConstantNames {
18+
19+
private static final Map<Integer, RefinementError> ERRORS = new TreeMap<>();
20+
21+
@BeforeAll
22+
static void verify() {
23+
CommandLineLauncher.launch("../liquidjava-example/src/main/java/testSuite/ErrorStaticFinalConstantName.java");
24+
for (LJError error : Diagnostics.getInstance().getErrors())
25+
ERRORS.put(error.getPosition().getLine(), assertInstanceOf(RefinementError.class, error));
26+
}
27+
28+
@Test
29+
void constantPassedDirectlyKeepsItsName() {
30+
assertShowsName(ERRORS.get(14));
31+
}
32+
33+
@Test
34+
void constantPassedThroughLocalKeepsItsName() {
35+
assertShowsName(ERRORS.get(19));
36+
}
37+
38+
private static void assertShowsName(RefinementError error) {
39+
assertEquals("mode == ImageWriteParam.MODE_EXPLICIT is not a subtype of mode != ImageWriteParam.MODE_EXPLICIT",
40+
error.getMessage().replaceAll("[⁰¹²³⁴⁵⁶⁷⁸⁹]", ""));
41+
assertTrue(error.getCounterexample().isEmpty());
42+
}
43+
}

‎liquidjava-verifier/src/test/java/liquidjava/rj_language/opt/VCFoldingTest.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import liquidjava.rj_language.ast.BinaryExpression;
77
import liquidjava.rj_language.ast.Enum;
88
import liquidjava.rj_language.ast.LiteralInt;
9+
import liquidjava.rj_language.ast.Var;
910
import org.junit.jupiter.api.Test;
1011

1112
class VCFoldingTest {
@@ -120,7 +121,7 @@ void foldsResolvedEnumLiterals() {
120121
VCImplication implication = new VCImplication(
121122
new Predicate(new BinaryExpression(limit, "==", new LiteralInt(3))));
122123

123-
assertSimplificationSteps(folding, implication, step("3 == 3"), step("true"));
124+
assertSimplificationSteps(folding, implication, step("true"));
124125
}
125126

126127
@Test
@@ -131,6 +132,15 @@ void foldsResolvedEnumLiteralsInsideLargerExpression() {
131132
VCImplication implication = new VCImplication(
132133
new Predicate(new BinaryExpression(arithmetic, "==", new LiteralInt(5))));
133134

134-
assertSimplificationSteps(folding, implication, step("3 + 2 == 5"), step("5 == 5"), step("true"));
135+
assertSimplificationSteps(folding, implication, step("5 == 5"), step("true"));
136+
}
137+
138+
@Test
139+
void keepsResolvedEnumNameWhenNothingFolds() {
140+
Enum mode = new Enum("ImageWriteParam", "MODE_EXPLICIT");
141+
mode.setResolvedLiteral(new LiteralInt(2));
142+
VCImplication implication = new VCImplication(new Predicate(new BinaryExpression(new Var("x"), "==", mode)));
143+
144+
assertSimplificationSteps(folding, implication, step("x == ImageWriteParam.MODE_EXPLICIT"));
135145
}
136146
}

0 commit comments

Comments
 (0)