Skip to content

Commit 52fcd80

Browse files
committed
Code Refactoring (GPT-5)
1 parent b8d5efb commit 52fcd80

3 files changed

Lines changed: 41 additions & 40 deletions

File tree

liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/VCChecker.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.ArrayList;
44
import java.util.HashMap;
5-
import java.util.HashSet;
65
import java.util.List;
76
import java.util.Set;
87
import java.util.Stack;
@@ -113,38 +112,37 @@ public boolean canProcessSubtyping(Predicate type, Predicate expectedType, List<
113112
*/
114113
private List<GhostState> filterGhostStatesForVariables(List<GhostState> list, List<RefinedVariable> mainVars,
115114
List<RefinedVariable> vars) {
116-
if (list.isEmpty()) return list;
115+
if (list.isEmpty())
116+
return list;
117117

118118
// Collect all relevant qualified type names from involved variables and their supertypes
119-
Set<String> allowedPrefixes = new HashSet<>();
119+
if (list == null || list.isEmpty())
120+
return list;
121+
122+
// Collect all relevant qualified type names (types + supertypes), keeping order and deduping
123+
Set<String> allowedPrefixes = new java.util.LinkedHashSet<>();
120124
Consumer<RefinedVariable> collect = rv -> {
121-
if (rv.getType() != null)
125+
if (rv.getType() != null) {
122126
allowedPrefixes.add(rv.getType().getQualifiedName());
127+
}
123128
for (CtTypeReference<?> st : rv.getSuperTypes()) {
124-
if (st != null) allowedPrefixes.add(st.getQualifiedName());
129+
if (st != null) {
130+
allowedPrefixes.add(st.getQualifiedName());
131+
}
125132
}
126133
};
127134
mainVars.forEach(collect);
128135
vars.forEach(collect);
129136

130-
// If we couldn't determine any types, return the original list
131-
if (allowedPrefixes.isEmpty()) return list;
132-
133-
// Otherwise filter the list
134-
List<GhostState> filtered = new ArrayList<>();
135-
for (GhostState g : list) {
136-
String prefix;
137-
if (g.getParent() != null) {
138-
prefix = g.getParent().getPrefix();
139-
} else {
140-
// Standalone ghosts use their own prefix
141-
prefix = g.getPrefix();
142-
}
143-
if (allowedPrefixes.contains(prefix)) {
144-
filtered.add(g);
145-
}
146-
}
147-
// Fallback to original if filtering removed everything
137+
if (allowedPrefixes.isEmpty())
138+
return list; // avoid over-filtering when types are unknown
139+
140+
List<GhostState> filtered = list.stream().filter(g -> {
141+
String prefix = (g.getParent() != null) ? g.getParent().getPrefix() : g.getPrefix();
142+
return allowedPrefixes.contains(prefix);
143+
}).collect(Collectors.toList());
144+
145+
// If nothing matched, keep original to avoid accidental empties
148146
return filtered.isEmpty() ? list : filtered;
149147
}
150148

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,25 +212,27 @@ private static Predicate getMissingStates(String t, TypeChecker tc, Predicate p)
212212
* Collect ghost states for the given qualified class name and its immediate supertypes (superclass and interfaces).
213213
*/
214214
private static List<GhostState> getGhostStatesFor(String qualifiedClass, TypeChecker tc) {
215-
List<GhostState> res = new ArrayList<>();
216-
String simpleT = Utils.getSimpleName(qualifiedClass);
217-
List<GhostState> base = tc.getContext().getGhostState(simpleT);
218-
if (base != null)
219-
res.addAll(base);
215+
// Keep order: class, then superclass, then interfaces; avoid duplicates
216+
java.util.LinkedHashSet<String> typeNames = new java.util.LinkedHashSet<>();
217+
typeNames.add(Utils.getSimpleName(qualifiedClass));
218+
220219
CtTypeReference<?> ref = tc.getFactory().Type().createReference(qualifiedClass);
221220
if (ref != null) {
222221
CtTypeReference<?> sup = ref.getSuperclass();
223-
if (sup != null) {
224-
List<GhostState> supStates = tc.getContext().getGhostState(Utils.getSimpleName(sup.getQualifiedName()));
225-
if (supStates != null)
226-
res.addAll(supStates);
227-
}
222+
if (sup != null)
223+
typeNames.add(Utils.getSimpleName(sup.getQualifiedName()));
228224
for (CtTypeReference<?> itf : ref.getSuperInterfaces()) {
229-
List<GhostState> ifStates = tc.getContext().getGhostState(Utils.getSimpleName(itf.getQualifiedName()));
230-
if (ifStates != null)
231-
res.addAll(ifStates);
225+
if (itf != null)
226+
typeNames.add(Utils.getSimpleName(itf.getQualifiedName()));
232227
}
233228
}
229+
230+
List<GhostState> res = new ArrayList<>();
231+
for (String tn : typeNames) {
232+
List<GhostState> states = tc.getContext().getGhostState(tn);
233+
if (states != null)
234+
res.addAll(states);
235+
}
234236
return res;
235237
}
236238

liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ public Expr<?> makeFunctionInvocation(String name, Expr<?>[] params) throws Exce
9393
if (name.equals("getFromIndex"))
9494
return makeSelect(name, params);
9595
FuncDecl<?> fd = funcTranslation.get(name);
96-
if (fd == null) fd = resolveFunctionDeclFallback(name, params);
97-
96+
if (fd == null)
97+
fd = resolveFunctionDeclFallback(name, params);
98+
9899
Sort[] s = fd.getDomain();
99100
for (int i = 0; i < s.length; i++) {
100101
Expr<?> param = params[i];
@@ -116,8 +117,8 @@ public Expr<?> makeFunctionInvocation(String name, Expr<?>[] params) throws Exce
116117

117118
/**
118119
* Fallback resolver for function declarations when an exact qualified name lookup fails. Tries to match by simple
119-
* name and number of parameters, preferring an exact qualified-name match if found among candidates; otherwise returns the first
120-
* compatible candidate and relies on later coercion via var supertypes.
120+
* name and number of parameters, preferring an exact qualified-name match if found among candidates; otherwise
121+
* returns the first compatible candidate and relies on later coercion via var supertypes.
121122
*/
122123
private FuncDecl<?> resolveFunctionDeclFallback(String name, Expr<?>[] params) throws Exception {
123124
String simple = Utils.getSimpleName(name);

0 commit comments

Comments
 (0)