Skip to content

Commit fc7acd4

Browse files
Leave out refinements the verifier would not apply and states of same-named classes
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b841eb0 commit fc7acd4

8 files changed

Lines changed: 121 additions & 11 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package testSuite.classes.state_hints_generic_inherited_method_error;
2+
3+
import liquidjava.specification.ExternalRefinementsFor;
4+
import liquidjava.specification.StateRefinement;
5+
import liquidjava.specification.StateSet;
6+
7+
@StateSet({"empty", "nonEmpty"})
8+
@ExternalRefinementsFor("java.util.Stack")
9+
public interface StackRefinements<E> {
10+
@StateRefinement(to = "empty(this)")
11+
void Stack();
12+
13+
// Stack inherits add from Vector, and the verifier does not find this refinement for a call to it
14+
@StateRefinement(to = "nonEmpty(this)")
15+
boolean add(E e);
16+
17+
@StateRefinement(from = "nonEmpty(this)")
18+
E pop();
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package testSuite.classes.state_hints_generic_inherited_method_error;
2+
3+
import java.util.Stack;
4+
5+
public class Use {
6+
static void popEmpty() {
7+
Stack<String> s = new Stack<>();
8+
s.pop(); // Expect: State Refinement Error
9+
}
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package testSuite.classes.state_hints_same_simple_name_error;
2+
3+
import liquidjava.specification.ExternalRefinementsFor;
4+
import liquidjava.specification.StateRefinement;
5+
import liquidjava.specification.StateSet;
6+
7+
@StateSet({"empty", "nonEmpty"})
8+
@ExternalRefinementsFor("java.util.ArrayList")
9+
public interface ArrayListRefinements<E> {
10+
@StateRefinement(to = "empty(this)")
11+
void ArrayList();
12+
13+
@StateRefinement(to = "nonEmpty(this)")
14+
boolean add(E e);
15+
16+
@StateRefinement(from = "nonEmpty(this)")
17+
E remove(int index);
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package testSuite.classes.state_hints_same_simple_name_error;
2+
3+
import liquidjava.specification.StateRefinement;
4+
import liquidjava.specification.StateSet;
5+
6+
// shares its simple name with java.util.Collection, a supertype of ArrayList, but its states are not ArrayList's
7+
@StateSet({"fresh", "used"})
8+
public class Collection {
9+
@StateRefinement(to = "fresh(this)")
10+
public Collection() {}
11+
12+
@StateRefinement(from = "fresh(this)", to = "used(this)")
13+
public void use() {}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package testSuite.classes.state_hints_same_simple_name_error;
2+
3+
import java.util.ArrayList;
4+
5+
public class Use {
6+
static void removeFromEmpty() {
7+
ArrayList<String> l = new ArrayList<>();
8+
l.remove(0); // Expect: State Refinement Error
9+
}
10+
}

‎liquidjava-verifier/src/main/java/liquidjava/diagnostics/hints/StateHints.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ private List<Hint> reachingCalls(RefinedFunction call, Set<State> goal) {
8484
Map<RefinedFunction, List<State>> sources = new LinkedHashMap<>();
8585
for (Transition t : machine.transitionsEnabling(call))
8686
sources.computeIfAbsent(t.method(), m -> new ArrayList<>()).add(t.from());
87-
Stream<String> constructors = machine.constructorsEnabling(call).stream().map(StateHints::constructor);
87+
Stream<String> constructors = machine.constructorsEnabling(call).stream().map(StateHints::constructor)
88+
.distinct();
8889
Stream<String> calls = sources.entrySet().stream()
8990
.map(e -> call(e.getKey()) + " (from " + names(e.getValue()) + ")");
9091
List<String> ways = Stream.concat(constructors, calls).toList();
@@ -123,7 +124,8 @@ private Hint unreachable(Set<State> current, RefinedFunction call, Set<State> go
123124
receiver, names(current), names(goal), receiver));
124125
String message = String.format("No sequence of calls moves %s from %s to %s", receiver, names(current),
125126
names(goal));
126-
List<String> constructors = machine.constructorsEnabling(call).stream().map(StateHints::constructor).toList();
127+
List<String> constructors = machine.constructorsEnabling(call).stream().map(StateHints::constructor).distinct()
128+
.toList();
127129
if (!constructors.isEmpty())
128130
message += ". Create a new object instead with " + String.join(" or ", constructors);
129131
return new Hint(Kind.UNREACHABLE_STATE, message);

‎liquidjava-verifier/src/main/java/liquidjava/processor/state_machine/StateMachineBuilder.java‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import spoon.reflect.declaration.CtElement;
2525
import spoon.reflect.declaration.CtType;
2626
import spoon.reflect.reference.CtExecutableReference;
27+
import spoon.reflect.reference.CtTypeParameterReference;
2728
import spoon.reflect.reference.CtTypeReference;
2829

2930
/**
@@ -92,12 +93,14 @@ public static List<GhostState> ghostStatesOf(TypeChecker tc, String className) {
9293

9394
private static List<GhostState> ghostStatesOf(Context context, CtTypeReference<?> type,
9495
List<CtTypeReference<?>> supertypes) {
95-
// a state set can be registered more than once, so states are told apart by name
96+
// a state set can be registered more than once, so states are told apart by name, and states are kept by
97+
// simple class name, so those of another class with the same simple name are left out
9698
Map<String, GhostState> ghostStates = new LinkedHashMap<>();
9799
for (CtTypeReference<?> t : Stream.concat(Stream.of(type), supertypes.stream()).toList()) {
98100
List<GhostState> declared = context.getGhostState(Utils.getSimpleName(t.getQualifiedName()));
99101
if (declared != null)
100-
declared.forEach(g -> ghostStates.putIfAbsent(g.getQualifiedName(), g));
102+
declared.stream().filter(g -> t.getQualifiedName().equals(g.getPrefix()))
103+
.forEach(g -> ghostStates.putIfAbsent(g.getQualifiedName(), g));
101104
}
102105
return List.copyOf(ghostStates.values());
103106
}
@@ -172,16 +175,22 @@ private StateMachine build(CtTypeReference<?> type, List<CtTypeReference<?>> sup
172175
continue;
173176
boolean constructor = CtExecutableReference.CONSTRUCTOR_NAME.equals(function.getName());
174177
if (className.equals(function.getTargetClass()) && constructor) {
178+
if (abstractType(type))
179+
continue; // cannot be suggested as a new object
175180
Optional<Set<State>> created = createdStates(function);
176181
created.ifPresent(s -> constructors.put(function, s));
177182
continue;
178183
}
184+
if (className.equals(function.getTargetClass()) && !appliesToOwnType(function, type, supertypes)) {
185+
complete = false;
186+
continue;
187+
}
179188
if (!className.equals(function.getTargetClass())) {
180189
if (constructor || !inherited.contains(function.getTargetClass()))
181190
continue;
182191
if (overridden(function, className))
183192
continue; // the class's own refinement for it is the one in the machine
184-
if (!inheritedFrom(function, type, supertypes)) {
193+
if (!resolvesTo(function, type, supertypes)) {
185194
complete = false;
186195
continue;
187196
}
@@ -205,6 +214,24 @@ private boolean subclassChangesState(String className, CtTypeReference<?> type,
205214
.map(c -> tc.getFactory().Type().createReference(c)).anyMatch(other -> other.isSubtypeOf(type));
206215
}
207216

217+
private static boolean abstractType(CtTypeReference<?> type) {
218+
CtType<?> declaration = type.getTypeDeclaration();
219+
return declaration == null || declaration.isInterface() || declaration.isAbstract();
220+
}
221+
222+
/**
223+
* Whether the verifier checks a call to {@code method}, specified for {@code type}, on an object of that type. It
224+
* looks the refinement up by the receiver's type, which needs the parameter types to match those of the method it
225+
* resolves to, and otherwise by the type that declares that method. A parameter whose type is a type variable may
226+
* not match, so then {@code type} must declare the method.
227+
*/
228+
private boolean appliesToOwnType(RefinedFunction method, CtTypeReference<?> type,
229+
List<CtTypeReference<?>> supertypes) {
230+
boolean concreteParameters = method.getArguments().stream()
231+
.noneMatch(a -> a.getType() instanceof CtTypeParameterReference);
232+
return concreteParameters || resolvesTo(method, type, supertypes);
233+
}
234+
208235
/** Whether {@code className} has its own refinement for {@code method}, with the same parameter types */
209236
private boolean overridden(RefinedFunction method, String className) {
210237
String name = Utils.getSimpleName(method.getName());
@@ -219,13 +246,11 @@ private static List<String> parameterTypes(RefinedFunction function) {
219246
}
220247

221248
/**
222-
* Whether the verifier checks a call to {@code method}, specified for a supertype, on an object of {@code type}
223-
* that has no refinement of its own for it. Like the verifier, it uses the refinement for the nearest type that
224-
* declares the method. Methods are matched by name and number of parameters, so an overload can hide an inherited
225-
* method, which only leaves the machine incomplete.
249+
* Whether the nearest type in the hierarchy of {@code type} that declares {@code method} is the one it is specified
250+
* for, which is where the verifier looks up the refinement of a call. Methods are matched by name and number of
251+
* parameters, so an overload can hide an inherited method, which only leaves the machine incomplete.
226252
*/
227-
private boolean inheritedFrom(RefinedFunction method, CtTypeReference<?> type,
228-
List<CtTypeReference<?>> supertypes) {
253+
private boolean resolvesTo(RefinedFunction method, CtTypeReference<?> type, List<CtTypeReference<?>> supertypes) {
229254
String name = Utils.getSimpleName(method.getName());
230255
int arity = method.getArguments().size();
231256
for (CtTypeReference<?> t : Stream.concat(Stream.of(type), supertypes.stream()).toList()) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ void stateSetAndMethodsFromTheReceiversSuperclassSpec() {
9797
new Hint(Kind.AVAILABLE_CALLS, "In state displayable, host can call: dispose(), pack()"));
9898
}
9999

100+
@Test
101+
void ownGenericRefinementOfAnInheritedMethodIsNotSuggested() {
102+
assertHints("classes/state_hints_generic_inherited_method_error");
103+
}
104+
105+
@Test
106+
void statesOfAnotherClassWithTheSimpleNameOfASupertypeAreLeftOut() {
107+
assertHints("classes/state_hints_same_simple_name_error",
108+
new Hint(Kind.CALL_SEQUENCE, "Call l.add(E) before this call to move l from empty to nonEmpty"),
109+
new Hint(Kind.AVAILABLE_CALLS, "In state empty, l can call: add(E)"));
110+
}
111+
100112
@Test
101113
void stateDependingOnThePathAsksForTheSameStateOnEveryPath() {
102114
assertHints("ErrorStateHintPathDependent.java", new Hint(Kind.PATH_DEPENDENT_STATE,

0 commit comments

Comments
 (0)