Skip to content

Commit 02cb508

Browse files
Check each case of a switch from the state before it
A switch is checked as the chain of ifs that tests its labels: each case knows the selector equals one of its labels (the default case, none of the others), starts from the state before the switch, falls through into the next case when it does not break, and the states of the cases that reach the end are joined after it. Switch expressions are checked the same way. Pattern and guarded cases keep the previous checking. A break or yield nested inside a case's statements is not followed: what those cases change is forgotten on that path. Fixes #494 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ae278d2 commit 02cb508

5 files changed

Lines changed: 275 additions & 11 deletions

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,38 @@ public static void main(String[] args) {
4242
st.setMode(Mode.Video); // noMode -> videoMode
4343
st.takeVideo();
4444
}
45+
46+
// Each case starts from the state before the switch and knows the value of the selector
47+
public static void capture(Mode mode) {
48+
CorrectEnumUsage st = new CorrectEnumUsage();
49+
switch (mode) {
50+
case Photo:
51+
st.setMode(Mode.Photo);
52+
st.takePhoto();
53+
break;
54+
case Video:
55+
st.setMode(mode);
56+
st.takeVideo();
57+
break;
58+
default:
59+
break;
60+
}
61+
}
62+
63+
public static boolean captureExpression(Mode mode) {
64+
CorrectEnumUsage st = new CorrectEnumUsage();
65+
return switch (mode) {
66+
case Photo -> {
67+
st.setMode(Mode.Photo);
68+
st.takePhoto();
69+
yield true;
70+
}
71+
case Video -> {
72+
st.setMode(Mode.Video);
73+
st.takeVideo();
74+
yield true;
75+
}
76+
default -> false;
77+
};
78+
}
4579
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,33 @@ public static void main(String[] args) {
3030
st.setMode(Mode.Video);
3131
st.takePhoto(); // Expect: State Refinement Error
3232
}
33+
34+
// Video is not in photoMode: only Photo falls through to it
35+
public static void capture(Mode mode) {
36+
ErrorEnumUsage st = new ErrorEnumUsage();
37+
switch (mode) {
38+
case Photo:
39+
st.setMode(Mode.Photo);
40+
case Video:
41+
st.takePhoto(); // Expect: State Refinement Error
42+
break;
43+
default:
44+
break;
45+
}
46+
}
47+
48+
// The break inside the if leaves the switch before setMode
49+
public static void captureUnlessSkipped(Mode mode, boolean skip) {
50+
ErrorEnumUsage st = new ErrorEnumUsage();
51+
switch (mode) {
52+
case Video:
53+
if (skip)
54+
break;
55+
st.setMode(Mode.Photo);
56+
break;
57+
default:
58+
st.setMode(Mode.Photo);
59+
}
60+
st.takePhoto(); // Expect: State Refinement Error
61+
}
3362
}

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

Lines changed: 205 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.ArrayList;
66
import java.util.Arrays;
77
import java.util.Deque;
8+
import java.util.HashSet;
89
import java.util.LinkedHashSet;
910
import java.util.List;
1011
import java.util.Optional;
@@ -29,13 +30,17 @@
2930
import liquidjava.utils.constants.Types;
3031

