Skip to content

Commit d65da22

Browse files
State hints: name only the state the call leads to, and no guard when a call sequence works
- A call-sequence hint now reads "Call S before this call; afterwards <state>", with no "from" part: the state before is in the message. - A step whose condition gives its only parameter a value is written as the call with that value, e.g. wParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT). - No guard hint when a sequence of calls reaches a state that allows the call: wrapping the call in the check would skip it where it fails. - The guard reads "Guard this call with if (<check>); inside it, <state>". - The calls allowed now are listed only when no other hint is given. Closes #518 Closes #519 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 538ac47 commit d65da22

3 files changed

Lines changed: 124 additions & 98 deletions

File tree

‎liquidjava-example/src/main/java/testSuite/ErrorStateHintGuards.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ static void severalChecks() {
7171
GuardCoarse g = GuardCoarse.make();
7272
g.run(); // Expect: State Refinement Error
7373
}
74+
75+
// a call reaches the needed state on every path: the check is not suggested, as it would skip the call where it fails
76+
static void reachableOnEveryPath(boolean shown) {
77+
GuardFrame f = new GuardFrame();
78+
if (shown)
79+
f.show();
80+
f.decorate(); // Expect: State Refinement Error
81+
}
7482
}
7583

7684
@StateSet({"open", "closed"})
@@ -216,3 +224,21 @@ public GuardReader() {}
216224
@StateRefinement(from = "eof(this)")
217225
public void finish() {}
218226
}
227+
228+
@StateSet({"hidden", "shown"})
229+
class GuardFrame {
230+
@StateRefinement(to = "hidden(this)")
231+
public GuardFrame() {}
232+
233+
@StateRefinement(to = "shown(this)")
234+
public void show() {}
235+
236+
@StateRefinement(to = "hidden(this)")
237+
public void dispose() {}
238+
239+
@StateRefinement(to = "return ? shown(this) : hidden(this)")
240+
public boolean isShown() { return true; }
241+
242+
@StateRefinement(from = "hidden(this)")
243+
public void decorate() {}
244+
}

