Skip to content

Commit 36ef288

Browse files
Keep generated names apart from program variables with a ## prefix
A program variable named like a generated value (read_result) has instances #read_result_N, which the name pattern took for generated. Generated names now start with ##, which no instance name can, and the integration test fails on a missing or extra error. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6eb04af commit 36ef288

9 files changed

Lines changed: 53 additions & 44 deletions

File tree

‎liquidjava-verifier/src/main/java/liquidjava/rj_language/ast/formatter/VariableFormatter.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.util.regex.Matcher;
44
import java.util.regex.Pattern;
55

6-
import liquidjava.utils.GeneratedNames;
7-
86
/**
97
* Formats internal variable names into a cleaner display representation using superscript notation
108
*/
@@ -30,7 +28,7 @@ public static String format(String name) {
3028
String receiver = FieldDisplayNames.writeReceiver(name);
3129
if (receiver != null && isField(baseName))
3230
return (receiver.isEmpty() ? "" : receiver + ".") + fieldOf(baseName) + toSuperscript(counter);
33-
String prefix = isGenerated(name, baseName) ? "#" : "";
31+
String prefix = isReturn(baseName) ? "#" : "";
3432
return prefix + formatField(baseName) + toSuperscript(counter);
3533
}
3634

@@ -82,8 +80,8 @@ private static String toSuperscript(String number) {
8280
return sb.toString();
8381
}
8482

