JS: fix reified T::class for primitive T (e.g. Int) (KT-32215 fixed)

This commit is contained in:
Anton Bannykh
2019-06-27 17:28:41 +03:00
parent d972d50296
commit fadeac083f
17 changed files with 163 additions and 12 deletions
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JS
// WITH_RUNTIME
// WITH_REFLECT
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JS
// WITH_RUNTIME
// WITH_REFLECT
@@ -1,4 +1,4 @@
// IGNORE_BACKEND: JS, NATIVE
// IGNORE_BACKEND: NATIVE
// IGNORE_BACKEND: JVM_IR
// WITH_REFLECT
// WITH_COROUTINES
@@ -127,6 +127,8 @@ var JsFunction.coroutineMetadata: CoroutineMetadata? by MetadataProperty(default
var JsExpression.range: Pair<RangeType, RangeKind>? by MetadataProperty(default = null)
var JsExpression.primitiveKClass: JsExpression? by MetadataProperty(default = null)
data class CoroutineMetadata(
val doResumeName: JsName,
val stateName: JsName,
@@ -163,7 +165,8 @@ enum class SpecialFunction(val suggestedName: String) {
COROUTINE_RESULT("coroutineResult"),
COROUTINE_CONTROLLER("coroutineController"),
COROUTINE_RECEIVER("coroutineReceiver"),
SET_COROUTINE_RESULT("setCoroutineResult")
SET_COROUTINE_RESULT("setCoroutineResult"),
GET_KCLASS("getKClass")
}
enum class BoxingKind {
@@ -8,10 +8,7 @@ package org.jetbrains.kotlin.js.inline
import org.jetbrains.kotlin.js.backend.ast.*
import org.jetbrains.kotlin.js.backend.ast.metadata.localAlias
import org.jetbrains.kotlin.js.backend.ast.metadata.staticRef
import org.jetbrains.kotlin.js.inline.clean.removeUnusedFunctionDefinitions
import org.jetbrains.kotlin.js.inline.clean.removeUnusedImports
import org.jetbrains.kotlin.js.inline.clean.renameLabels
import org.jetbrains.kotlin.js.inline.clean.simplifyWrappedFunctions
import org.jetbrains.kotlin.js.inline.clean.*
import org.jetbrains.kotlin.js.inline.util.*
import org.jetbrains.kotlin.js.translate.declaration.transformSpecialFunctionsToCoroutineMetadata
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils
@@ -232,6 +229,7 @@ class ImportIntoFragmentInliningScope private constructor(
InlineSuspendFunctionSplitter(this).accept(allCode)
simplifyWrappedFunctions(allCode)
emergePrimitiveKClass(allCode)
removeUnusedFunctionDefinitions(allCode, collectNamedFunctions(allCode))
removeUnusedImports(fragment, allCode)
renameLabels(allCode)
@@ -0,0 +1,27 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.js.inline.clean
import org.jetbrains.kotlin.js.backend.ast.*
import org.jetbrains.kotlin.js.backend.ast.metadata.SpecialFunction
import org.jetbrains.kotlin.js.backend.ast.metadata.primitiveKClass
import org.jetbrains.kotlin.js.backend.ast.metadata.specialFunction
// Replaces getKClass(<Primive class constructor>) with PrimitiveClasses.<primitive class KClass>
fun emergePrimitiveKClass(root: JsNode) {
val visitor = object : JsVisitorWithContextImpl() {
override fun endVisit(invocation: JsInvocation, ctx: JsContext<in JsNode>) {
val qualifier = invocation.qualifier as? JsNameRef ?: return
if (qualifier.name?.specialFunction != SpecialFunction.GET_KCLASS) return
val firstArg = invocation.arguments.firstOrNull() as? JsNameRef ?: return
firstArg.primitiveKClass?.let {
ctx.replaceMe(it)
}
}
}
visitor.accept(root)
}
+1
View File
@@ -464,6 +464,7 @@ enum SpecialFunction {
COROUTINE_CONTROLLER = 7;
COROUTINE_RECEIVER = 8;
SET_COROUTINE_RESULT = 9;
GET_KCLASS = 10;
}
@@ -557,6 +557,7 @@ class JsAstDeserializer(program: JsProgram, private val sourceRoots: Iterable<Fi
JsAstProtoBuf.SpecialFunction.COROUTINE_CONTROLLER -> SpecialFunction.COROUTINE_CONTROLLER
JsAstProtoBuf.SpecialFunction.COROUTINE_RECEIVER -> SpecialFunction.COROUTINE_RECEIVER
JsAstProtoBuf.SpecialFunction.SET_COROUTINE_RESULT -> SpecialFunction.SET_COROUTINE_RESULT
JsAstProtoBuf.SpecialFunction.GET_KCLASS -> SpecialFunction.GET_KCLASS
}
private fun <T : JsNode> withLocation(fileId: Int?, location: Location?, action: () -> T): T {
@@ -179,6 +179,10 @@ public final class JsAstProtoBuf {
* <code>SET_COROUTINE_RESULT = 9;</code>
*/
SET_COROUTINE_RESULT(8, 9),
/**
* <code>GET_KCLASS = 10;</code>
*/
GET_KCLASS(9, 10),
;
/**
@@ -217,6 +221,10 @@ public final class JsAstProtoBuf {
* <code>SET_COROUTINE_RESULT = 9;</code>
*/
public static final int SET_COROUTINE_RESULT_VALUE = 9;
/**
* <code>GET_KCLASS = 10;</code>
*/
public static final int GET_KCLASS_VALUE = 10;
public final int getNumber() { return value; }
@@ -232,6 +240,7 @@ public final class JsAstProtoBuf {
case 7: return COROUTINE_CONTROLLER;
case 8: return COROUTINE_RECEIVER;
case 9: return SET_COROUTINE_RESULT;
case 10: return GET_KCLASS;
default: return null;
}
}
@@ -599,6 +599,7 @@ class JsAstSerializer(private val jsAstValidator: ((JsProgramFragment, Set<JsNam
SpecialFunction.COROUTINE_CONTROLLER -> JsAstProtoBuf.SpecialFunction.COROUTINE_CONTROLLER
SpecialFunction.COROUTINE_RECEIVER -> JsAstProtoBuf.SpecialFunction.COROUTINE_RECEIVER
SpecialFunction.SET_COROUTINE_RESULT -> JsAstProtoBuf.SpecialFunction.SET_COROUTINE_RESULT
SpecialFunction.GET_KCLASS -> JsAstProtoBuf.SpecialFunction.GET_KCLASS
}
private fun serialize(name: JsName): Int = nameMap.getOrPut(name) {
@@ -6703,6 +6703,11 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
runTest("js/js.translator/testData/box/reflection/kClassOnReifiedTypeInLambda.kt");
}
@TestMetadata("kClassOnReifiedTypeInLambda-advanced.kt")
public void testKClassOnReifiedTypeInLambda_advanced() throws Exception {
runTest("js/js.translator/testData/box/reflection/kClassOnReifiedTypeInLambda-advanced.kt");
}
@TestMetadata("kClassSimpleName.kt")
public void testKClassSimpleName() throws Exception {
runTest("js/js.translator/testData/box/reflection/kClassSimpleName.kt");
@@ -6713,6 +6718,11 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
runTest("js/js.translator/testData/box/reflection/kClassToAndFromJsClass.kt");
}
@TestMetadata("primitiveKClassOnReifiedType.kt")
public void testPrimitiveKClassOnReifiedType() throws Exception {
runTest("js/js.translator/testData/box/reflection/primitiveKClassOnReifiedType.kt");
}
@TestMetadata("primitives.kt")
public void testPrimitives() throws Exception {
runTest("js/js.translator/testData/box/reflection/primitives.kt");
@@ -6738,6 +6738,11 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
runTest("js/js.translator/testData/box/reflection/kClassOnReifiedTypeInLambda.kt");
}
@TestMetadata("kClassOnReifiedTypeInLambda-advanced.kt")
public void testKClassOnReifiedTypeInLambda_advanced() throws Exception {
runTest("js/js.translator/testData/box/reflection/kClassOnReifiedTypeInLambda-advanced.kt");
}
@TestMetadata("kClassSimpleName.kt")
public void testKClassSimpleName() throws Exception {
runTest("js/js.translator/testData/box/reflection/kClassSimpleName.kt");
@@ -6748,6 +6753,11 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
runTest("js/js.translator/testData/box/reflection/kClassToAndFromJsClass.kt");
}
@TestMetadata("primitiveKClassOnReifiedType.kt")
public void testPrimitiveKClassOnReifiedType() throws Exception {
runTest("js/js.translator/testData/box/reflection/primitiveKClassOnReifiedType.kt");
}
@TestMetadata("primitives.kt")
public void testPrimitives() throws Exception {
runTest("js/js.translator/testData/box/reflection/primitives.kt");
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.kotlin.descriptors.annotations.KotlinRetention;
import org.jetbrains.kotlin.js.backend.ast.*;
import org.jetbrains.kotlin.js.backend.ast.metadata.MetadataProperties;
import org.jetbrains.kotlin.js.backend.ast.metadata.SpecialFunction;
import org.jetbrains.kotlin.js.naming.NameSuggestion;
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
import org.jetbrains.kotlin.js.translate.declaration.ClassTranslator;
@@ -288,11 +289,11 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
public static JsExpression getObjectKClass(@NotNull TranslationContext context, @Nullable ClassifierDescriptor descriptor) {
JsExpression primitiveExpression = getPrimitiveClass(context, descriptor);
if (primitiveExpression != null) return primitiveExpression;
return new JsInvocation(context.getReferenceToIntrinsic(GET_KCLASS), UtilsKt.getReferenceToJsClass(descriptor, context));
return new JsInvocation(context.getNameForSpecialFunction(SpecialFunction.GET_KCLASS).makeRef(), UtilsKt.getReferenceToJsClass(descriptor, context));
}
@Nullable
private static JsExpression getPrimitiveClass(@NotNull TranslationContext context, @Nullable ClassifierDescriptor classifierDescriptor) {
public static JsExpression getPrimitiveClass(@NotNull TranslationContext context, @Nullable ClassifierDescriptor classifierDescriptor) {
if (!context.getConfig().isAtLeast(LanguageVersion.KOTLIN_1_2) || findPrimitiveClassesObject(context) == null) return null;
if (!(classifierDescriptor instanceof ClassDescriptor)) return null;
ClassDescriptor descriptor = (ClassDescriptor) classifierDescriptor;
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.js.backend.ast.metadata.*
import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator
import org.jetbrains.kotlin.js.translate.context.Namer
import org.jetbrains.kotlin.js.translate.context.TranslationContext
import org.jetbrains.kotlin.js.translate.expression.ExpressionVisitor
import org.jetbrains.kotlin.js.translate.intrinsic.functions.basic.FunctionIntrinsicWithReceiverComputed
import org.jetbrains.kotlin.js.translate.reference.ReferenceTranslator
import org.jetbrains.kotlin.js.translate.utils.TranslationUtils.simpleReturnFunction
@@ -141,6 +142,8 @@ fun getReferenceToJsClass(classifierDescriptor: ClassifierDescriptor?, context:
else -> {
throw IllegalStateException("Can't get reference for $classifierDescriptor")
}
}.also {
it.primitiveKClass = ExpressionVisitor.getPrimitiveClass(context, classifierDescriptor)
}
}
@@ -1,4 +1,8 @@
// EXPECTED_REACHABLE_NODES: 1318
// FILE: lib.kt
// RECOMPILE
package foo
import kotlin.reflect.KClass
@@ -10,11 +14,17 @@ inline fun <reified T : Any> foo(b: Boolean = false): KClass<T> {
return T::class
}
// FILE: main.kt
package foo
fun box(): String {
check(A::class, foo<A>())
check(B::class, foo<B>())
check(O::class, foo<O>())
check(E::class, foo<E>())
check(Int::class, foo<Int>())
check(ByteArray::class, foo<ByteArray>())
return "OK"
}
@@ -0,0 +1,30 @@
// EXPECTED_REACHABLE_NODES: 1320
// IGNORE_BACKEND: JS
// FILE: lib.kt
// RECOMPILE
package foo
import kotlin.reflect.KClass
inline fun <reified T : Any> foo(b: Boolean = false): () -> KClass<T> {
if (b) {
val T = 1
}
return { T::class }
}
// FILE: main.kt
package foo
fun box(): String {
check(A::class, foo<A>()())
check(B::class, foo<B>()())
check(O::class, foo<O>()())
check(E::class, foo<E>()())
check(Int::class, foo<Int>()())
check(ByteArray::class, foo<ByteArray>()())
return "OK"
}
@@ -0,0 +1,51 @@
// EXPECTED_REACHABLE_NODES: 1321
// MODULE: lib
// FILE: lib.kt
inline fun <reified T: Any> klassLib() = T::class
// MODULE: main(lib)
// FILE: main.kt
import kotlin.reflect.KClass
inline fun <reified T: Any> klass() = T::class
fun box(): String {
check(js("Object"), "Any", klass<Any>())
check(js("String"), "String", klass<String>())
check(js("Boolean"), "Boolean", klass<Boolean>())
check(js("Error"), "Throwable", klass<Throwable>())
check(js("Array"), "Array", klass<Array<Any>>())
check(js("Function"), "Function0", klass<Function0<*>>())
check(js("Function"), "Function1", klass<Function1<*, *>>())
check(js("Number"), "Byte", klass<Byte>())
check(js("Number"), "Short", klass<Short>())
check(js("Number"), "Int", klass<Int>())
check(js("Number"), "Float", klass<Float>())
check(js("Number"), "Double", klass<Double>())
check(js("Number"), "Number", klass<Number>())
check(js("Array"), "BooleanArray", klass<BooleanArray>())
check(js("Uint16Array"), "CharArray", klass<CharArray>())
check(js("Int8Array"), "ByteArray", klass<ByteArray>())
check(js("Int16Array"), "ShortArray", klass<ShortArray>())
check(js("Int32Array"), "IntArray", klass<IntArray>())
check(js("Array"), "LongArray", klass<LongArray>())
check(js("Float32Array"), "FloatArray", klass<FloatArray>())
check(js("Float64Array"), "DoubleArray", klass<DoubleArray>())
// Check same instance
if (Int::class !== klass<Int>()) return "Same instance check failed"
// Check inlining from other module works
check(js("Int8Array"), "ByteArray", klassLib<ByteArray>())
return "OK"
}
private fun check(nativeClass: dynamic, simpleName: String, c: KClass<*>) {
assertEquals(simpleName, c.simpleName, "Simple name of class has unexpected value")
assertEquals(nativeClass, c.js, "Kotlin class does not correspond native class ${nativeClass.name}")
}