‎liquidjava-verifier/src/main/java/liquidjava/diagnostics/hints/StateHints.java‎

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import liquidjava.processor.state_machine.State;
2121
import liquidjava.processor.state_machine.StateMachine;
2222
import liquidjava.rj_language.Predicate;
23+
import liquidjava.rj_language.ast.BinaryExpression;
24+
import liquidjava.rj_language.ast.Expression;
25+
import liquidjava.rj_language.ast.Var;
2326
import liquidjava.utils.Utils;
2427
import liquidjava.utils.constants.Keys;
2528
import spoon.reflect.reference.CtExecutableReference;
@@ -89,12 +92,17 @@ public static List<Hint> forCall(StateMachine machine, RefinedFunction call, Set
8992
if (category == ErrorCategory.UNKNOWN || checkedElsewhere)
9093
return hints.anyState(current, call, checkedElsewhere);
9194
boolean pathDependent = category == ErrorCategory.PATH_DEPENDENT_STATE;
92-
List<Hint> sequences = hints.howToReach(current, call, false,
95+
List<Hint> sequences = hints.howToReach(current, call,
9396
() -> machine.complete() ? List.of(hints.unreachable(current, call, pathDependent)) : List.of());
94-
// in a known state, a check is suggested only when no sequence of calls reaches a state that allows the call
97+
// a check is suggested only when no sequence of calls reaches a state that allows the call: wrapping the call
98+
// in it would skip the call in the states where it fails
9599
boolean reachable = sequences.stream().anyMatch(h -> h.kind() == Kind.CALL_SEQUENCE);
96-
List<Hint> guards = reachable && current.size() == 1 ? List.of() : hints.guard(current, call);
97-
return Stream.of(sequences, guards, hints.allowedCalls(current)).flatMap(List::stream).toList();
100+
List<Hint> found = reachable ? sequences
101+
: Stream.concat(sequences.stream(), hints.guard(current, call).stream()).toList();
102+
// the calls allowed now are listed only when nothing else is suggested
103+
Stream<Hint> allowed = hints.allowedCalls(current).stream()
104+
.filter(h -> found.isEmpty() || h.kind() != Kind.AVAILABLE_CALLS);
105+
return Stream.concat(found.stream(), allowed).toList();
98106
}
99107

100108
/** The earlier call whose arguments decided the receiver's state, and values that would allow the call */
@@ -127,20 +135,17 @@ private List<Hint> anyState(Set<State> current, RefinedFunction call, boolean ch
127135
List<Hint> hints = new ArrayList<>();
128136
hints.add(new Hint(Kind.NEEDED_STATE, message));
129137
hints.addAll(guard(current, call));
130-
hints.addAll(howToReach(current, call, true, List::of));
138+
hints.addAll(howToReach(current, call, List::of));
131139
return hints;
132140
}
133141

134142
/**
135143
* The shortest sequences of calls that are allowed in every state of {@code current} and lead to {@code call}
136144
*
137-
* @param fromAnyState
138-
* whether to name {@code current} as any state
139145
* @param unreachable
140146
* the hints to give if no sequence exists
141147
*/
142-
private List<Hint> howToReach(Set<State> current, RefinedFunction call, boolean fromAnyState,
143-
Supplier<List<Hint>> unreachable) {
148+
private List<Hint> howToReach(Set<State> current, RefinedFunction call, Supplier<List<Hint>> unreachable) {
144149
Optional<List<List<CallVariant>>> search = machine.shortestPaths(current, call, MAX_SEQUENCES);
145150
if (search.isEmpty())
146151
return List.of();
@@ -149,7 +154,7 @@ private List<Hint> howToReach(Set<State> current, RefinedFunction call, boolean
149154
return unreachable.get();
150155
if (sequences.get(0).size() > MAX_SEQUENCE_LENGTH)
151156
return List.of();
152-
return sequences.stream().map(sequence -> callSequence(current, sequence, fromAnyState)).toList();
157+
return sequences.stream().map(sequence -> callSequence(current, sequence)).toList();
153158
}
154159

155160
/**
@@ -172,12 +177,11 @@ private List<Hint> guard(Set<State> current, RefinedFunction call) {
172177
if (guards.size() != 1)
173178
return List.of();
174179
CallVariant guard = guards.get(0);
175-
boolean negated = guard.negatedResult();
176-
String check = (negated ? "!" : "") + guardCall(guard.method());
180+
String check = (guard.negatedResult() ? "!" : "") + guardCall(guard.method());
177181
String arguments = guard.method().getArguments().isEmpty() ? ""
178182
: ", filling in the arguments of " + declaration(guard.method());
179-
String message = String.format("Guard this call with if (%s)%s: when it returns %s, %s", check, arguments,
180-
!negated, (asValues ? "" : receiver + " is ") + needed());
183+
String message = String.format("Guard this call with if (%s)%s; inside it, %s", check, arguments,
184+
isIn(needed()));
181185
return List.of(new Hint(Kind.GUARD, message));
182186
}
183187

@@ -208,16 +212,18 @@ private boolean namesCheckedState(RefinedFunction method) {
208212
return refinements.anyMatch(p -> StateMachine.mayName(p, checked));
209213
}
210214

211-
/** The states before and after {@code sequence} are named only in the state sets it changes */
212-
private Hint callSequence(Set<State> current, List<CallVariant> sequence, boolean fromAnyState) {
215+
/**
216+
* The state after {@code sequence} is named only in the state sets it changes. The state before is in the error's
217+
* message.
218+
*/
219+
private Hint callSequence(Set<State> current, List<CallVariant> sequence) {
213220
Set<State> reached = current;
214221
for (CallVariant step : sequence)
215222
reached = machine.after(reached, step);
216223
Set<Integer> changed = machine.stateSetsChanged(current, reached);
217-
String from = fromAnyState ? "any state" : join(machine.describeOnly(current, changed));
218224
String calls = sequence.stream().map(this::call).collect(Collectors.joining(", then "));
219-
return new Hint(Kind.CALL_SEQUENCE, String.format("Call %s before this call to move %s from %s to %s", calls,
220-
receiver, from, join(machine.describeOnly(reached, changed))));
225+
return new Hint(Kind.CALL_SEQUENCE, String.format("Call %s before this call; afterwards %s", calls,
226+
isIn(join(machine.describeOnly(reached, changed)))));
221227
}
222228

223229
private Hint unreachable(Set<State> current, RefinedFunction call, boolean pathDependent) {
@@ -286,14 +292,37 @@ private static String declaration(RefinedFunction method) {
286292
}
287293

288294
/**
289-
* A step of a sequence, with the condition on its arguments if it has one, e.g.
290-
* {@code p.setMode(int mode) with mode == 2}
295+
* A step of a sequence, with the condition on its arguments if it has one: as the call with its argument when the
296+
* condition gives the only parameter a value, e.g. {@code p.setMode(2)}, and otherwise after the call, e.g.
297+
* {@code p.set(int lo, int hi) with lo < hi}
291298
*/
292299
private String call(CallVariant step) {
293300
if (step.kind() != CallVariant.Kind.ARGUMENTS)
294301
return call(step.method());
295-
return receiver + "." + declaration(step.method()) + " with "
296-
+ step.condition().getExpression().toDisplayString();
302+
Expression condition = step.condition().getExpression();
303+
return argumentValue(step.method(), condition)
304+
.map(value -> receiver + "." + Utils.getSimpleName(step.method().getName()) + "(" + value + ")")
305+
.orElse(receiver + "." + declaration(step.method()) + " with " + condition.toDisplayString());
306+
}
307+
308+
/** The value {@code condition} gives the only parameter of {@code method}, as {@code 2} in {@code mode == 2} */
309+
private static Optional<String> argumentValue(RefinedFunction method, Expression condition) {
310+
if (method.getArguments().size() != 1 || !(condition instanceof BinaryExpression equality)
311+
|| !equality.getOperator().equals("=="))
312+
return Optional.empty();
313+
Var parameter = new Var(method.getArguments().get(0).getName());
314+
if (equality.getFirstOperand().equals(parameter))
315+
return constant(equality.getSecondOperand());
316+
if (equality.getSecondOperand().equals(parameter))
317+
return constant(equality.getFirstOperand());
318+
return Optional.empty();
319+
}
320+
321+
/** {@code e} as shown in hints, if it names no variable */
322+
private static Optional<String> constant(Expression e) {
323+
List<String> variables = new ArrayList<>();
324+
e.getVariableNames(variables);
325+
return variables.isEmpty() ? Optional.of(e.toDisplayString()) : Optional.empty();
297326
}
298327

299328
/** A method as shown in hints, e.g. {@code open(int)} */
@@ -315,6 +344,11 @@ private static String parameters(RefinedFunction function) {
315344
.collect(Collectors.joining(", ", "(", ")"));
316345
}
317346

347+
/** That the receiver is in {@code states}, e.g. {@code f is open}, or {@code states} if they name the receiver */
348+
private String isIn(String states) {
349+
return (asValues ? "" : receiver + " is ") + states;
350+
}
351+
318352
/** {@code states} named as briefly as possible, e.g. {@code open or loud} */
319353
private String names(Collection<State> states) {
320354
return join(machine.describe(states));

0 commit comments

Comments
 (0)