Type uncapturing

This commit is contained in:
Michael Bogdanov
2016-01-13 14:29:52 +03:00
parent 2228cb6a9a
commit 607f32615f
10 changed files with 142 additions and 1 deletions
@@ -2459,7 +2459,8 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
for (Map.Entry<TypeParameterDescriptor, KotlinType> entry : typeArguments.entrySet()) {
TypeParameterDescriptor key = entry.getKey();
KotlinType type = entry.getValue();
KotlinType type = TypeUtils.uncaptureTypeForInlineMapping(entry.getValue());
TypeParameterDescriptor parameterDescriptor = TypeUtils.getTypeParameterDescriptorOrNull(type);
if (parameterDescriptor == null) {
// type is not generic
@@ -0,0 +1,17 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
import java.util.*
public fun Array<in String>.slice1() = copyOfRange1 { 1 }
fun box(): String {
val comparable = arrayOf("123").slice1()
val method = comparable.javaClass.getMethod("test", Any::class.java)
val genericParameterTypes = method.genericParameterTypes
if (genericParameterTypes.size != 1) return "fail 1: ${genericParameterTypes.size}"
var name = (genericParameterTypes[0] as Class<*>).name
if (name != "java.lang.Object") return "fail 2: ${name}"
return "OK"
}
@@ -0,0 +1,12 @@
package test
interface F<T> {
fun test(p: T) : Int
}
inline fun <T: Any?> Array<T>.copyOfRange1(crossinline toIndex: () -> Int) =
object : F<T> {
override fun test(p: T): Int {
return toIndex()
}
}
@@ -0,0 +1,17 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
import java.util.*
public fun Array<out CharSequence>.slice1() = copyOfRange1 { 1 }
fun box(): String {
val comparable = arrayOf("123").slice1()
val method = comparable.javaClass.getMethod("test", Any::class.java)
val genericParameterTypes = method.genericParameterTypes
if (genericParameterTypes.size != 1) return "fail 1: ${genericParameterTypes.size}"
var name = (genericParameterTypes[0] as Class<*>).name
if (name != "java.lang.CharSequence") return "fail 2: ${name}"
return "OK"
}
@@ -0,0 +1,12 @@
package test
interface F<T> {
fun test(p: T) : Int
}
inline fun <T> Array<T>.copyOfRange1(crossinline toIndex: () -> Int) =
object : F<T> {
override fun test(p: T): Int {
return toIndex()
}
}
@@ -0,0 +1,17 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
import java.util.*
public fun Array<*>.slice1() = copyOfRange1 { 1 }
fun box(): String {
val comparable = arrayOf("123").slice1()
val method = comparable.javaClass.getMethod("test", Any::class.java)
val genericParameterTypes = method.genericParameterTypes
if (genericParameterTypes.size != 1) return "fail 1: ${genericParameterTypes.size}"
var name = (genericParameterTypes[0] as Class<*>).name
if (name != "java.lang.Object") return "fail 2: ${name}"
return "OK"
}
@@ -0,0 +1,12 @@
package test
interface F<T> {
fun test(p: T) : Int
}
inline fun <T> Array<T>.copyOfRange1(crossinline toIndex: () -> Int) =
object : F<T> {
override fun test(p: T): Int {
return toIndex()
}
}
@@ -1429,6 +1429,18 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.1.kt$"), true);
}
@TestMetadata("inProjectionSubstitution.1.kt")
public void testInProjectionSubstitution() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/signature/inProjectionSubstitution.1.kt");
doTestMultiFileWithInlineCheck(fileName);
}
@TestMetadata("outProjectionSubstitution.1.kt")
public void testOutProjectionSubstitution() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/signature/outProjectionSubstitution.1.kt");
doTestMultiFileWithInlineCheck(fileName);
}
@TestMetadata("recursion.1.kt")
public void testRecursion() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/signature/recursion.1.kt");
@@ -1447,6 +1459,12 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
doTestMultiFileWithInlineCheck(fileName);
}
@TestMetadata("starProjectionSubstitution.1.kt")
public void testStarProjectionSubstitution() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/signature/starProjectionSubstitution.1.kt");
doTestMultiFileWithInlineCheck(fileName);
}
@TestMetadata("typeParameterInLambda.1.kt")
public void testTypeParameterInLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/signature/typeParameterInLambda.1.kt");
@@ -1429,6 +1429,18 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.1.kt$"), true);
}
@TestMetadata("inProjectionSubstitution.1.kt")
public void testInProjectionSubstitution() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/signature/inProjectionSubstitution.1.kt");
doBoxTestWithInlineCheck(fileName);
}
@TestMetadata("outProjectionSubstitution.1.kt")
public void testOutProjectionSubstitution() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/signature/outProjectionSubstitution.1.kt");
doBoxTestWithInlineCheck(fileName);
}
@TestMetadata("recursion.1.kt")
public void testRecursion() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/signature/recursion.1.kt");
@@ -1447,6 +1459,12 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
doBoxTestWithInlineCheck(fileName);
}
@TestMetadata("starProjectionSubstitution.1.kt")
public void testStarProjectionSubstitution() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/signature/starProjectionSubstitution.1.kt");
doBoxTestWithInlineCheck(fileName);
}
@TestMetadata("typeParameterInLambda.1.kt")
public void testTypeParameterInLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/signature/typeParameterInLambda.1.kt");
@@ -25,9 +25,12 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.resolve.calls.inference.CapturedType;
import org.jetbrains.kotlin.resolve.calls.inference.CapturedTypeConstructor;
import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstructor;
import org.jetbrains.kotlin.resolve.scopes.MemberScope;
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
import java.util.*;
@@ -514,8 +517,22 @@ public class TypeUtils {
return typeParameterDescriptor != null && typeParameterDescriptor.isReified();
}
@NotNull
public static KotlinType uncaptureTypeForInlineMapping(@NotNull KotlinType type) {
TypeConstructor constructor = type.getConstructor();
if (constructor instanceof CapturedTypeConstructor) {
TypeProjection projection = ((CapturedTypeConstructor) constructor).getTypeProjection();
if (Variance.IN_VARIANCE == projection.getProjectionKind()) {
//in variance could be captured only for <T> or <T: Any?> declarations
return TypeUtilsKt.getBuiltIns(type).getNullableAnyType();
}
return uncaptureTypeForInlineMapping(projection.getType());
}
return type;
}
@Nullable
public static TypeParameterDescriptor getTypeParameterDescriptorOrNull(@NotNull KotlinType type) {
assert !(type instanceof CapturedType) : "Type should be non-captured " + type;
if (type.getConstructor().getDeclarationDescriptor() instanceof TypeParameterDescriptor) {
return (TypeParameterDescriptor) type.getConstructor().getDeclarationDescriptor();
}