Skip to content

Commit 803f614

Browse files
Keep the form of a ghost named like a state of another set in state errors
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4673074 commit 803f614

4 files changed

Lines changed: 46 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 liquidjava.specification.Ghost;
4+
import liquidjava.specification.StateRefinement;
5+
import liquidjava.specification.StateSet;
6+
7+
// a ghost named like a state of another class is not shown as that state
8+
@Ghost("boolean ready")
9+
public class ErrorStateValueGhostName {
10+
11+
@StateRefinement(from = "ready(this)")
12+
void use() {}
13+
14+
static void test() {
15+
ErrorStateValueGhostName sensor = new ErrorStateValueGhostName();
16+
sensor.use(); // Expect: State Refinement Error
17+
}
18+
}
19+
20+
@StateSet(name = "phase", value = {"ready", "busy"})
21+
class ErrorStateValueGhostNameMachine {}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ protected void throwStateRefinementError(SourcePosition position, SourcePosition
464464
VCSimplificationResult shown = VCSimplification.simplifyOnce(simplified,
465465
new VCRelevantFacts(expected.getExpression(), this::ghostFunctionsOf));
466466
throw new StateRefinementError(position, declarationPosition, expected, shown, map, customMessage, hints,
467-
category, context, e -> StateValues.toSetForm(e, this.context.getGhostStates()));
467+
category, context,
468+
e -> StateValues.toSetForm(e, this.context.getGhostStates(), this.context.getGhosts()));
468469
}
469470

470471
/** The ghost functions an invocation name stands for: a state's state set function, or the ghost itself */

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,30 +96,36 @@ private Optional<FunctionInvocation> stateOf(Expression set, Expression name) th
9696
/**
9797
* {@code expression} with each state of a named set, {@code open(e)}, written as a value of its set,
9898
* {@code phase(e) == open}, and {@code !open(e)} as {@code phase(e) != open}, for display. A state keeps the
99-
* shorthand when its set has a generated name, such as {@code state1}, or when sets with different names have a
100-
* state of that name
99+
* shorthand when its set has a generated name, such as {@code state1}, when sets with different names have a state
100+
* of that name, or when a ghost that is not a state has that name: the name alone does not tell which it is
101+
*
102+
* @param ghosts
103+
* the ghosts that are not states, and the state set functions
101104
*/
102-
public static Expression toSetForm(Expression expression, List<GhostState> states) {
103-
return rewriteToSetForm(expression.clone(), states);
105+
public static Expression toSetForm(Expression expression, List<GhostState> states, List<GhostFunction> ghosts) {
106+
return rewriteToSetForm(expression.clone(), states, ghosts);
104107
}
105108

106-
private static Expression rewriteToSetForm(Expression e, List<GhostState> states) {
109+
private static Expression rewriteToSetForm(Expression e, List<GhostState> states, List<GhostFunction> ghosts) {
107110
if (e instanceof UnaryExpression u && u.getOp().equals("!")) {
108-
Optional<BinaryExpression> value = setValue(u.getExpression(), "!=", states);
111+
Optional<BinaryExpression> value = setValue(u.getExpression(), "!=", states, ghosts);
109112
if (value.isPresent())
110113
return value.get();
111114
}
112-
Optional<BinaryExpression> value = setValue(e, "==", states);
115+
Optional<BinaryExpression> value = setValue(e, "==", states, ghosts);
113116
if (value.isPresent())
114117
return value.get();
115118
for (int i = 0; i < e.getChildren().size(); i++)
116-
e.setChild(i, rewriteToSetForm(e.getChildren().get(i), states));
119+
e.setChild(i, rewriteToSetForm(e.getChildren().get(i), states, ghosts));
117120
return e;
118121
}
119122

120123
/** {@code set(e) op state} when {@code e} is the state {@code state(e)} of a set with one, given name */
121-
private static Optional<BinaryExpression> setValue(Expression e, String op, List<GhostState> states) {
122-
if (!(e instanceof FunctionInvocation invocation && invocation.getArgs().size() == 1))
124+
private static Optional<BinaryExpression> setValue(Expression e, String op, List<GhostState> states,
125+
List<GhostFunction> ghosts) {
126+
if (!(e instanceof FunctionInvocation invocation && invocation.getArgs().size() == 1)
127+
|| ghosts.stream().anyMatch(g -> g.matches(invocation.getName()))
128+
|| states.stream().anyMatch(s -> s.getParent() == null && s.matches(invocation.getName())))
123129
return Optional.empty();
124130
List<String> setNames = states.stream().filter(s -> s.getParent() != null && s.matches(invocation.getName()))
125131
.map(s -> s.getParent().getName()).distinct().toList();

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,13 @@ void queryWrittenWithItsStateSetIsOfferedAsAGuard() {
354354
new Hint(Kind.GUARD, "Guard this call with if (d.isUnlocked()): when it returns true, d is unlocked"));
355355
}
356356

357+
@Test
358+
void ghostNamedLikeAStateOfAnotherClassKeepsItsForm() {
359+
StateRefinementError error = errors("ErrorStateValueGhostName.java", 1).get(0);
360+
assertEquals("found ready(sensor) == false but expected ready(sensor)",
361+
error.getMessage().replaceAll("[⁰¹²³⁴⁵⁶⁷⁸⁹]", ""));
362+
}
363+
357364
@Test
358365
void classWithoutStateSetKeepsTheGeneralHint() {
359366
assertHints("ErrorUnconstrainedStateRefinement.java", Hint.general(

0 commit comments

Comments
 (0)