Fix equality

This commit is contained in:
Victorious3 2023-03-28 11:46:30 +02:00
parent b9f19cc105
commit 3963baa9ce
2 changed files with 26 additions and 18 deletions

View File

@ -121,6 +121,16 @@ public class ASTToTargetAST {
input.add(pair);
}
static void addToEquality(Map<TypePlaceholder, TypePlaceholder> equality, TypePlaceholder from, TypePlaceholder to) {
for (var entry : new HashSet<>(equality.entrySet())) {
if (entry.getValue().equals(from)) {
equality.remove(entry.getKey());
equality.put(entry.getKey(), to);
}
}
equality.put(from, to);
}
static Set<ResultPair<?, ?>> transitiveClosure(Set<ResultPair<?, ?>> generics) {
Set<ResultPair<?, ?>> all = new HashSet<>(generics);
HashSet<ResultPair<?, ?>> toAdd = new HashSet<>();
@ -152,6 +162,7 @@ public class ASTToTargetAST {
) {
// Type variables with bounds that are also type variables of the method
for (var typeVariable : new HashSet<>(typeVariables)) {
typeVariable = equality.getOrDefault(typeVariable, typeVariable);
if (classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, typeVariable))
continue;
for (var pair : simplifiedConstraints) {
@ -573,7 +584,6 @@ public class ASTToTargetAST {
}
System.out.println(method.name + ": " + txResult + " & " + javaResult);
return generics;
}
@ -621,20 +631,17 @@ public class ASTToTargetAST {
findAllBounds(field.getType(), txResult, txEquality);
}
{ // Java Generics
var referenced = new HashSet<TypePlaceholder>();
eliminateCycles(javaResult, equality, referenced);
eliminateInfima(javaResult, equality, referenced);
var txReferenced = new HashSet<TypePlaceholder>();
eliminateInfima(txResult, txEquality, txReferenced);
eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult, equality, referenced);
equalizeTypeVariables(javaResult, equality);
}
{ // TX Generics
var referenced = new HashSet<TypePlaceholder>();
eliminateInfima(txResult, txEquality, referenced);
eliminateInnerTypeVariablesOfClass(classOrInterface, txResult, txEquality, referenced);
}
eliminateInnerTypeVariablesOfClass(classOrInterface, txResult, txEquality, txReferenced);
System.out.println("Class " + classOrInterface.getClassName().getClassName() + ": " + txResult);
System.out.println("Class " + classOrInterface.getClassName().getClassName() + ": " + txResult + ", " + javaResult);
return generics;
}
@ -642,7 +649,7 @@ public class ASTToTargetAST {
for (var pair : new HashSet<>(input)) {
if (pair instanceof PairTPHsmallerTPH ptph) {
if (ptph.left.getVariance() == 1 && ptph.right.getVariance() == -1) {
equality.put(ptph.left, ptph.right);
addToEquality(equality, ptph.left, ptph.right);
input.remove(ptph);
for (var pair2 : new HashSet<>(input)) {
if (pair2 instanceof PairTPHsmallerTPH ptph2 && ptph2.right.equals(ptph.left)) {
@ -735,7 +742,7 @@ public class ASTToTargetAST {
var right = cycle.get(i + 1);
var pair = new PairTPHsmallerTPH(left, right);
input.remove(pair);
equality.put(left, newTph);
addToEquality(equality, left, newTph);
}
}
}
@ -759,7 +766,7 @@ public class ASTToTargetAST {
addToPairs(input, new PairTPHsmallerTPH(left, newTph));
input.removeAll(infima);
for (var infimum : infima) {
equality.put(infimum.right, newTph);
addToEquality(equality, infimum.right, newTph);
new HashSet<>(input).forEach(pair -> {
if (pair.getLeft().equals(infimum.right)) {
input.remove(pair);

View File

@ -51,6 +51,7 @@ public class TestGenerics {
var anyMethod = result.findMethod("anyMethod");
var otherMethod = result.findMethod("otherMethod");
var a = result.findField("a");
var b = result.findField("b");
var generics = result.genericsResults.get(0);
assertEquals(1, generics.get(anyMethod).size());
@ -60,7 +61,7 @@ public class TestGenerics {
var ECK2 = generics.getBounds(otherMethod.getReturnType(), result.clazz, anyMethod);
var ECKChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT));
assertEquals(ECK1, ECK2);
assertEquals(ECK2, ECKChain);
assertEquals(ECK2, generics.getBounds(b.getType(), result.clazz));
var M = generics.getBounds(a.getType(), result.clazz);
var MChain = new BoundsList(new Bound(false, TypePlaceholder.of("ECK")), new Bound(false, ASTToTargetAST.OBJECT));