Skip to content

Commit ea5a870

Browse files
Build char literals directly instead of parsing their text
Literals in operations, operator assignments and expression refinements were turned into predicates by parsing getValue().toString(). RJ has no char-literal syntax, so '[' or ' ' raised a syntax error, 'a' was read as a variable named a, and '0' as the int 0. Wrap the value as an RJ literal node instead, as StaticConstants already does for resolved constants. Fixes #444 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f7f366c commit ea5a870

5 files changed

Lines changed: 75 additions & 3 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.Refinement;
4+
5+
@SuppressWarnings("unused")
6+
public class CorrectCharLiteralComparisons {
7+
8+
static int skipTo(String s) {
9+
int i = 0;
10+
while (s.charAt(i) != '[')
11+
i++;
12+
return i;
13+
}
14+
15+
static boolean isSeparator(char c) {
16+
return c == '{' || c == '}' || c == ',' || c == ' ' || c == 'Z' || c == '"' || c == '\\';
17+
}
18+
19+
@Refinement("_ == 48")
20+
static int zero(char c) {
21+
if (c == '0')
22+
return c;
23+
return 48;
24+
}
25+
26+
@Refinement("_ == 97")
27+
static int letterA(char c) {
28+
int a = 5;
29+
if (c == 'a')
30+
return c;
31+
return 97;
32+
}
33+
34+
@Refinement("_ == 91")
35+
static int bracket(char c) {
36+
char d = c;
37+
d += '[' - c;
38+
return d;
39+
}
40+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.Refinement;
4+
5+
@SuppressWarnings("unused")
6+
public class ErrorCharLiteralComparisons {
7+
8+
@Refinement("_ == 0")
9+
static int zero(char c) {
10+
if (c == '0')
11+
return c; // Expect: Refinement Error
12+
return 0;
13+
}
14+
15+
@Refinement("_ == 5")
16+
static int letterA(char c) {
17+
int a = 5;
18+
if (c == 'a')
19+
return c; // Expect: Refinement Error
20+
return 5;
21+
}
22+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ private Predicate getExpressionRefinements(CtExpression<?> element) throws LJErr
882882
return getRefinement(op);
883883
} else if (element instanceof CtLiteral<?>) {
884884
CtLiteral<?> l = (CtLiteral<?>) element;
885-
return new Predicate(l.getValue().toString(), l);
885+
return Predicate.createLit(l.getValue(), l);
886886
} else if (element instanceof CtInvocation<?>) {
887887
CtInvocation<?> inv = (CtInvocation<?>) element;
888888
visitCtInvocation(inv);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private Predicate getOperationRefinements(CtBinaryOperator<?> operator, CtVariab
311311
return new Predicate();
312312
}
313313

314-
return new Predicate(l.getValue().toString(), element);
314+
return Predicate.createLit(l.getValue(), element);
315315

316316
} else if (element instanceof CtInvocation<?> inv) {
317317
CtExecutable<?> method = inv.getExecutable().getDeclaration();
@@ -428,7 +428,7 @@ private Predicate getOperatorAssignmentRefinement(CtExpression<?> element) throw
428428
} else if (element instanceof CtLiteral<?> literal) {
429429
if (literal.getValue() == null)
430430
return new Predicate(); // null literals are not supported yet, carry no information
431-
return new Predicate(literal.getValue().toString(), element);
431+
return Predicate.createLit(literal.getValue(), element);
432432
} else if (element instanceof CtInvocation<?>) {
433433
VariableInstance invocationValue = (VariableInstance) element.getMetadata(Keys.TARGET);
434434
if (invocationValue != null)

‎liquidjava-verifier/src/main/java/liquidjava/rj_language/Predicate.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ public static Predicate createITE(Predicate c1, Predicate c2, Predicate c3) {
339339
return new Predicate(new Ite(c1.getExpression(), c2.getExpression(), c3.getExpression()));
340340
}
341341

342+
/**
343+
* Wrap the value of a Java literal as an RJ literal. The literal node is built directly instead of parsing the
344+
* value's text, since RJ has no char-literal syntax: {@code '['} would not parse and {@code 'a'} would read as a
345+
* variable.
346+
*/
347+
public static Predicate createLit(Object value, CtElement element) throws LJError {
348+
Predicate lit = StaticConstants.asLiteralPredicate(value);
349+
return lit != null ? lit : new Predicate(value.toString(), element);
350+
}
351+
342352
public static Predicate createLit(String value, String type) {
343353
Expression exp = switch (type) {
344354
case Types.BOOLEAN -> new LiteralBoolean(value);

0 commit comments

Comments
 (0)