Skip to content

Commit a464ff2

Browse files
Warn when a state set is named like a method of its class (#516)
Closes #514 ## What and why With named state sets, a set whose name is also a method name makes state errors read like method calls. With a set named `undo`, the error says `expected undo(undoManager) == undoable`, which reads as "the result of `undoManager.undo()`", the very call that failed. This is a confound for the error-message study. This PR: - Adds a **warning** (not an error: nothing is ambiguous for the verifier) when a `@StateSet` name is also the name of a method of its class. Inherited methods count too, since `x.undo()` reads the same either way, except private methods of a superclass. For an `@ExternalRefinementsFor` spec it checks the methods of the class the spec refines (this includes the methods declared in the spec). Sets without a name are not checked. - Renames the sets in `state_hint_study_undo_manager_correct` from `phase`/`undo`/`redo` to `phase`/`undoState`/`redoState`. - Adds one sentence to the README after the named-set paragraph. ## Warning text ``` Warning: State set name 'undo' is also the name of a method of UndoManager, so undo(x) == undoable reads like a call to undo(). Choose another name, such as undoState ``` The example state is the first state of the set. ## Before / after (real verifier output) `state_hint_study_undo_manager_error` from main, with its sets named as in the correct fixture. Only the `UNDO` error is shown; `REDO` is the same. **Before** (main, sets `phase`/`undo`/`redo`): no warning, and the error reads like a call. ``` State Refinement Error: expected undo(undoManager) == undoable but could not prove it 33 | case UNDO -> undoManager.undo(); // Expect: State Refinement Error | ^^^^^^^^^^^^^^^^^^^ ``` **After** (this branch, same names): two warnings on the set names, then the same errors. ``` Warning: State set name 'undo' is also the name of a method of UndoManager, so undo(x) == undoable reads like a call to undo(). Choose another name, such as undoState 20 | @stateset(name="undo", value={"undoable", "notUndoable"}) | ^^^^ Warning: State set name 'redo' is also the name of a method of UndoManager, so redo(x) == redoable reads like a call to redo(). Choose another name, such as redoState 21 | @stateset(name="redo", value={"redoable", "notRedoable"}) | ^^^^ ``` **After renaming** to `phase`/`undoState`/`redoState`: no warning, and the error no longer looks like a call. ``` State Refinement Error: expected undoState(undoManager) == undoable but could not prove it 33 | case UNDO -> undoManager.undo(); // Expect: State Refinement Error | ^^^^^^^^^^^^^^^^^^^ ``` ## Tests Three `// Expect: Warning` cases and one no-warning case added to the existing `ErrorStateSetNames` fixture. Each one fails without the fix (main reports 6 diagnostics on the file, the fixture expects 9): - `ErrorStateSetNameOfAMethod`: a set `undo` on a class with a method `undo()`. - `ErrorStateSetNameOfAnInheritedMethod`: a set `undo` on a subclass that inherits `undo()`. It also has a set `reset` with no expected warning, because the superclass's `reset()` is private (without that rule it warns once too many). - `ErrorStateSetNameOfAMethodOfTheTarget`: an `@ExternalRefinementsFor("java.util.ArrayList")` spec with a set `clear`. The renamed undo manager fixture keeps passing with no warning. Full `mvn test` passes locally. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 3b6d47a commit a464ff2

4 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ A state set can be given a `name`, and a state is then written as a value of its
6767
State Refinement Error: found connection(socket) == bound but expected connection(socket) == connected
6868
```
6969

70+
Choose set names that are not also names of methods of the class: with a set named `undo`, `undo(x) == undoable` reads like a call to `undo()`, so LiquidJava warns about such names.
71+
7072
Lambda bodies are checked separately from the code that creates them. Creating a lambda does not apply its state
7173
transitions to captured objects. While checking the body, LiquidJava keeps declared refinements but forgets the current
7274
state of mutable captures, since that state may change before invocation. Callback execution summaries are not supported:

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package testSuite;
22

3+
import liquidjava.specification.ExternalRefinementsFor;
34
import liquidjava.specification.Ghost;
45
import liquidjava.specification.RefinementPredicate;
56
import liquidjava.specification.StateSet;
67

78
// A state set name must be a lowercase identifier that no state, ghost or other state set of the class uses, and
8-
// not one of the names given to state sets without a name
9+
// not one of the names given to state sets without a name. A name that is also a method of the class is only a
10+
// warning (#514): undo(x) == undoable reads like a call to undo()
911
@StateSet(name = "Door", value = {"closed", "opened"}) // Expect: Error
1012
public class ErrorStateSetNames {}
1113

@@ -27,3 +29,17 @@ class ErrorStateSetNameOfARefinementPredicate {}
2729

2830
@StateSet(name = "state2", value = {"closed", "opened"}) // Expect: Error
2931
class ErrorStateSetNameOfAGeneratedName {}
32+
33+
@StateSet(name = "undo", value = {"undoable", "notUndoable"}) // Expect: Warning
34+
class ErrorStateSetNameOfAMethod {
35+
void undo() {}
36+
private void reset() {}
37+
}
38+
39+
@StateSet(name = "undo", value = {"undoable", "notUndoable"}) // Expect: Warning
40+
@StateSet(name = "reset", value = {"fresh", "used"}) // no warning: reset() is private to the superclass
41+
class ErrorStateSetNameOfAnInheritedMethod extends ErrorStateSetNameOfAMethod {}
42+
43+
@ExternalRefinementsFor("java.util.ArrayList")
44+
@StateSet(name = "clear", value = {"empty", "nonEmpty"}) // Expect: Warning
45+
interface ErrorStateSetNameOfAMethodOfTheTarget {}

‎liquidjava-example/src/main/java/testSuite/classes/state_hint_study_undo_manager_correct/UndoManagerRefinementsExpert.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
// once (AbstractUndoableEdit.hasBeenDone), so end() makes it undoable and not redoable.
1818
@ExternalRefinementsFor("javax.swing.undo.UndoManager")
1919
@StateSet(name="phase", value={"start", "canEdit", "end"})
20-
@StateSet(name="undo", value={"undoable", "notUndoable"})
21-
@StateSet(name="redo", value={"redoable", "notRedoable"})
20+
@StateSet(name="undoState", value={"undoable", "notUndoable"})
21+
@StateSet(name="redoState", value={"redoable", "notRedoable"})
2222
public interface UndoManagerRefinementsExpert {
2323

2424
@StateRefinement(to="start(this) && notUndoable(this) && notRedoable(this)")
@@ -81,9 +81,9 @@ public interface UndoManagerRefinementsExpert {
8181
// Queries: they never change the manager, they tell the verifier which state it is in. When it is
8282
// already known to be undoable / redoable the answer is true; otherwise (not known, or a field whose
8383
// state is unknown) the answer decides.
84-
// When canUndo() returns false it keeps the undo set by name.
84+
// When canUndo() returns false it keeps the undoState set by name.
8585
@StateRefinement(from="undoable(this)", to="return && undoable(this)")
86-
@StateRefinement(to="return ? undoable(this) : undo(this) == undo(old(this))")
86+
@StateRefinement(to="return ? undoable(this) : undoState(this) == undoState(old(this))")
8787
boolean canUndo();
8888

8989
@StateRefinement(from="redoable(this)", to="return && redoable(this)")

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import liquidjava.diagnostics.errors.*;
1313
import liquidjava.diagnostics.categories.ErrorCategory;
1414
import liquidjava.diagnostics.hints.Hint;
15+
import liquidjava.diagnostics.warnings.CustomWarning;
1516
import liquidjava.diagnostics.warnings.UnsatisfiableRefinementWarning;
1617
import liquidjava.processor.context.AliasWrapper;
1718
import liquidjava.processor.context.Context;
@@ -230,8 +231,10 @@ public void registerGhostsAndStateSets(CtElement element) throws LJError {
230231
}
231232
// checked once every set and ghost of the class exists, so the order of the annotations does not matter; a
232233
// generated name such as state1 can clash too
233-
for (Map.Entry<GhostFunction, SourcePosition> stateSet : stateSets.entrySet())
234+
for (Map.Entry<GhostFunction, SourcePosition> stateSet : stateSets.entrySet()) {
234235
checkStateSetNameIsFree(stateSet.getKey(), stateSet.getValue());
236+
warnIfStateSetNameIsAMethod(stateSet.getKey(), stateSet.getValue());
237+
}
235238
}
236239

237240
/**
@@ -285,6 +288,28 @@ private void checkStateSetNameIsFree(GhostFunction set, SourcePosition position)
285288
set.getParentClassName()), position);
286289
}
287290

291+
/**
292+
* Warns about a named state set whose name is also the name of a method of its class, or of the class an external
293+
* spec refines: in a message such as {@code undo(x) == undoable} the set reads like a call to that method
294+
*/
295+
private void warnIfStateSetNameIsAMethod(GhostFunction set, SourcePosition position) {
296+
String name = set.getName();
297+
if (name.matches(Formats.STATE_SET_NAMES))
298+
return; // a set without a name, whose name the user did not choose
299+
CtType<?> type = factory.Type().createReference(set.getPrefix()).getTypeDeclaration();
300+
// a private method of a superclass is not a method of this class
301+
if (type == null || type.getAllMethods().stream()
302+
.noneMatch(m -> m.getSimpleName().equals(name) && (m.getDeclaringType() == type || !m.isPrivate())))
303+
return;
304+
String state = context.getGhostState(set.getParentClassName()).stream().filter(gs -> gs.getParent() == set)
305+
.map(GhostState::getName).findFirst().orElse("...");
306+
diagnostics.add(new CustomWarning(position,
307+
String.format(
308+
"State set name '%s' is also the name of a method of %s, so %s(x) == %s reads like a "
309+
+ "call to %s(). Choose another name, such as %sState",
310+
name, set.getParentClassName(), name, state, name, name)));
311+
}
312+
288313
private GhostFunction createStateSet(CtNewArray<String> e, String name, CtElement element) throws LJError {
289314

290315
// if any of the states starts with uppercase, throw error (reserved for alias)

0 commit comments

Comments
 (0)