Skip to content

Commit 4b2de48

Browse files
Read a conditional's condition before its branches, and snapshot written fields
A branch that writes a field the condition reads (f ? (f = false) : true) made the condition name the written value. The condition's value is now taken before the branches are checked, and a guard snapshots the fields the method writes, as it already did for parameters. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9d66230 commit 4b2de48

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ static void oneElement(@Refinement("length(a) == 1") int[] a) {}
1919
static void pick(boolean b) {
2020
oneElement(b ? new int[1] : new int[2]); // Expect: Refinement Error
2121
}
22+
23+
boolean flag;
24+
25+
void clear() {
26+
@Refinement("_ == true")
27+
boolean was = flag ? (flag = false) : true; // Expect: Refinement Error
28+
}
2229
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ public void visitCtIf(CtIf ifElement) {
562562
Predicate lastExpRefs = substituteAllVariablesForLastInstance(expRefs);
563563
if (canCompleteNormally(ifElement.getThenStatement()) || ifElement.getElseStatement() == null
564564
|| canCompleteNormally(ifElement.getElseStatement()))
565-
lastExpRefs = snapshotMutableParameters(lastExpRefs, exp);
565+
lastExpRefs = snapshotMutableVariables(lastExpRefs, exp);
566566
expRefs = Predicate.createConjunction(expRefs, lastExpRefs);
567567

568568
// TODO Change in future
@@ -668,7 +668,7 @@ private boolean visitSwitch(CtAbstractSwitch<?> switchElement) {
668668
// the selector is evaluated once, so the cases compare its value then, not what its variables hold later
669669
String selectorName = GeneratedNames.of(GeneratedNames.SWITCH_SELECTOR, context.getCounter());
670670
Predicate selectorRefs = getRefinement(selector).substituteVariable(Keys.WILDCARD, selectorName);
671-
selectorRefs = snapshotMutableParameters(substituteAllVariablesForLastInstance(selectorRefs), selector);
671+
selectorRefs = snapshotMutableVariables(substituteAllVariablesForLastInstance(selectorRefs), selector);
672672
context.addInstanceToContext(selectorName, selector.getType(), selectorRefs, selector);
673673

674674
// the cases with no statements share the statements of the next case, the default one is tested last
@@ -822,21 +822,26 @@ private static CtStatement endingJump(CtStatement statement) {
822822
return statement instanceof CtCFlowBreak ? statement : null;
823823
}
824824

825-
private Predicate snapshotMutableParameters(Predicate predicate, CtExpression<?> condition) {
826-
// An unchanged parameter can keep its name; a later write must not change this guard's meaning.
825+
private Predicate snapshotMutableVariables(Predicate predicate, CtExpression<?> condition) {
826+
// An unchanged parameter or field can keep its name; a later write must not change this guard's meaning.
827827
CtExecutable<?> executable = condition.getParent(CtExecutable.class);
828828
Set<CtVariable<?>> written = new LinkedHashSet<>();
829829
if (executable != null)
830830
for (CtVariableWrite<?> write : executable
831831
.getElements(new TypeFilter<CtVariableWrite<?>>(CtVariableWrite.class)))
832832
if (write.getVariable().getDeclaration() != null)
833833
written.add(write.getVariable().getDeclaration());
834-
Set<String> parameters = new LinkedHashSet<>();
835-
for (CtVariableRead<?> read : condition.getElements(new TypeFilter<CtVariableRead<?>>(CtVariableRead.class)))
836-
if (read.getVariable().getDeclaration()instanceof CtParameter<?> parameter && written.contains(parameter))
837-
parameters.add(read.getVariable().getSimpleName());
834+
Set<String> mutable = new LinkedHashSet<>();
835+
for (CtVariableRead<?> read : condition.getElements(new TypeFilter<CtVariableRead<?>>(CtVariableRead.class))) {
836+
if (!written.contains(read.getVariable().getDeclaration()))
837+
continue;
838+
if (read instanceof CtFieldRead<?> fieldRead)
839+
mutable.add(Utils.qualifyFieldName(fieldRead.getVariable()));
840+
else if (read.getVariable().getDeclaration() instanceof CtParameter<?>)
841+
mutable.add(read.getVariable().getSimpleName());
842+
}
838843
for (String name : predicate.getVariableNames()) {
839-
if (!parameters.contains(name) || !(context.getVariableByName(name)instanceof Variable variable)
844+
if (!mutable.contains(name) || !(context.getVariableByName(name)instanceof Variable variable)
840845
|| variable.getLastInstance().isPresent())
841846
continue;
842847
String snapshot = String.format(Formats.INSTANCE, name, context.getCounter());
@@ -1354,17 +1359,18 @@ public <T> void visitCtConditional(CtConditional<T> conditional) {
13541359
// each branch is checked knowing which way the condition went, as the branches of an if
13551360
CtExpression<Boolean> condition = conditional.getCondition();
13561361
scan(condition);
1362+
Predicate value = conditionValue(condition);
13571363
scanAssuming(conditional.getThenExpression(), condition, true);
13581364
scanAssuming(conditional.getElseExpression(), condition, false);
1359-
Predicate c = Predicate.createITE(conditionValue(condition), getRefinement(conditional.getThenExpression()),
1365+
Predicate c = Predicate.createITE(value, getRefinement(conditional.getThenExpression()),
13601366
getRefinement(conditional.getElseExpression()));
13611367
conditional.putMetadata(Keys.REFINEMENT, c);
13621368
}
13631369

13641370
/**
13651371
* The value of the condition of a conditional, as a fresh boolean: the wildcard of the condition's refinement is
13661372
* the condition, while the wildcard of the conditional's refinement is its result. The fresh boolean keeps the
1367-
* values the condition was evaluated on, as later writes do not change which branch was taken.
1373+
* values the condition was evaluated on, as later writes (even in a branch) do not change which branch was taken.
13681374
*/
13691375
private Predicate conditionValue(CtExpression<Boolean> condition) {
13701376
String name = GeneratedNames.of(GeneratedNames.TERNARY_CONDITION, context.getCounter());
@@ -1378,7 +1384,7 @@ else if (refs.getVariableNames().contains(Keys.WILDCARD) || condition instanceof
13781384
value = refs.substituteVariable(Keys.WILDCARD, name);
13791385
else
13801386
value = Predicate.createEquals(Predicate.createVar(name), refs);
1381-
value = snapshotMutableParameters(substituteAllVariablesForLastInstance(value), condition);
1387+
value = snapshotMutableVariables(substituteAllVariablesForLastInstance(value), condition);
13821388
context.addInstanceToContext(name, factory.Type().BOOLEAN_PRIMITIVE, value, condition);
13831389
return Predicate.createVar(name);
13841390
}

0 commit comments

Comments
 (0)