TestContraVariant

This commit is contained in:
Daniel Holle 2023-05-09 12:18:12 +02:00
parent f8f76e9f7d
commit 8992260264
2 changed files with 20 additions and 10 deletions

View File

@ -1,6 +1,7 @@
public class TestContraVariant {
main(x) {
return x;
var pair = m(x);
return pair.a;
}
m(x) {

View File

@ -206,8 +206,10 @@ public class ASTToTargetAST {
if (classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, typeVariable))
continue;
for (var pair : simplifiedConstraints) {
if (pair.left.equals(typeVariable) && typeVariables.contains(pair.right)) {
addToPairs(result, new PairTPHsmallerTPH(pair.left, equality.getOrDefault(pair.right, pair.right)));
var left = equality.getOrDefault(pair.left, pair.left);
var right = equality.getOrDefault(pair.right, pair.right);
if (left.equals(typeVariable) && typeVariables.contains(right)) {
addToPairs(result, new PairTPHsmallerTPH(left, right));
}
}
}
@ -249,7 +251,6 @@ public class ASTToTargetAST {
if (optMethod.isEmpty()) return;
var method = optMethod.get();
var generics = generics(owner, method);
System.out.println("Generics: " + generics);
// transitive and
var all = transitiveClosure(generics);
@ -365,13 +366,16 @@ public class ASTToTargetAST {
var closure = transitiveClosure((Set) simplifiedConstraints);
// Type variables with bounds that are also type variables of the class
for (var typeVariable : new HashSet<>(typeVariables)) {
typeVariable = equality.getOrDefault(typeVariable, typeVariable);
if (typeVariablesOfClass.contains(typeVariable)) continue;
var pairs = new HashSet<PairTPHsmallerTPH>();
for (var pair : closure) {
if (!(pair instanceof PairTPHsmallerTPH ptph)) continue;
if (ptph.left.equals(typeVariable) && typeVariablesOfClass.contains(ptph.right)) {
pairs.add(new PairTPHsmallerTPH(ptph.left, equality.getOrDefault(ptph.right, ptph.right)));
var left = equality.getOrDefault(ptph.left, ptph.left);
var right = equality.getOrDefault(ptph.right, ptph.right);
if (left.equals(typeVariable) && typeVariablesOfClass.contains(right)) {
pairs.add(new PairTPHsmallerTPH(left, right));
}
}
@ -402,12 +406,16 @@ public class ASTToTargetAST {
}
if (minimalPair != null)
addToPairs(result, minimalPair);
addToPairs(result, new PairTPHsmallerTPH(
equality.getOrDefault(minimalPair.left, minimalPair.left),
equality.getOrDefault(minimalPair.right, minimalPair.right)
));
}
// All unbounded type variables (bounds not in method)
outer:
for (var typeVariable : typeVariables) {
typeVariable = equality.getOrDefault(typeVariable, typeVariable);
if (classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, typeVariable))
continue;
for (var pair : result) {
@ -420,12 +428,13 @@ public class ASTToTargetAST {
// All unbounded bounds
outer:
for (var pair : simplifiedConstraints) {
var r = equality.getOrDefault(pair.right, pair.right);
for (var pair2 : simplifiedConstraints) {
if (pair.right.equals(pair2.left))
if (r.equals(equality.getOrDefault(pair2.left, pair2.left)))
continue outer;
}
if (!classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, pair.right) && typeVariables.contains(pair.right)) {
addToPairs(result, new PairTPHequalRefTypeOrWildcardType(pair.right, OBJECT));
if (!classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, r) && typeVariables.contains(r)) {
addToPairs(result, new PairTPHequalRefTypeOrWildcardType(r, OBJECT));
}
}
}