From ae07127f08b39ef2ae7f3abbf030475785055d80 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 30 Jun 2021 22:23:17 +0200 Subject: [PATCH] JVM IR: support raw types in typeOf --- .../FirBlackBoxCodegenTestGenerated.java | 24 ++++++++ .../ir/types/jvmSpecificFlexibleTypes.kt | 18 ++++-- .../typeOf/noReflect/rawTypes_after.kt | 57 ++++++++++++++++++ .../typeOf/noReflect/rawTypes_before.kt | 57 ++++++++++++++++++ .../box/reflection/typeOf/rawTypes_after.kt | 59 +++++++++++++++++++ .../box/reflection/typeOf/rawTypes_before.kt | 59 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 24 ++++++++ .../IrBlackBoxCodegenTestGenerated.java | 24 ++++++++ .../LightAnalysisModeTestGenerated.java | 20 +++++++ 9 files changed, 338 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_after.kt create mode 100644 compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_before.kt create mode 100644 compiler/testData/codegen/box/reflection/typeOf/rawTypes_after.kt create mode 100644 compiler/testData/codegen/box/reflection/typeOf/rawTypes_before.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index bfc1692b4c7..bc35197ac3f 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -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 { diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/jvmSpecificFlexibleTypes.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/jvmSpecificFlexibleTypes.kt index 43fd328fce1..abcfddaa7f3 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/jvmSpecificFlexibleTypes.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/jvmSpecificFlexibleTypes.kt @@ -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) } diff --git a/compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_after.kt b/compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_after.kt new file mode 100644 index 00000000000..a2fbf880423 --- /dev/null +++ b/compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_after.kt @@ -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 + +fun box(): String { + check("test.C", returnTypeOf { J.raw() }) + check("test.C", returnTypeOf { J.rawNotNull() }) + check("java.util.List", returnTypeOf { J.rawList() }) + check("java.util.Map", returnTypeOf { J.rawNotNullMap() }) + + return "OK" +} + +inline fun returnTypeOf(block: () -> T) = + typeOf() + +// 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; + } +} diff --git a/compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_before.kt b/compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_before.kt new file mode 100644 index 00000000000..ebcc6984eb7 --- /dev/null +++ b/compiler/testData/codegen/box/reflection/typeOf/noReflect/rawTypes_before.kt @@ -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 + +fun box(): String { + check("test.C", returnTypeOf { J.raw() }) + check("test.C", returnTypeOf { J.rawNotNull() }) + check("java.util.List", returnTypeOf { J.rawList() }) + check("java.util.Map", returnTypeOf { J.rawNotNullMap() }) + + return "OK" +} + +inline fun returnTypeOf(block: () -> T) = + typeOf() + +// 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; + } +} diff --git a/compiler/testData/codegen/box/reflection/typeOf/rawTypes_after.kt b/compiler/testData/codegen/box/reflection/typeOf/rawTypes_after.kt new file mode 100644 index 00000000000..0f07d900f86 --- /dev/null +++ b/compiler/testData/codegen/box/reflection/typeOf/rawTypes_after.kt @@ -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 + +fun box(): String { + val v1 = returnTypeOf { J.raw() }.toString() + if (v1 != "(test.C..test.C<*, *>?)") return "Fail 1: $v1" + + val v2 = returnTypeOf { J.rawNotNull() }.toString() + if (v2 != "(test.C..test.C<*, *>)") return "Fail 2: $v2" + + val v3 = returnTypeOf { J.rawList() }.toString() + if (v3 != "(kotlin.collections.MutableList..kotlin.collections.List<*>?)") return "Fail 3: $v3" + + val v4 = returnTypeOf { J.rawNotNullMap() }.toString() + if (v4 != "(kotlin.collections.MutableMap..kotlin.collections.Map<*, *>)") return "Fail 4: $v4" + + return "OK" +} + +inline fun returnTypeOf(block: () -> T) = + typeOf() + +// 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; + } +} diff --git a/compiler/testData/codegen/box/reflection/typeOf/rawTypes_before.kt b/compiler/testData/codegen/box/reflection/typeOf/rawTypes_before.kt new file mode 100644 index 00000000000..2dcd81205d8 --- /dev/null +++ b/compiler/testData/codegen/box/reflection/typeOf/rawTypes_before.kt @@ -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 + +fun box(): String { + val v1 = returnTypeOf { J.raw() }.toString() + if (v1 != "test.C") return "Fail 1: $v1" + + val v2 = returnTypeOf { J.rawNotNull() }.toString() + if (v2 != "test.C") return "Fail 2: $v2" + + val v3 = returnTypeOf { J.rawList() }.toString() + if (v3 != "kotlin.collections.List") return "Fail 3: $v3" + + val v4 = returnTypeOf { J.rawNotNullMap() }.toString() + if (v4 != "kotlin.collections.Map") return "Fail 4: $v4" + + return "OK" +} + +inline fun returnTypeOf(block: () -> T) = + typeOf() + +// 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; + } +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index 30082fe2d82..cb4f9f2b2ba 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -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 { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index dd1e40f2478..a0c389e3039 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -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 { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index bcbed236ba0..218350614bb 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -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");