85-
private static boolean isGenerated(String name, String baseName) {
86-
return GeneratedNames.isGenerated(name) || baseName.equals("ret");
83+
private static boolean isReturn(String id) {
84+
return id.equals("ret");
8785
}
8886

8987
private static boolean isApplication(String name) {

‎liquidjava-verifier/src/main/java/liquidjava/utils/GeneratedNames.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
/**
1313
* Names of the values the verifier introduces, such as path conditions and call results. Each is written
14-
* {@code #<what the value is>_<counter>}, so that error messages say what the value stands for; the counter keeps it
15-
* unique and {@code #} keeps it apart from the variables of the program.
14+
* {@code ##<what the value is>_<counter>}, so that error messages say what the value stands for. The counter keeps it
15+
* unique, and the second {@code #} keeps it apart from the instances of program variables,
16+
* {@code #<variable>_<counter>}, as a Java name cannot start with {@code #}.
1617
*/
1718
public final class GeneratedNames {
1819
public static final String IF_CONDITION = "if_condition";
@@ -28,27 +29,26 @@ public final class GeneratedNames {
2829
private static final String OPERATION_RESULT = "operation" + RESULT;
2930
private static final String EXPRESSION_VALUE = "expression" + VALUE;
3031

31-
private static final Pattern GENERATED = Pattern
32-
.compile("^#(" + NEW + "\\w+|\\w+_(condition|branch|length|result|value))_[0-9]+$");
32+
private static final String PREFIX = "##";
3333
private static final Pattern NOT_IN_IDENTIFIER = Pattern.compile("[^A-Za-z0-9_]");
3434

3535
private GeneratedNames() {
3636
}
3737

3838
public static String of(String description, int counter) {
39-
return String.format("#%s_%d", NOT_IN_IDENTIFIER.matcher(description).replaceAll("_"), counter);
39+
return String.format(PREFIX + "%s_%d", NOT_IN_IDENTIFIER.matcher(description).replaceAll("_"), counter);
4040
}
4141

4242
/**
43-
* Names the value of an expression after what it is: {@code #size_result_3} for a call to {@code size},
44-
* {@code #count_value_3} for a read of field {@code count}, {@code #new_File_3} for a new {@code File}
43+
* Names the value of an expression after what it is: {@code ##size_result_3} for a call to {@code size},
44+
* {@code ##count_value_3} for a read of field {@code count}, {@code ##new_File_3} for a new {@code File}
4545
*/
4646
public static String valueOf(CtExpression<?> expression, int counter) {
4747
return of(describe(expression), counter);
4848
}
4949

5050
public static boolean isGenerated(String name) {
51-
return GENERATED.matcher(name).matches();
51+
return name.startsWith(PREFIX);
5252
}
5353

5454
private static String describe(CtExpression<?> expression) {

‎liquidjava-verifier/src/test/java/liquidjava/api/tests/TestCounterexamples.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void fieldArgumentKeepsTheField() {
116116
@Test
117117
void callInsideArgumentKeepsItsResultAfterTheArgument() {
118118
RefinementError error = verify("ErrorCounterexampleCallArgument.java");
119-
assertAssignments(error, assignment("#timeout", "-1"), assignment("#read_result", "0"));
119+
assertAssignments(error, assignment("#timeout", "-1"), assignment("##read_result", "0"));
120120
}
121121

122122
@Test

‎liquidjava-verifier/src/test/java/liquidjava/api/tests/TestGeneratedNames.java‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,75 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5+
import java.util.List;
56
import java.util.Map;
67
import java.util.TreeMap;
8+
import java.util.stream.Collectors;
79

810
import org.junit.jupiter.api.BeforeAll;
911
import org.junit.jupiter.api.Test;
1012

1113
import liquidjava.api.CommandLineLauncher;
1214
import liquidjava.diagnostics.Diagnostics;
13-
import liquidjava.diagnostics.errors.LJError;
1415

1516
/**
1617
* Checks that the messages for {@code classes/generated_names_error} name each value the verifier introduces after what
1718
* it is, without the instance counters that depend on verification order
1819
*/
1920
class TestGeneratedNames {
2021

21-
private static final Map<Integer, String> MESSAGES = new TreeMap<>();
22+
private static Map<Integer, String> messages;
2223

2324
@BeforeAll
2425
static void verify() {
2526
CommandLineLauncher.launch("../liquidjava-example/src/main/java/testSuite/classes/generated_names_error");
26-
for (LJError error : Diagnostics.getInstance().getErrors())
27-
MESSAGES.put(error.getPosition().getLine(), plain(error.getMessage()));
27+
// a second error on a line fails here instead of replacing the first
28+
messages = Diagnostics.getInstance().getErrors().stream().collect(
29+
Collectors.toMap(error -> error.getPosition().getLine(), error -> plain(error.getMessage()), (a, b) -> {
30+
throw new AssertionError("two errors on one line: " + a + " and " + b);
31+
}, TreeMap::new));
32+
}
33+
34+
@Test
35+
void reportsOneErrorPerOrigin() {
36+
assertEquals(List.of(25, 30, 34, 43, 48, 54, 58), List.copyOf(messages.keySet()));
2837
}
2938

3039
@Test
3140
void ifConditionIsNamedAfterTheIf() {
32-
assertEquals("#if_condition == ready && #if_condition is not a subtype of a > 0", MESSAGES.get(25));
41+
assertEquals("#if_condition == ready && #if_condition is not a subtype of a > 0", messages.get(25));
3342
}
3443

3544
@Test
3645
void loopConditionIsNamedAfterTheLoop() {
37-
assertEquals("#loop_condition == ready && #loop_condition == true is not a subtype of a > 0", MESSAGES.get(30));
46+
assertEquals("#loop_condition == ready && #loop_condition == true is not a subtype of a > 0", messages.get(30));
3847
}
3948

4049
@Test
4150
void ternaryConditionIsNamedAfterTheTernary() {
4251
assertEquals("#ternary_condition == ready && #ternary_condition == true is not a subtype of a < 0",
43-
MESSAGES.get(34));
52+
messages.get(34));
4453
}
4554

4655
@Test
4756
void tryAndCatchPathsAreNamedAfterTheirBranches() {
48-
assertEquals("!#try_branch && !#catch_branch is not a subtype of a > 0", MESSAGES.get(43));
57+
assertEquals("!#try_branch && !#catch_branch is not a subtype of a > 0", messages.get(43));
4958
}
5059

5160
@Test
5261
void callResultIsNamedAfterTheMethod() {
53-
assertEquals("x == #read_result - 1 is not a subtype of x > 0", MESSAGES.get(48));
62+
assertEquals("x == #read_result - 1 is not a subtype of x > 0", messages.get(48));
5463
}
5564

5665
@Test
5766
void untranslatedOperationIsNamedAsAnOperationResult() {
5867
assertEquals("#if_condition == #operation_result && #if_condition && x == 0 is not a subtype of x > 0",
59-
MESSAGES.get(54));
68+
messages.get(54));
6069
}
6170

6271
@Test
6372
void newReceiverIsNamedAfterItsClass() {
64-
assertEquals("found closed(#new_Door) but expected opened(#new_Door)", MESSAGES.get(58));
73+
assertEquals("found closed(#new_Door) but expected opened(#new_Door)", messages.get(58));
6574
}
6675

6776
private static String plain(String text) {

‎liquidjava-verifier/src/test/java/liquidjava/diagnostics/errors/RefinementErrorCounterexampleTest.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ void keepsFoundConjunctsLinkedThroughAnEqualityChain() {
4545
@Test
4646
void omitsUnmentionedPathBinderAndListsGeneratedNamesLast() {
4747
RefinementError error = error("t >= 0",
48-
vc("∀#read_result_1:int. true", "∀port:int. true", "∀#if_condition_2:boolean. port > 0",
49-
"∀t:int. t == #read_result_1 + port", "true"),
50-
"#if_condition_2=false", "t=-1", "#read_result_1=-2", "port=1");
51-
assertEquals(List.of(pair("t", "-1"), pair("port", "1"), pair("#read_result_1", "-2")),
48+
vc("∀##read_result_1:int. true", "∀port:int. true", "∀##if_condition_2:boolean. port > 0",
49+
"∀t:int. t == ##read_result_1 + port", "true"),
50+
"##if_condition_2=false", "t=-1", "##read_result_1=-2", "port=1");
51+
assertEquals(List.of(pair("t", "-1"), pair("port", "1"), pair("##read_result_1", "-2")),
5252
error.getCounterexample().assignments());
5353
}
5454

@@ -91,7 +91,7 @@ void hasNoCounterexampleWhenFoundStatesEveryValue() {
9191
void splitsNegatedDisjunctionsSoEachGuardLinksOnlyItsOwnVariables() {
9292
RefinementError error = error("t >= 0",
9393
vc("∀port:int. true", "∀retries:int. true", "∀t:int. true",
94-
"∀#if_condition_1:boolean. !(port < 0 || retries < 0 || t > 100)", "true"),
94+
"∀##if_condition_1:boolean. !(port < 0 || retries < 0 || t > 100)", "true"),
9595
"port=0", "retries=0", "t=-1");
9696
assertEquals(List.of(pair("t", "-1")), error.getCounterexample().assignments());
9797
}
@@ -100,7 +100,7 @@ void splitsNegatedDisjunctionsSoEachGuardLinksOnlyItsOwnVariables() {
100100
void splitsNestedConjunctionsAndDoubleNegations() {
101101
RefinementError error = error("t >= 0",
102102
vc("∀port:int. true", "∀retries:int. true", "∀backoff:int. true", "∀t:int. true",
103-
"∀#if_condition_1:boolean. !(port < 0 || !!(retries > 3 && backoff > 1) || t > 100)", "true"),
103+
"∀##if_condition_1:boolean. !(port < 0 || !!(retries > 3 && backoff > 1) || t > 100)", "true"),
104104
"port=0", "retries=0", "backoff=0", "t=-1");
105105
assertEquals(List.of(pair("t", "-1")), error.getCounterexample().assignments());
106106
}
@@ -109,7 +109,7 @@ void splitsNestedConjunctionsAndDoubleNegations() {
109109
void keepsAGuardThatRelatesExpectedToAnotherVariable() {
110110
RefinementError error = error("t >= 0",
111111
vc("∀port:int. true", "∀retries:int. true", "∀t:int. true",
112-
"∀#if_condition_1:boolean. !(port < 0 || retries > t)", "true"),
112+
"∀##if_condition_1:boolean. !(port < 0 || retries > t)", "true"),
113113
"port=0", "retries=-2", "t=-1");
114114
assertEquals(List.of(pair("t", "-1"), pair("retries", "-2")), error.getCounterexample().assignments());
115115
}

‎liquidjava-verifier/src/test/java/liquidjava/rj_language/ast/formatter/VariableFormatterTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ void forgetRecordedNames() {
1616
void formatsVariables() {
1717
assertEquals("x", VariableFormatter.format("x"));
1818
assertEquals("x²", VariableFormatter.format("#x_2"));
19-
assertEquals("#if_condition¹²", VariableFormatter.format("#if_condition_12"));
20-
assertEquals("#size_result⁴", VariableFormatter.format("#size_result_4"));
19+
assertEquals("#if_condition¹²", VariableFormatter.format("##if_condition_12"));
20+
assertEquals("#size_result⁴", VariableFormatter.format("##size_result_4"));
21+
assertEquals("size_result⁴", VariableFormatter.format("#size_result_4"));
2122
assertEquals("#ret³", VariableFormatter.format("#ret_3"));
2223
assertEquals("this#Class", VariableFormatter.format("this#Class"));
2324
// ghost applications
2425
assertEquals("size(s²)", VariableFormatter.format("size(#s_2)"));
2526
assertEquals("size(bag)", VariableFormatter.format("size(bag)"));
26-
assertEquals("at(list³, #get_result¹²)", VariableFormatter.format("at(#list_3, #get_result_12)"));
27+
assertEquals("at(list³, #get_result¹²)", VariableFormatter.format("at(#list_3, ##get_result_12)"));
2728
assertEquals("size(this)", VariableFormatter.format("size(this)"));
2829
assertEquals("f(g(a¹, b²), c³)", VariableFormatter.format("f(g(#a_1, #b_2), #c_3)"));
2930
assertEquals("f(g(h(a¹)), b)", VariableFormatter.format("f(g(h(#a_1)),b)"));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void removesTrueBinderWhenVariableIsUnusedDownstream() {
3030

3131
@Test
3232
void removesFreshPathBinderWhenVariableIsUnusedDownstream() {
33-
assertSimplificationSteps(binderSimplification, vc("∀#if_condition_1:boolean. #if_condition_1", "y > 0"),
33+
assertSimplificationSteps(binderSimplification, vc("∀##if_condition_1:boolean. ##if_condition_1", "y > 0"),
3434
step("y > 0"));
3535
}
3636

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ void removesConstraintImpliedByEarlierAntecedent() {
4343
@Test
4444
void removesCoverageConstraintsImpliedByStrongerLaterConstraints() {
4545
assertSimplificationSteps(simplification,
46-
vc("∀x:int. x >= 0", "∀#if_condition_40:boolean. !(y < 40)", "∀#if_condition_60:boolean. !(y < 60)",
47-
"∀#if_condition_80:boolean. !(y < 80)", "x + y > 0"),
46+
vc("∀x:int. x >= 0", "∀##if_condition_40:boolean. !(y < 40)", "∀##if_condition_60:boolean. !(y < 60)",
47+
"∀##if_condition_80:boolean. !(y < 80)", "x + y > 0"),
4848
step("x >= 0", "!(y < 60)", "!(y < 80)", "x + y > 0"), step("x >= 0", "!(y < 80)", "x + y > 0"));
4949
}
5050

@@ -74,7 +74,7 @@ void doesNotUseConclusionToSimplifyConstraint() {
7474
@Test
7575
void simplifiesOnlyFirstImpliedConstraint() {
7676
assertSimplificationSteps(simplification,
77-
vc("∀x:int. x > 2", "∀#if_condition_1:boolean. x > 1", "∀#if_condition_2:boolean. x > 0", "y > 0"),
77+
vc("∀x:int. x > 2", "∀##if_condition_1:boolean. x > 1", "∀##if_condition_2:boolean. x > 0", "y > 0"),
7878
step("x > 2", "x > 0", "y > 0"), step("x > 2", "y > 0"));
7979
}
8080
}

‎liquidjava-verifier/src/test/java/liquidjava/utils/GeneratedNamesTest.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class GeneratedNamesTest {
1010

1111
@Test
1212
void namesTheValueAfterWhatItIs() {
13-
assertEquals("#if_condition_7", GeneratedNames.of(GeneratedNames.IF_CONDITION, 7));
14-
assertEquals("#catch_branch_12", GeneratedNames.of(GeneratedNames.CATCH_BRANCH, 12));
13+
assertEquals("##if_condition_7", GeneratedNames.of(GeneratedNames.IF_CONDITION, 7));
14+
assertEquals("##catch_branch_12", GeneratedNames.of(GeneratedNames.CATCH_BRANCH, 12));
1515
}
1616

1717
@Test
1818
void replacesCharactersTheRefinementLanguageDoesNotAccept() {
19-
assertEquals("#new_Map_Entry_3", GeneratedNames.of("new_Map$Entry", 3));
20-
assertEquals("#t_ma_result_4", GeneratedNames.of("tæma_result", 4));
19+
assertEquals("##new_Map_Entry_3", GeneratedNames.of("new_Map$Entry", 3));
20+
assertEquals("##t_ma_result_4", GeneratedNames.of("tæma_result", 4));
2121
}
2222

2323
@Test
@@ -31,7 +31,8 @@ void recognizesEveryGeneratedName() {
3131
@Test
3232
void doesNotTakeProgramVariablesForGeneratedNames() {
3333
assertFalse(GeneratedNames.isGenerated("#x_3"));
34-
assertFalse(GeneratedNames.isGenerated("#condition_3"));
34+
assertFalse(GeneratedNames.isGenerated("#if_condition_3"));
35+
assertFalse(GeneratedNames.isGenerated("#read_result_3"));
3536
assertFalse(GeneratedNames.isGenerated("#ret_3"));
3637
assertFalse(GeneratedNames.isGenerated("#this_3"));
3738
assertFalse(GeneratedNames.isGenerated("if_condition"));

0 commit comments

Comments
 (0)