Skip to content

Commit 636dc4d

Browse files
Use only known argument values in delegated constructor states
- A variable passed to new is referred to only through its current instance. Its raw name made the state follow a later assignment (new PrinterStateReasons(n); n = 5;), and a lambda parameter reached Z3 with the wrong sort; with no instance the value is unknown and the walk stops. - Skip a spec whose to-state mentions old(this) (it leaves another state set unchanged): a new object has no old state, and the delegated call failed with "Ghost 'old' could not be found". - Literals: booleans and ints only; extract the shared CtLiteral case. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8ba0b70 commit 636dc4d

6 files changed

Lines changed: 101 additions & 10 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package testSuite.classes.delegated_argument_reassigned_error;
2+
3+
import liquidjava.specification.ExternalRefinementsFor;
4+
import liquidjava.specification.Ghost;
5+
import liquidjava.specification.StateRefinement;
6+
7+
@ExternalRefinementsFor("java.util.HashMap")
8+
@Ghost("int capacity")
9+
public interface HashMapRefinements<K, V> {
10+
@StateRefinement(to = "capacity(this) == initialCapacity")
11+
void HashMap(int initialCapacity);
12+
13+
@StateRefinement(from = "capacity(this) == 5")
14+
V get(Object key);
15+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package testSuite.classes.delegated_argument_reassigned_error;
2+
3+
import java.util.function.IntFunction;
4+
import javax.print.attribute.standard.PrinterStateReasons;
5+
6+
// PrinterStateReasons(int) runs HashMap(int): the capacity is the argument's value at the call, not a later one
7+
public class Test {
8+
static void assignedBefore(int n) {
9+
n = 5;
10+
PrinterStateReasons r = new PrinterStateReasons(n);
11+
r.get("k");
12+
}
13+
14+
static void assignedAfter(int n) {
15+
PrinterStateReasons r = new PrinterStateReasons(n);
16+
n = 5;
17+
r.get("k"); // Expect: State Refinement Error
18+
}
19+
20+
static void assignedBeforeAndAfter(int n) {
21+
n = 6;
22+
PrinterStateReasons r = new PrinterStateReasons(n);
23+
n = 5;
24+
r.get("k"); // Expect: State Refinement Error
25+
}
26+
27+
static IntFunction<PrinterStateReasons> lambdaParameter() {
28+
return k -> {
29+
PrinterStateReasons r = new PrinterStateReasons(k);
30+
r.get("k"); // Expect: State Refinement Error
31+
return r;
32+
};
33+
}
34+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package testSuite.classes.delegated_spec_keeping_other_sets_correct;
2+
3+
import liquidjava.specification.ExternalRefinementsFor;
4+
import liquidjava.specification.StateRefinement;
5+
import liquidjava.specification.StateSet;
6+
7+
@ExternalRefinementsFor("java.util.HashMap")
8+
@StateSet({ "a1", "a2" })
9+
@StateSet({ "b1", "b2" })
10+
public interface HashMapRefinements<K, V> {
11+
@StateRefinement(to = "a2(this)")
12+
void HashMap(int initialCapacity);
13+
14+
@StateRefinement(from = "b1(this)")
15+
V get(Object key);
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package testSuite.classes.delegated_spec_keeping_other_sets_correct;
2+
3+
import javax.print.attribute.standard.PrinterStateReasons;
4+
5+
// HashMap(int)'s spec leaves the b set unchanged, which refers to the old object: a new object has none, so the
6+
// delegated call gets the default state (b1) instead of that spec
7+
public class Test {
8+
static void get() {
9+
PrinterStateReasons r = new PrinterStateReasons(5);
10+
r.get("k");
11+
}
12+
}

‎liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/object_checkers/AuxStateHandler.java‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ private static Optional<List<Predicate>> specifiedStates(TypeChecker tc, CtExecu
147147
List<Predicate> states = spec.get().function().getToStates();
148148
if (states.stream().anyMatch(to -> to.getVariableNames().stream().anyMatch(unknown::contains)))
149149
return Optional.empty();
150+
// a spec that leaves some state sets unchanged refers to old(this), which a new object does not have
151+
if (states.stream().anyMatch(to -> to.invokes(Keys.OLD)))
152+
return Optional.empty();
150153
return Optional.of(states.stream()
151154
.map(to -> values.entrySet().stream().reduce(to,
152155
(p, v) -> p.substituteVariable(v.getKey(), v.getValue()), (p, q) -> p))
@@ -167,27 +170,31 @@ private static Optional<Predicate> argumentValue(CtExpression<?> arg) {
167170
if (arg instanceof CtVariableRead<?> vr
168171
&& vr.getVariable() instanceof spoon.reflect.reference.CtParameterReference<?>)
169172
return Optional.of(Predicate.createVar(vr.getVariable().getSimpleName()));
170-
return arg instanceof CtLiteral<?> lit ? literal(lit.getValue()) : Optional.empty();
173+
return literalValue(arg);
171174
}
172175

173-
/** How a state can write {@code arg} at a call: by the variable's current instance (as for calls) or a literal. */
176+
/**
177+
* How a state can write {@code arg} at a call: by the variable's current instance, which keeps the value it has now
178+
* even if the variable is assigned later, or a literal. A variable without an instance (e.g. a parameter not
179+
* assigned yet) has no known value here.
180+
*/
174181
private static Optional<Predicate> callArgumentValue(TypeChecker tc, CtExpression<?> arg) {
175-
if (arg instanceof CtVariableRead<?> vr && !(arg instanceof CtFieldRead<?>)) {
176-
String name = vr.getVariable().getSimpleName();
177-
return Optional.of(Predicate
178-
.createVar(tc.getContext().getLastVariableInstance(name).map(Refined::getName).orElse(name)));
179-
}
182+
if (arg instanceof CtVariableRead<?> vr && !(arg instanceof CtFieldRead<?>))
183+
return tc.getContext().getLastVariableInstance(vr.getVariable().getSimpleName())
184+
.map(vi -> Predicate.createVar(vi.getName()));
185+
return literalValue(arg);
186+
}
187+
188+
/** {@code arg} as a literal of the refinement language, if it is a boolean or int literal. */
189+
private static Optional<Predicate> literalValue(CtExpression<?> arg) {
180190
return arg instanceof CtLiteral<?> lit ? literal(lit.getValue()) : Optional.empty();
181191
}
182192

183-
/** {@code value} as a literal of the refinement language, for booleans and whole numbers. */
184193
private static Optional<Predicate> literal(Object value) {
185194
if (value instanceof Boolean)
186195
return Optional.of(Predicate.createLit(value.toString(), Types.BOOLEAN));
187196
if (value instanceof Integer || value instanceof Short || value instanceof Byte)
188197
return Optional.of(Predicate.createLit(value.toString(), Types.INT));
189-
if (value instanceof Long)
190-
return Optional.of(Predicate.createLit(value.toString(), Types.LONG));
191198
return Optional.empty();
192199
}
193200

‎liquidjava-verifier/src/main/java/liquidjava/rj_language/Predicate.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ public Predicate substituteVariable(String from, Predicate to) {
183183
return new Predicate(exp.clone().substitute(new Var(from), to.getExpression().clone()));
184184
}
185185

186+
/** Whether the function {@code name} is called anywhere in this predicate. */
187+
public boolean invokes(String name) {
188+
List<String> found = new ArrayList<>();
189+
exp.getStateInvocations(found, List.of(name));
190+
return !found.isEmpty();
191+
}
192+
186193
public List<String> getVariableNames() {
187194
List<String> l = new ArrayList<>();
188195
exp.getVariableNames(l);

0 commit comments

Comments
 (0)