3132
import org.apache.commons.lang3.NotImplementedException;
33+
import spoon.reflect.code.CaseKind;
3234
import spoon.reflect.code.CtArrayRead;
3335
import spoon.reflect.code.CtAbstractInvocation;
36+
import spoon.reflect.code.CtAbstractSwitch;
3437
import spoon.reflect.code.CtArrayWrite;
3538
import spoon.reflect.code.CtAssignment;
3639
import spoon.reflect.code.CtBinaryOperator;
3740
import spoon.reflect.code.CtBlock;
3841
import spoon.reflect.code.CtBreak;
42+
import spoon.reflect.code.CtCFlowBreak;
43+
import spoon.reflect.code.CtCase;
3944
import spoon.reflect.code.CtCatchVariable;
4045
import spoon.reflect.code.CtConditional;
4146
import spoon.reflect.code.CtContinue;
@@ -57,6 +62,7 @@
5762
import spoon.reflect.code.CtNewArray;
5863
import spoon.reflect.code.CtNewClass;
5964
import spoon.reflect.code.CtOperatorAssignment;
65+
import spoon.reflect.code.CtPattern;
6066
import spoon.reflect.code.CtReturn;
6167
import spoon.reflect.code.CtStatement;
6268
import spoon.reflect.code.CtSuperAccess;
@@ -71,6 +77,7 @@
7177
import spoon.reflect.code.CtVariableRead;
7278
import spoon.reflect.code.CtVariableWrite;
7379
import spoon.reflect.code.CtWhile;
80+
import spoon.reflect.code.CtYieldStatement;
7481
import spoon.reflect.declaration.*;
7582
import spoon.reflect.factory.Factory;
7683
import spoon.reflect.reference.CtFieldReference;
@@ -575,6 +582,189 @@ public void visitCtIf(CtIf ifElement) {
575582
context.variablesFinishIfCombination();
576583
}
577584

