TypeSubstitutor respects flexible types

This commit is contained in:
Andrey Breslav
2014-06-24 20:07:46 +04:00
parent f11095b1d0
commit f7de0e274c
6 changed files with 94 additions and 3 deletions
@@ -0,0 +1,9 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
import java.util.*
fun takeJ(map: Map<Any, Any>) {}
fun test() {
takeJ(HashMap())
}
@@ -0,0 +1,20 @@
// FILE: p/J.java
package p;
import java.util.ArrayList;
public class J {
public ArrayList<String> list() { return null; }
public void takeList(ArrayList<String> list) { }
}
// FILE: k.kt
import p.*
fun test(j: J) {
j.takeList(j.list())
val l = j.list()
j.takeList(l)
}
@@ -0,0 +1,18 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// FILE: p/J.java
package p;
public class J {
public J j() { return this; }
}
// FILE: k.kt
import p.*
fun takeJ(j: J) {}
fun test() {
takeJ(J().j())
}
@@ -0,0 +1,7 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
import java.util.*
fun test(map: Map<Any, Any>) {
HashMap(map)
}
@@ -7715,6 +7715,26 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("javaCollectionToKotlin.kt")
public void testJavaCollectionToKotlin() throws Exception {
doTest("compiler/testData/diagnostics/tests/platformTypes/methodCall/javaCollectionToKotlin.kt");
}
@TestMetadata("javaToJava.kt")
public void testJavaToJava() throws Exception {
doTest("compiler/testData/diagnostics/tests/platformTypes/methodCall/javaToJava.kt");
}
@TestMetadata("javaToKotlin.kt")
public void testJavaToKotlin() throws Exception {
doTest("compiler/testData/diagnostics/tests/platformTypes/methodCall/javaToKotlin.kt");
}
@TestMetadata("kotlinCollectionToJava.kt")
public void testKotlinCollectionToJava() throws Exception {
doTest("compiler/testData/diagnostics/tests/platformTypes/methodCall/kotlinCollectionToJava.kt");
}
@TestMetadata("list.kt")
public void testList() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/methodCall/list.kt");
@@ -151,8 +151,25 @@ public class TypeSubstitutor {
@NotNull
private TypeProjection unsafeSubstitute(@NotNull TypeProjection originalProjection, int recursionDepth) throws SubstitutionException {
assertRecursionDepth(recursionDepth, originalProjection, substitution);
// The type is within the substitution range, i.e. T or T?
JetType type = originalProjection.getType();
Variance originalProjectionKind = originalProjection.getProjectionKind();
if (type instanceof FlexibleType) {
FlexibleType flexibleType = (FlexibleType) type;
TypeProjection substitutedLower =
unsafeSubstitute(new TypeProjectionImpl(originalProjectionKind, flexibleType.getLowerBound()), recursionDepth + 1);
TypeProjection substitutedUpper =
unsafeSubstitute(new TypeProjectionImpl(originalProjectionKind, flexibleType.getUpperBound()), recursionDepth + 1);
// todo: projection kind is neglected
return new TypeProjectionImpl(originalProjectionKind,
new DelegatingFlexibleType(
substitutedLower.getType(),
substitutedUpper.getType()
)
);
}
if (KotlinBuiltIns.getInstance().isNothing(type) || type.isError()) return originalProjection;
TypeProjection replacement = substitution.get(type.getConstructor());
@@ -161,7 +178,7 @@ public class TypeSubstitutor {
// It must be a type parameter: only they can be directly substituted for
TypeParameterDescriptor typeParameter = (TypeParameterDescriptor) type.getConstructor().getDeclarationDescriptor();
switch (conflictType(originalProjection.getProjectionKind(), replacement.getProjectionKind())) {
switch (conflictType(originalProjectionKind, replacement.getProjectionKind())) {
case OUT_IN_IN_POSITION:
throw new SubstitutionException("Out-projection in in-position");
case IN_IN_OUT_POSITION:
@@ -170,7 +187,7 @@ public class TypeSubstitutor {
case NO_CONFLICT:
boolean resultingIsNullable = type.isNullable() || replacement.getType().isNullable();
JetType substitutedType = TypeUtils.makeNullableAsSpecified(replacement.getType(), resultingIsNullable);
Variance resultingProjectionKind = combine(originalProjection.getProjectionKind(), replacement.getProjectionKind());
Variance resultingProjectionKind = combine(originalProjectionKind, replacement.getProjectionKind());
return new TypeProjectionImpl(resultingProjectionKind, substitutedType);
default:
@@ -187,7 +204,7 @@ public class TypeSubstitutor {
type.isNullable(), // Same nullability
substitutedArguments,
new SubstitutingScope(type.getMemberScope(), this));
return new TypeProjectionImpl(originalProjection.getProjectionKind(), substitutedType);
return new TypeProjectionImpl(originalProjectionKind, substitutedType);
}
}