JVM IR: support raw types in typeOf

This commit is contained in:
Alexander Udalov
2021-06-30 22:23:17 +02:00
parent 012c7c39af
commit ae07127f08
9 changed files with 338 additions and 4 deletions
@@ -37445,6 +37445,18 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/reflection/typeOf/mutableCollections_before.kt");
}
@Test
@TestMetadata("rawTypes_after.kt")
public void testRawTypes_after() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/rawTypes_after.kt");
}
@Test
@TestMetadata("rawTypes_before.kt")
public void testRawTypes_before() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/rawTypes_before.kt");
}
@Test
@TestMetadata("typeOfCapturedStar.kt")
public void testTypeOfCapturedStar() throws Exception {
@@ -37506,6 +37518,18 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/mutableCollections_before.kt");
}
@Test
@TestMetadata("rawTypes_after.kt")
public void testRawTypes_after() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_after.kt");
}
@Test
@TestMetadata("rawTypes_before.kt")
public void testRawTypes_before() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_before.kt");
}
@Test
@TestMetadata("typeReferenceEqualsHashCode.kt")
public void testTypeReferenceEqualsHashCode() throws Exception {
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.ir.types
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.types.impl.IrStarProjectionImpl
import org.jetbrains.kotlin.ir.types.impl.buildSimpleType
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.ir.util.hasAnnotation
@@ -26,7 +27,8 @@ private class IrJvmFlexibleTypeImpl(
val irType: IrSimpleType,
val builtIns: IrBuiltIns,
val nullability: Boolean,
val mutability: Boolean
val mutability: Boolean,
val raw: Boolean,
) : IrJvmFlexibleType {
override val lowerBound: IrSimpleType
get() = irType.buildSimpleType {
@@ -55,12 +57,16 @@ private class IrJvmFlexibleTypeImpl(
else -> error("Mutability-flexible type with unknown classifier: ${irType.render()}, FQ name: $readonlyClassFqName")
}
}
if (raw) {
arguments = List(arguments.size) { IrStarProjectionImpl }
}
kotlinType = null
}
}
internal val FLEXIBLE_NULLABILITY_FQN = FqName("kotlin.internal.ir.FlexibleNullability")
internal val FLEXIBLE_MUTABILITY_FQN = FqName("kotlin.internal.ir.FlexibleMutability")
internal val RAW_TYPE_FQN = FqName("kotlin.internal.ir.RawType")
internal fun IrType.isWithFlexibleNullability(): Boolean =
hasAnnotation(FLEXIBLE_NULLABILITY_FQN)
@@ -68,17 +74,21 @@ internal fun IrType.isWithFlexibleNullability(): Boolean =
internal fun IrType.isWithFlexibleMutability(): Boolean =
hasAnnotation(FLEXIBLE_MUTABILITY_FQN)
internal fun IrType.isRaw(): Boolean =
hasAnnotation(RAW_TYPE_FQN)
internal fun IrType.asJvmFlexibleType(builtIns: IrBuiltIns): FlexibleTypeMarker? {
if (this !is IrSimpleType || annotations.isEmpty()) return null
val nullability = isWithFlexibleNullability()
val mutability = isWithFlexibleMutability()
if (!nullability && !mutability) return null
val raw = isRaw()
if (!nullability && !mutability && !raw) return null
val baseType = this.removeAnnotations { irCtorCall ->
val fqName = irCtorCall.type.classFqName
fqName == FLEXIBLE_NULLABILITY_FQN || fqName == FLEXIBLE_MUTABILITY_FQN
fqName == FLEXIBLE_NULLABILITY_FQN || fqName == FLEXIBLE_MUTABILITY_FQN || fqName == RAW_TYPE_FQN
} as IrSimpleType
return IrJvmFlexibleTypeImpl(baseType, builtIns, nullability, mutability)
return IrJvmFlexibleTypeImpl(baseType, builtIns, nullability, mutability, raw)
}
@@ -0,0 +1,57 @@
// !API_VERSION: LATEST
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// FILE: box.kt
package test
import kotlin.reflect.KType
import kotlin.reflect.typeOf
import kotlin.test.assertEquals
fun check(expected: String, actual: KType) {
assertEquals(expected + " (Kotlin reflection is not available)", actual.toString())
}
class C<T, U : Number>
fun box(): String {
check("test.C<java.lang.Object?, java.lang.Number>", returnTypeOf { J.raw() })
check("test.C<java.lang.Object?, java.lang.Number>", returnTypeOf { J.rawNotNull() })
check("java.util.List<java.lang.Object?>", returnTypeOf { J.rawList() })
check("java.util.Map<java.lang.Object?, java.lang.Object?>", returnTypeOf { J.rawNotNullMap() })
return "OK"
}
inline fun <reified T : Any> returnTypeOf(block: () -> T) =
typeOf<T>()
// FILE: J.java
import test.C;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
public class J {
public static C raw() {
return null;
}
@NotNull
public static C rawNotNull() {
return null;
}
public static List rawList() {
return null;
}
@NotNull
public static Map rawNotNullMap() {
return null;
}
}
@@ -0,0 +1,57 @@
// !API_VERSION: 1.5
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// FILE: box.kt
package test
import kotlin.reflect.KType
import kotlin.reflect.typeOf
import kotlin.test.assertEquals
fun check(expected: String, actual: KType) {
assertEquals(expected + " (Kotlin reflection is not available)", actual.toString())
}
class C<T, U : Number>
fun box(): String {
check("test.C<java.lang.Object?, java.lang.Number>", returnTypeOf { J.raw() })
check("test.C<java.lang.Object?, java.lang.Number>", returnTypeOf { J.rawNotNull() })
check("java.util.List<java.lang.Object?>", returnTypeOf { J.rawList() })
check("java.util.Map<java.lang.Object?, java.lang.Object?>", returnTypeOf { J.rawNotNullMap() })
return "OK"
}
inline fun <reified T : Any> returnTypeOf(block: () -> T) =
typeOf<T>()
// FILE: J.java
import test.C;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
public class J {
public static C raw() {
return null;
}
@NotNull
public static C rawNotNull() {
return null;
}
public static List rawList() {
return null;
}
@NotNull
public static Map rawNotNullMap() {
return null;
}
}
@@ -0,0 +1,59 @@
// !API_VERSION: LATEST
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_REFLECT
// FILE: box.kt
package test
import kotlin.reflect.KType
import kotlin.reflect.typeOf
class C<T, U : Number>
fun box(): String {
val v1 = returnTypeOf { J.raw() }.toString()
if (v1 != "(test.C<kotlin.Any?, kotlin.Number>..test.C<*, *>?)") return "Fail 1: $v1"
val v2 = returnTypeOf { J.rawNotNull() }.toString()
if (v2 != "(test.C<kotlin.Any?, kotlin.Number>..test.C<*, *>)") return "Fail 2: $v2"
val v3 = returnTypeOf { J.rawList() }.toString()
if (v3 != "(kotlin.collections.MutableList<kotlin.Any?>..kotlin.collections.List<*>?)") return "Fail 3: $v3"
val v4 = returnTypeOf { J.rawNotNullMap() }.toString()
if (v4 != "(kotlin.collections.MutableMap<kotlin.Any?, kotlin.Any?>..kotlin.collections.Map<*, *>)") return "Fail 4: $v4"
return "OK"
}
inline fun <reified T : Any> returnTypeOf(block: () -> T) =
typeOf<T>()
// FILE: J.java
import test.C;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
public class J {
public static C raw() {
return null;
}
@NotNull
public static C rawNotNull() {
return null;
}
public static List rawList() {
return null;
}
@NotNull
public static Map rawNotNullMap() {
return null;
}
}
@@ -0,0 +1,59 @@
// !API_VERSION: 1.5
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_REFLECT
// FILE: box.kt
package test
import kotlin.reflect.KType
import kotlin.reflect.typeOf
class C<T, U : Number>
fun box(): String {
val v1 = returnTypeOf { J.raw() }.toString()
if (v1 != "test.C<kotlin.Any?, kotlin.Number>") return "Fail 1: $v1"
val v2 = returnTypeOf { J.rawNotNull() }.toString()
if (v2 != "test.C<kotlin.Any?, kotlin.Number>") return "Fail 2: $v2"
val v3 = returnTypeOf { J.rawList() }.toString()
if (v3 != "kotlin.collections.List<kotlin.Any?>") return "Fail 3: $v3"
val v4 = returnTypeOf { J.rawNotNullMap() }.toString()
if (v4 != "kotlin.collections.Map<kotlin.Any?, kotlin.Any?>") return "Fail 4: $v4"
return "OK"
}
inline fun <reified T : Any> returnTypeOf(block: () -> T) =
typeOf<T>()
// FILE: J.java
import test.C;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
public class J {
public static C raw() {
return null;
}
@NotNull
public static C rawNotNull() {
return null;
}
public static List rawList() {
return null;
}
@NotNull
public static Map rawNotNullMap() {
return null;
}
}
@@ -37403,6 +37403,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/reflection/typeOf/mutableCollections_before.kt");
}
@Test
@TestMetadata("rawTypes_after.kt")
public void testRawTypes_after() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/rawTypes_after.kt");
}
@Test
@TestMetadata("rawTypes_before.kt")
public void testRawTypes_before() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/rawTypes_before.kt");
}
@Test
@TestMetadata("typeOfCapturedStar.kt")
public void testTypeOfCapturedStar() throws Exception {
@@ -37464,6 +37476,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/mutableCollections_before.kt");
}
@Test
@TestMetadata("rawTypes_after.kt")
public void testRawTypes_after() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_after.kt");
}
@Test
@TestMetadata("rawTypes_before.kt")
public void testRawTypes_before() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_before.kt");
}
@Test
@TestMetadata("typeReferenceEqualsHashCode.kt")
public void testTypeReferenceEqualsHashCode() throws Exception {
@@ -37445,6 +37445,18 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/reflection/typeOf/mutableCollections_before.kt");
}
@Test
@TestMetadata("rawTypes_after.kt")
public void testRawTypes_after() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/rawTypes_after.kt");
}
@Test
@TestMetadata("rawTypes_before.kt")
public void testRawTypes_before() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/rawTypes_before.kt");
}
@Test
@TestMetadata("typeOfCapturedStar.kt")
public void testTypeOfCapturedStar() throws Exception {
@@ -37506,6 +37518,18 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/mutableCollections_before.kt");
}
@Test
@TestMetadata("rawTypes_after.kt")
public void testRawTypes_after() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_after.kt");
}
@Test
@TestMetadata("rawTypes_before.kt")
public void testRawTypes_before() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_before.kt");
}
@Test
@TestMetadata("typeReferenceEqualsHashCode.kt")
public void testTypeReferenceEqualsHashCode() throws Exception {
@@ -29772,6 +29772,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/reflection/typeOf/mutableCollections_before.kt");
}
@TestMetadata("rawTypes_after.kt")
public void testRawTypes_after() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/rawTypes_after.kt");
}
@TestMetadata("rawTypes_before.kt")
public void testRawTypes_before() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/rawTypes_before.kt");
}
@TestMetadata("typeOfCapturedStar.kt")
public void testTypeOfCapturedStar() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/typeOfCapturedStar.kt");
@@ -29832,6 +29842,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/mutableCollections_before.kt");
}
@TestMetadata("rawTypes_after.kt")
public void testRawTypes_after() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_after.kt");
}
@TestMetadata("rawTypes_before.kt")
public void testRawTypes_before() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_before.kt");
}
@TestMetadata("typeReferenceEqualsHashCode.kt")
public void testTypeReferenceEqualsHashCode() throws Exception {
runTest("compiler/testData/codegen/box/reflection/typeOf/noReflect/typeReferenceEqualsHashCode.kt");