Skip to content

Commit 797608f

Browse files
Show a counterexample for state errors, without state values
A state refinement error now shows the values a model of what is known where the call fails gives the variables of the found the message shows. Values found already states, generated names and ghost or state values are left out, so A1_09 shows previousMode == BORDERLESS and an error about states alone shows none. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 538ac47 commit 797608f

5 files changed

Lines changed: 57 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private Counterexample filterCounterexample(Counterexample counterexample) {
154154
}
155155

156156
// Whether a fact of found is term == value, which the counterexample would only repeat
157-
private static boolean isKnown(Pair<String, String> assignment, List<Expression> facts) {
157+
static boolean isKnown(Pair<String, String> assignment, List<Expression> facts) {
158158
return facts.stream()
159159
.anyMatch(fact -> fact instanceof BinaryExpression binary && "==".equals(binary.getOperator())
160160
&& (states(binary.getFirstOperand(), binary.getSecondOperand(), assignment)

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package liquidjava.diagnostics.errors;
22

3+
import static liquidjava.rj_language.opt.VCSimplificationUtils.factsOf;
34
import static liquidjava.rj_language.opt.VCSimplificationUtils.isTrue;
5+
import static liquidjava.rj_language.opt.VCSimplificationUtils.linkedToExpected;
46

7+
import java.util.ArrayList;
58
import java.util.List;
69
import java.util.function.UnaryOperator;
10+
import java.util.stream.Collectors;
711

812
import liquidjava.diagnostics.TranslationTable;
913
import liquidjava.diagnostics.categories.ErrorCategory;
@@ -13,6 +17,8 @@
1317
import liquidjava.rj_language.ast.Expression;
1418
import liquidjava.rj_language.ast.FunctionInvocation;
1519
import liquidjava.rj_language.opt.VCSimplificationResult;
20+
import liquidjava.smt.Counterexample;
21+
import liquidjava.utils.GeneratedNames;
1622
import spoon.reflect.cu.SourcePosition;
1723

1824
/**
@@ -54,6 +60,27 @@ public StateRefinementError(SourcePosition position, SourcePosition declarationP
5460
"No state information is known for this object. Make sure its state is initialized and available at this point."));
5561
}
5662

63+
/**
64+
* Shows the values the counterexample gives the variables of the found the message shows, but those found already
65+
* states. A ghost or state value is never shown: a state is an unconstrained number, and the message already says
66+
* which state each object is in
67+
*/
68+
public void showCounterexample(Counterexample counterexample) {
69+
Expression foundExpression = found.getImplication().toPredicate().getExpression();
70+
Expression shown = linkedToExpected(foundExpression, expected.getExpression());
71+
if (counterexample == null || !appliesGhost(shown))
72+
return;
73+
List<String> variables = new ArrayList<>();
74+
shown.getVariableNames(variables);
75+
List<Expression> facts = factsOf(foundExpression);
76+
String values = counterexample.assignments().stream()
77+
.filter(a -> variables.contains(a.first()) && !GeneratedNames.isGenerated(a.first()))
78+
.filter(a -> !RefinementError.isKnown(a, facts) && !a.second().contains("!val!"))
79+
.map(a -> getDisplayNames().get(a.first()) + " == " + a.second()).collect(Collectors.joining(" && "));
80+
if (!values.isEmpty())
81+
setCounterexampleStr("Counterexample: " + values);
82+
}
83+
5784
// Only the facts of found about the objects of expected, and only when one of them tells a state: an alias or a
5885
// guard on another value says nothing about the state expected needs
5986
private static boolean appliesGhost(Expression expression) {

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import liquidjava.rj_language.ast.Var;
3232
import liquidjava.rj_language.ast.formatter.FieldDisplayNames;
3333
import liquidjava.rj_language.parsing.RefinementsParser;
34+
import liquidjava.diagnostics.DebugLog;
35+
import liquidjava.smt.Counterexample;
3436
import liquidjava.smt.SMTEvaluator;
3537
import liquidjava.smt.SMTResult;
3638
import liquidjava.utils.Utils;
@@ -621,7 +623,19 @@ public void throwStateRefinementError(SourcePosition position, SourcePosition de
621623
Predicate expected, String customMessage, List<Hint> hints, ErrorCategory category,
622624
StateErrorContext context, List<String> declarationNotes) throws LJError {
623625
vcChecker.throwStateRefinementError(position, declarationPosition, found, expected, customMessage, hints,
624-
category, context, declarationNotes);
626+
category, context, declarationNotes, counterexample(found, expected, position));
627+
}
628+
629+
/** A model of what is known where {@code expected} fails, or null if the solver gives none */
630+
private Counterexample counterexample(Predicate found, Predicate expected, SourcePosition position) {
631+
try {
632+
SMTResult result = verifyStateSMT(found, expected, position, true);
633+
return result.isError() ? result.getCounterexample() : null;
634+
} catch (RuntimeException e) {
635+
// a failure to find a model must not replace the error being reported
636+
DebugLog.warn("Could not find a counterexample for the state error: " + e.getMessage());
637+
return null;
638+
}
625639
}
626640

627641
public void throwStateConflictError(SourcePosition position, Predicate expectedType) throws LJError {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,16 +457,19 @@ protected Predicate shownFound(Predicate found, Predicate expected) {
457457

458458
protected void throwStateRefinementError(SourcePosition position, SourcePosition declarationPosition,
459459
Predicate found, Predicate expected, String customMessage, List<Hint> hints, ErrorCategory category,
460-
StateErrorContext context, List<String> declarationNotes) throws StateRefinementError {
460+
StateErrorContext context, List<String> declarationNotes, Counterexample counterexample)
461+
throws StateRefinementError {
461462
TranslationTable map = new TranslationTable();
462463
VCImplication foundState = buildPremiseChain(map, expected, found);
463464
VCSimplificationResult simplified = foundState.simplify(NeededVariables.forStateError(expected));
464465
VCSimplificationResult shown = VCSimplification.simplifyOnce(simplified,
465466
new VCRelevantFacts(expected.getExpression(), this::ghostFunctionsOf));
466-
throw new StateRefinementError(position, declarationPosition, expected, shown, map, customMessage, hints,
467-
category, context,
467+
StateRefinementError error = new StateRefinementError(position, declarationPosition, expected, shown, map,
468+
customMessage, hints, category, context,
468469
e -> StateValues.toSetForm(e, this.context.getGhostStates(), this.context.getGhosts()),
469470
declarationNotes);
471+
error.showCounterexample(counterexample);
472+
throw error;
470473
}
471474

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ void indexIntoLocalArrayShowsTheIndexAndNotTheArray() {
234234
assertAssignments(error, assignment("k", "-1"));
235235
}
236236

237+
@Test
238+
void stateErrorShowsTheValueThatPicksTheFailingBranchButNoStateValue() {
239+
CommandLineLauncher.launch(TEST_SUITE + "ErrorStateHintPathDependent.java");
240+
List<LJError> errors = Diagnostics.getInstance().getErrors().stream().toList();
241+
assertEquals(1, errors.size());
242+
assertEquals("Counterexample: ready == false", errors.get(0).getCounterexampleStr());
243+
}
244+
237245
private static RefinementError verify(String test) {
238246
CommandLineLauncher.launch(TEST_SUITE + test);
239247
List<LJError> errors = Diagnostics.getInstance().getErrors().stream().toList();

0 commit comments

Comments
 (0)