585+
@Override
586+
public <S> void visitCtSwitch(CtSwitch<S> switchStatement) {
587+
if (!visitSwitch(switchStatement))
588+
super.visitCtSwitch(switchStatement);
589+
}
590+
591+
@Override
592+
public <T, S> void visitCtSwitchExpression(CtSwitchExpression<T, S> switchExpression) {
593+
if (!visitSwitch(switchExpression))
594+
super.visitCtSwitchExpression(switchExpression);
595+
}
596+
597+
/**
598+
* Checks a switch as the chain of ifs that tests its labels in turn (see visitCtIf): each case is checked from the
599+
* state before the switch, knowing that the selector equals one of its labels, and the default case knowing that it
600+
* equals none of the others. A case that falls through goes on into the next one. The states of the cases that
601+
* reach the end of the switch are joined after it. Pattern and guarded cases are not modelled.
602+
*
603+
* @return whether the switch was checked, false when it has pattern or guarded cases
604+
*/
605+
private boolean visitSwitch(CtAbstractSwitch<?> switchElement) {
606+
List<CtCase<?>> cases = new ArrayList<>(switchElement.getCases());
607+
if (cases.stream().anyMatch(c -> c.getGuard() != null
608+
|| c.getCaseExpressions().stream().anyMatch(label -> label instanceof CtPattern)))
609+
return false;
610+
CtExpression<?> selector = switchElement.getSelector();
611+
scan(selector);
612+
// the selector is evaluated once, so the cases compare its value then, not what its variables hold later
613+
String selectorName = GeneratedNames.of(GeneratedNames.SWITCH_SELECTOR, context.getCounter());
614+
Predicate selectorRefs = getRefinement(selector).substituteVariable(Keys.WILDCARD, selectorName);
615+
selectorRefs = snapshotMutableParameters(substituteAllVariablesForLastInstance(selectorRefs), selector);
616+
context.addInstanceToContext(selectorName, selector.getType(), selectorRefs, selector);
617+
618+
// the cases with no statements share the statements of the next case, the default one is tested last
619+
List<CaseEntry> entries = new ArrayList<>();
620+
CaseEntry defaultEntry = null;
621+
List<Predicate> labels = new ArrayList<>();
622+
boolean isDefault = false;
623+
int start = 0;
624+
for (int i = 0; i < cases.size(); i++) {
625+
CtCase<?> c = cases.get(i);
626+
for (CtExpression<?> label : c.getCaseExpressions()) {
627+
scan(label);
628+
labels.add(getRefinement(label));
629+
}
630+
isDefault |= c.getCaseExpressions().isEmpty() || c.getIncludesDefault();
631+
if (c.getStatements().isEmpty() && i < cases.size() - 1)
632+
continue;
633+
CaseEntry entry = new CaseEntry(start, labels);
634+
if (isDefault)
635+
defaultEntry = entry;
636+
else
637+
entries.add(entry);
638+
labels = new ArrayList<>();
639+
isDefault = false;
640+
start = i + 1;
641+
}
642+
if (defaultEntry != null)
643+
entries.add(defaultEntry);
644+
visitCases(switchElement, cases, entries, 0, selectorName, defaultEntry != null, new HashSet<>());
645+
return true;
646+
}
647+
648+
/** A case to which the selector jumps when it equals one of {@code labels}, by their refinements */
649+
private record CaseEntry(int start, List<Predicate> labels) {
650+
/** That the selector equals one of the labels, or null when a label has no known value */
651+
Predicate matches(String selectorName) {
652+
Predicate matches = null;
653+
for (Predicate label : labels) {
654+
if (!label.getVariableNames().contains(Keys.WILDCARD))
655+
return null;
656+
Predicate equals = label.substituteVariable(Keys.WILDCARD, selectorName);
657+
matches = matches == null ? equals : Predicate.createDisjunction(matches, equals);
658+
}
659+
return matches;
660+
}
661+
}
662+
663+
/**
664+
* Checks the entries of a switch from {@code index} on, as an if that tests the one at {@code index} and checks the
665+
* others in its else, the last one being the default case if there is one
666+
*
667+
* @return whether the end of the switch can be reached from here
668+
*/
669+
private boolean visitCases(CtAbstractSwitch<?> switchElement, List<CtCase<?>> cases, List<CaseEntry> entries,
670+
int index, String selectorName, boolean hasDefault, Set<CtCase<?>> checked) {
671+
if (index == entries.size())
672+
return true; // no label matches and there is no default case
673+
CaseEntry entry = entries.get(index);
674+
if (hasDefault && index == entries.size() - 1)
675+
return visitCasesFrom(switchElement, cases, entry.start(), checked);
676+
677+
String pathVarName = GeneratedNames.of(GeneratedNames.CASE_CONDITION, context.getCounter());
678+
// a label with no known value gives no information about which way the test went
679+
Predicate matches = entry.matches(selectorName);
680+
Predicate branchGuard = matches != null ? matches : Predicate.createVar(pathVarName);
681+
RefinedVariable pathVar = context.addInstanceToContext(pathVarName, factory.Type().BOOLEAN_PRIMITIVE,
682+
branchGuard, cases.get(entry.start()));
683+
keepFreshValues(branchGuard);
684+
vcChecker.addPathVariable(pathVar);
685+
686+
context.variablesNewIfCombination();
687+
context.variablesSetBeforeIf();
688+
context.enterContext();
689+
690+
context.enterContext();
691+
boolean thenReaches = visitCasesFrom(switchElement, cases, entry.start(), checked);
692+
if (thenReaches)
693+
context.variablesSetThenIf();
694+
context.exitContext();
695+
696+
context.newRefinementToVariableInContext(pathVarName, branchGuard.negate());
697+
context.enterContext();
698+
boolean elseReaches = visitCases(switchElement, cases, entries, index + 1, selectorName, hasDefault, checked);
699+
if (elseReaches)
700+
context.variablesSetElseIf();
701+
context.exitContext();
702+
703+
if (thenReaches == elseReaches) {
704+
context.newRefinementToVariableInContext(pathVarName, matches != null ? matches : new Predicate());
705+
vcChecker.removePathVariable(pathVar);
706+
} else {
707+
// keep the guard of the only branch that reaches the end of the switch
708+
context.newRefinementToVariableInContext(pathVarName, thenReaches ? branchGuard : branchGuard.negate());
709+
}
710+
context.exitContext();
711+
context.variablesCombineFromIf(branchGuard);
712+
context.variablesFinishIfCombination();
713+
return thenReaches || elseReaches;
714+
}
715+
716+
/**
717+
* Checks the statements of the case at {@code start}, and of the next cases while they fall through. The path
718+
* conditions they add are dropped after them.
719+
*
720+
* @return whether they reach the end of the switch
721+
*/
722+
private boolean visitCasesFrom(CtAbstractSwitch<?> switchElement, List<CtCase<?>> cases, int start,
723+
Set<CtCase<?>> checked) {
724+
List<RefinedVariable> pathVariables = vcChecker.getPathVariables();
725+
List<CtCFlowBreak> exits = new ArrayList<>();
726+
int end = start;
727+
boolean completes;
728+
while (true) {
729+
CtCase<?> c = cases.get(end);
730+
if (!checked.add(c))
731+
removeCheckMetadata(c, List.of()); // checked again after falling through from another case
732+
for (CtStatement statement : c.getStatements())
733+
scan(statement);
734+
exits.addAll(c.getElements(new TypeFilter<>(CtCFlowBreak.class)).stream()
735+
.filter(jump -> leaves(jump, switchElement)).toList());
736+
List<CtStatement> statements = c.getStatements();
737+
completes = statements.isEmpty() || canCompleteNormally(statements.get(statements.size() - 1));
738+
if (!completes || c.getCaseKind() == CaseKind.ARROW || end == cases.size() - 1)
739+
break;
740+
end++;
741+
}
742+
List<CtStatement> last = cases.get(end).getStatements();
743+
CtStatement lastExit = last.isEmpty() ? null : endingJump(last.get(last.size() - 1));
744+
// the states where a break or yield inside a statement leaves the switch are not followed: forget what these
745+
// cases change
746+
if (exits.stream().anyMatch(jump -> jump != lastExit))
747+
for (CtCase<?> c : cases.subList(start, end + 1))
748+
for (String name : changedIn(c, switchElement))
749+
forgetChanges(name, new Predicate(), c);
750+
vcChecker.restorePathVariables(pathVariables);
751+
return completes || !exits.isEmpty();
752+
}
753+
754+
/** Whether {@code jump} is a break or yield that leaves {@code switchElement} */
755+
private static boolean leaves(CtCFlowBreak jump, CtAbstractSwitch<?> switchElement) {
756+
if (jump instanceof CtBreak breakStatement)
757+
return jumpTarget(breakStatement) == switchElement;
758+
return jump instanceof CtYieldStatement && jump.getParent(CtSwitchExpression.class) == switchElement;
759+
}
760+
761+
/** The jump that a statement ends with, as the last statement of its blocks */
762+
private static CtStatement endingJump(CtStatement statement) {
763+
if (statement instanceof CtBlock<?> block)
764+
return block.getStatements().isEmpty() ? null : endingJump(block.getLastStatement());
765+
return statement instanceof CtCFlowBreak ? statement : null;
766+
}
767+
578768
private Predicate snapshotMutableParameters(Predicate predicate, CtExpression<?> condition) {
579769
// An unchanged parameter can keep its name; a later write must not change this guard's meaning.
580770
CtExecutable<?> executable = condition.getParent(CtExecutable.class);
@@ -651,7 +841,7 @@ boolean canCompleteNormally(CtStatement statement) {
651841
if (statement == null || isLeftByLabelledBreak(statement))
652842
return true;
653843
if (statement instanceof CtReturn<?> || statement instanceof CtThrow || statement instanceof CtBreak
654-
|| statement instanceof CtContinue)
844+
|| statement instanceof CtContinue || statement instanceof CtYieldStatement)
655845
return false;
656846
if (statement instanceof CtBlock<?> block) {
657847
List<CtStatement> statements = block.getStatements();
@@ -804,7 +994,11 @@ private static void removeCheckMetadata(CtLoop loop) {
804994
checkedBefore.addAll(forLoop.getForInit());
805995
if (loop instanceof CtForEach forEach)
806996
checkedBefore.add(forEach.getExpression());
807-
for (CtElement element : loop.getElements((CtElement e) -> true)) {
997+
removeCheckMetadata(loop, checkedBefore);
998+
}
999+
1000+
private static void removeCheckMetadata(CtElement checked, List<CtElement> checkedBefore) {
1001+
for (CtElement element : checked.getElements((CtElement e) -> true)) {
8081002
if (element instanceof CtTypeMember
8091003
|| (element instanceof CtParameter<?> && element.getParent() instanceof CtTypeMember)
8101004
|| checkedBefore.stream().anyMatch(before -> element == before || element.hasParent(before)))
@@ -906,9 +1100,14 @@ void forgetChanges(String name, Predicate known, CtElement element) {
9061100
* if it calls any method or constructor (which may write them)
9071101
*/
9081102
private Set<String> changedIn(CtLoop loop) {
1103+
return changedIn(loop, loop.getBody());
1104+
}
1105+
1106+
/** What {@code element} may change, but the variables declared in {@code scope}, which do not outlive it */
1107+
private Set<String> changedIn(CtElement element, CtElement scope) {
9091108
List<CtVariableAccess<?>> changed = new ArrayList<>(
910-
loop.getElements(new TypeFilter<CtVariableWrite<?>>(CtVariableWrite.class)));
911-
List<CtAbstractInvocation<?>> calls = loop
1109+
element.getElements(new TypeFilter<CtVariableWrite<?>>(CtVariableWrite.class)));
1110+
List<CtAbstractInvocation<?>> calls = element
9121111
.getElements(new TypeFilter<CtAbstractInvocation<?>>(CtAbstractInvocation.class));
9131112
Set<String> names = new LinkedHashSet<>();
9141113
for (CtAbstractInvocation<?> call : calls) {
@@ -921,8 +1120,8 @@ else if (inv.getTarget() != null && AuxStateHandler.isCurrentReceiver(inv.getTar
9211120
}
9221121
for (CtVariableAccess<?> access : changed) {
9231122
CtVariable<?> declaration = access.getVariable().getDeclaration();
924-
if (declaration != null && declaration.hasParent(loop.getBody()))
925-
continue; // declared in the body: a new variable on each iteration
1123+
if (declaration != null && declaration.hasParent(scope))
1124+
continue; // e.g. declared in a loop body: a new variable on each iteration
9261125
String name = access.getVariable().getSimpleName();
9271126
names.add(access instanceof CtFieldAccess<?> ? String.format(Formats.THIS, name) : name);
9281127
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public final class GeneratedNames {
1919
public static final String IF_CONDITION = "if_condition";
2020
public static final String LOOP_CONDITION = "loop_condition";
2121
public static final String TERNARY_CONDITION = "ternary_condition";
22+
public static final String SWITCH_SELECTOR = "switch_selector";
23+
public static final String CASE_CONDITION = "case_condition";
2224
public static final String TRY_BRANCH = "try_branch";
2325
public static final String CATCH_BRANCH = "catch_branch";
2426
public static final String ARRAY_LENGTH = "array_length";

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,11 @@ void checksOfTheStudysUndoManagerAreSuggestedOneAfterTheOther() {
325325
"The state of undoManager is unknown here, and this call needs it to be redoable"),
326326
new Hint(Kind.GUARD,
327327
"Guard this call with if (undoManager.canRedo()): when it returns true, undoManager is redoable"));
328-
assertHints(errors.get(1), new Hint(Kind.GUARD,
329-
"Guard this call with if (undoManager.canUndo()): when it returns true, undoManager is undoable"),
330-
new Hint(Kind.AVAILABLE_CALLS,
331-
"In state undoable and notRedoable or notUndoable and notRedoable, undoManager can call: "
332-
+ "canUndo(), canRedo() (and possibly others)"));
328+
assertHints(errors.get(1),
329+
new Hint(Kind.NEEDED_STATE,
330+
"The state of undoManager is unknown here, and this call needs it to be undoable"),
331+
new Hint(Kind.GUARD,
332+
"Guard this call with if (undoManager.canUndo()): when it returns true, undoManager is undoable"));
333333
}
334334

335335
@Test

0 commit comments

Comments
 (0)