|
1 | 1 | package liquidjava.diagnostics.errors; |
2 | 2 |
|
| 3 | +import static liquidjava.rj_language.opt.VCSimplificationUtils.factsOf; |
3 | 4 | import static liquidjava.rj_language.opt.VCSimplificationUtils.isTrue; |
| 5 | +import static liquidjava.rj_language.opt.VCSimplificationUtils.linkedToExpected; |
4 | 6 |
|
| 7 | +import java.util.ArrayList; |
5 | 8 | import java.util.List; |
6 | 9 | import java.util.function.UnaryOperator; |
| 10 | +import java.util.stream.Collectors; |
7 | 11 |
|
8 | 12 | import liquidjava.diagnostics.TranslationTable; |
9 | 13 | import liquidjava.diagnostics.categories.ErrorCategory; |
|
13 | 17 | import liquidjava.rj_language.ast.Expression; |
14 | 18 | import liquidjava.rj_language.ast.FunctionInvocation; |
15 | 19 | import liquidjava.rj_language.opt.VCSimplificationResult; |
| 20 | +import liquidjava.smt.Counterexample; |
| 21 | +import liquidjava.utils.GeneratedNames; |
16 | 22 | import spoon.reflect.cu.SourcePosition; |
17 | 23 |
|
18 | 24 | /** |
@@ -54,6 +60,27 @@ public StateRefinementError(SourcePosition position, SourcePosition declarationP |
54 | 60 | "No state information is known for this object. Make sure its state is initialized and available at this point.")); |
55 | 61 | } |
56 | 62 |
|
| 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 | + |
57 | 84 | // Only the facts of found about the objects of expected, and only when one of them tells a state: an alias or a |
58 | 85 | // guard on another value says nothing about the state expected needs |
59 | 86 | private static boolean appliesGhost(Expression expression) { |
|
0 commit comments