From ca37c6bfe65022673d0ee1b22c77628ecae1995f Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Wed, 8 Jul 2020 16:20:23 +0300 Subject: [PATCH] [KJS FE] Allow using typeOf with non-reified type parameters #KT-38771 fixed --- .../defaultUpperBound.kt | 13 +++++-- .../equalsOnClassParameters.kt | 11 ++++-- .../equalsOnFunctionParameters.kt | 10 ++++-- .../nonReifiedTypeParameters/innerGeneric.kt | 4 +-- .../simpleClassParameter.kt | 14 +++++--- .../simpleFunctionParameter.kt | 14 +++++--- .../simplePropertyParameter.kt | 14 +++++--- .../typeParameterFlags.kt | 26 ++++++++------ .../upperBoundUsesOuterClassParameter.kt | 4 +-- .../nonReifiedTypeParameters/upperBounds.kt | 19 +++++++--- .../reifiedTypeArgumentWithRecursion.kt | 2 +- .../typeOfWithNonReifiedParameter.kt | 35 ------------------- .../typeOfWithNonReifiedParameter.txt | 13 ------- .../DiagnosticsTestWithJsStdLibGenerated.java | 5 --- .../js/resolve/JsPlatformConfigurator.kt | 17 ++------- 15 files changed, 92 insertions(+), 109 deletions(-) delete mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/reflection/typeOfWithNonReifiedParameter.kt delete mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/reflection/typeOfWithNonReifiedParameter.txt diff --git a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/defaultUpperBound.kt b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/defaultUpperBound.kt index b9cd2f99b41..42a8b3ee6e8 100644 --- a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/defaultUpperBound.kt +++ b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/defaultUpperBound.kt @@ -1,7 +1,7 @@ // !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi -// IGNORE_BACKEND: JS, JS_IR, NATIVE -// IGNORE_BACKEND: JS_IR_ES6 +// IGNORE_BACKEND: NATIVE // WITH_REFLECT +// KJS_WITH_FULL_RUNTIME package test @@ -16,7 +16,14 @@ fun test() = typeOf>() fun box(): String { val type = test() val x = type.arguments.single().type!!.classifier as KTypeParameter - assertEquals("kotlin.Any?", x.upperBounds.joinToString()) + + val expected = className("kotlin", "Any?") + assertEquals(expected, x.upperBounds.joinToString()) return "OK" } + +fun className(qualifier: String, name: String): String { + val isJS = 1 as Any is Double + return if (isJS) name else "$qualifier.$name" +} diff --git a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/equalsOnClassParameters.kt b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/equalsOnClassParameters.kt index cdf9fa3f787..495b9a0989b 100644 --- a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/equalsOnClassParameters.kt +++ b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/equalsOnClassParameters.kt @@ -1,7 +1,7 @@ // !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi -// IGNORE_BACKEND: JS, JS_IR, NATIVE -// IGNORE_BACKEND: JS_IR_ES6 +// IGNORE_BACKEND: NATIVE // WITH_REFLECT +// KJS_WITH_FULL_RUNTIME package test @@ -36,7 +36,12 @@ fun box(): String { assertEquals(c.x1, c.x2) assertEquals(c.x1.hashCode(), c.x2.hashCode()) - assertNotEquals(c.x1, c.xFun) + + if (!isJS) { + assertNotEquals(c.x1, c.xFun) + } assertNotEquals(c.x1, c.y) return "OK" } + +val isJS = 1 as Any is Double diff --git a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/equalsOnFunctionParameters.kt b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/equalsOnFunctionParameters.kt index b3b656ace42..7f6651b432b 100644 --- a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/equalsOnFunctionParameters.kt +++ b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/equalsOnFunctionParameters.kt @@ -1,7 +1,7 @@ // !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi -// IGNORE_BACKEND: JS, JS_IR, NATIVE -// IGNORE_BACKEND: JS_IR_ES6 +// IGNORE_BACKEND: NATIVE // WITH_REFLECT +// KJS_WITH_FULL_RUNTIME package test @@ -22,6 +22,10 @@ fun box(): String { assertEquals(createX(), createX()) assertEquals(createX().hashCode(), createX().hashCode()) - assertNotEquals(createX(), createOtherX()) + if (!isJS) { + assertNotEquals(createX(), createOtherX()) + } return "OK" } + +val isJS = 1 as Any is Double diff --git a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/innerGeneric.kt b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/innerGeneric.kt index 1a4e21e69a8..f1f51ee83d3 100644 --- a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/innerGeneric.kt +++ b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/innerGeneric.kt @@ -1,7 +1,7 @@ // !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi -// IGNORE_BACKEND: JS, JS_IR, NATIVE -// IGNORE_BACKEND: JS_IR_ES6 +// IGNORE_BACKEND: NATIVE // WITH_REFLECT +// KJS_WITH_FULL_RUNTIME package test diff --git a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/simpleClassParameter.kt b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/simpleClassParameter.kt index 39100cfae8b..d535a38f1db 100644 --- a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/simpleClassParameter.kt +++ b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/simpleClassParameter.kt @@ -1,7 +1,7 @@ // !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi -// IGNORE_BACKEND: JS, JS_IR, NATIVE -// IGNORE_BACKEND: JS_IR_ES6 +// IGNORE_BACKEND: NATIVE // WITH_REFLECT +// KJS_WITH_FULL_RUNTIME package test @@ -16,7 +16,13 @@ class C { } fun box(): String { - assertEquals("test.Container", C().notNull().toString()) - assertEquals("test.Container", C().nullable().toString()) + val fqn = className("test", "Container") + assertEquals("$fqn", C().notNull().toString()) + assertEquals("$fqn", C().nullable().toString()) return "OK" } + +fun className(qualifier: String, name: String): String { + val isJS = 1 as Any is Double + return if (isJS) name else "$qualifier.$name" +} diff --git a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/simpleFunctionParameter.kt b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/simpleFunctionParameter.kt index 4d6a2756288..b55d1dc1125 100644 --- a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/simpleFunctionParameter.kt +++ b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/simpleFunctionParameter.kt @@ -1,7 +1,7 @@ // !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi -// IGNORE_BACKEND: JS, JS_IR, NATIVE -// IGNORE_BACKEND: JS_IR_ES6 +// IGNORE_BACKEND: NATIVE // WITH_REFLECT +// KJS_WITH_FULL_RUNTIME package test @@ -14,7 +14,13 @@ fun notNull() = typeOf>() fun nullable() = typeOf>() fun box(): String { - assertEquals("test.Container", notNull().toString()) - assertEquals("test.Container", nullable().toString()) + val fqn = className("test", "Container") + assertEquals("$fqn", notNull().toString()) + assertEquals("$fqn", nullable().toString()) return "OK" } + +fun className(qualifier: String, name: String): String { + val isJS = 1 as Any is Double + return if (isJS) name else "$qualifier.$name" +} diff --git a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/simplePropertyParameter.kt b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/simplePropertyParameter.kt index 8dad8f5b0aa..453372356d1 100644 --- a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/simplePropertyParameter.kt +++ b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/simplePropertyParameter.kt @@ -1,7 +1,7 @@ // !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi -// IGNORE_BACKEND: JS, JS_IR, NATIVE -// IGNORE_BACKEND: JS_IR_ES6 +// IGNORE_BACKEND: NATIVE // WITH_REFLECT +// KJS_WITH_FULL_RUNTIME package test @@ -14,7 +14,13 @@ val X1.notNull get() = typeOf>() val X2.nullable get() = typeOf>() fun box(): String { - assertEquals("test.Container", "".notNull.toString()) - assertEquals("test.Container", "".nullable.toString()) + val fqn = className("test", "Container") + assertEquals("$fqn", "".notNull.toString()) + assertEquals("$fqn", "".nullable.toString()) return "OK" } + +fun className(qualifier: String, name: String): String { + val isJS = 1 as Any is Double + return if (isJS) name else "$qualifier.$name" +} diff --git a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/typeParameterFlags.kt b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/typeParameterFlags.kt index ccedd962668..64bc818c4ae 100644 --- a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/typeParameterFlags.kt +++ b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/typeParameterFlags.kt @@ -1,7 +1,7 @@ // !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi -// IGNORE_BACKEND: JS, JS_IR, NATIVE -// IGNORE_BACKEND: JS_IR_ES6 +// IGNORE_BACKEND: NATIVE // WITH_REFLECT +// KJS_WITH_FULL_RUNTIME package test @@ -25,16 +25,22 @@ fun box(): String { assertEquals(KVariance.OUT, c.getOut().variance) assertEquals(false, c.getInv().isReified) - val y = getY() - assertEquals(false, y.isReified) - val x = y.upperBounds.single().classifier as KTypeParameter - assertEquals(true, x.isReified) - assertEquals(KVariance.INVARIANT, x.variance) + if (!isJS) { + val y = getY() + assertEquals(false, y.isReified) + val x = y.upperBounds.single().classifier as KTypeParameter + assertEquals(true, x.isReified) + assertEquals(KVariance.INVARIANT, x.variance) + assertEquals("X", x.toString()) + } assertEquals("INV", c.getInv().toString()) - assertEquals("in IN", c.getIn().toString()) - assertEquals("out OUT", c.getOut().toString()) - assertEquals("X", x.toString()) + if (!isJS) { + assertEquals("in IN", c.getIn().toString()) + assertEquals("out OUT", c.getOut().toString()) + } return "OK" } + +val isJS = 1 as Any is Double diff --git a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/upperBoundUsesOuterClassParameter.kt b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/upperBoundUsesOuterClassParameter.kt index 38dc1f76ca5..5c9fe365191 100644 --- a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/upperBoundUsesOuterClassParameter.kt +++ b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/upperBoundUsesOuterClassParameter.kt @@ -1,7 +1,7 @@ // !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi -// IGNORE_BACKEND: JS, JS_IR, NATIVE -// IGNORE_BACKEND: JS_IR_ES6 +// IGNORE_BACKEND: NATIVE // WITH_REFLECT +// KJS_WITH_FULL_RUNTIME package test diff --git a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/upperBounds.kt b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/upperBounds.kt index 46cef4c6aa7..d5e75a2e458 100644 --- a/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/upperBounds.kt +++ b/compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/upperBounds.kt @@ -1,7 +1,7 @@ // !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi -// IGNORE_BACKEND: JS, JS_IR, NATIVE -// IGNORE_BACKEND: JS_IR_ES6 +// IGNORE_BACKEND: NATIVE // WITH_REFLECT +// KJS_WITH_FULL_RUNTIME package test @@ -16,16 +16,25 @@ fun test() where X : Y?, Y : List, Z : Set fun box(): String { val type = test>?, MutableList>, Set>() - assertEquals("test.Container", type.toString()) + val containerNmae = className("test", "Container") + assertEquals("$containerNmae", type.toString()) val x = type.arguments.single().type!!.classifier as KTypeParameter assertEquals("Y?", x.upperBounds.joinToString()) val y = x.upperBounds.single().classifier as KTypeParameter - assertEquals("kotlin.collections.List", y.upperBounds.joinToString()) + val listName = className("kotlin.collections", "List") + assertEquals("$listName", y.upperBounds.joinToString()) val z = y.upperBounds.single().arguments.single().type!!.classifier as KTypeParameter - assertEquals("kotlin.collections.Set", z.upperBounds.joinToString()) + val setName = className("kotlin.collections", "Set") + val stringName = className("kotlin", "String") + assertEquals("$setName<$stringName>", z.upperBounds.joinToString()) return "OK" } + +fun className(qualifier: String, name: String): String { + val isJS = 1 as Any is Double + return if (isJS) name else "$qualifier.$name" +} diff --git a/compiler/testData/codegen/box/reified/reifiedTypeArgumentWithRecursion.kt b/compiler/testData/codegen/box/reified/reifiedTypeArgumentWithRecursion.kt index 9e1c64a26fe..8dcff939afe 100644 --- a/compiler/testData/codegen/box/reified/reifiedTypeArgumentWithRecursion.kt +++ b/compiler/testData/codegen/box/reified/reifiedTypeArgumentWithRecursion.kt @@ -16,7 +16,7 @@ inline fun T.causeBug() { T::class Array(1) { x } - // Non-reified type parameters with recursive bounds are not yet supported + // Non-reified type parameters with recursive bounds are not yet supported, see Z from class Something // typeOf() } diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/reflection/typeOfWithNonReifiedParameter.kt b/compiler/testData/diagnostics/testsWithJsStdLib/reflection/typeOfWithNonReifiedParameter.kt deleted file mode 100644 index 2bd33843377..00000000000 --- a/compiler/testData/diagnostics/testsWithJsStdLib/reflection/typeOfWithNonReifiedParameter.kt +++ /dev/null @@ -1,35 +0,0 @@ -// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi -// !LANGUAGE: +ProhibitNonReifiedArraysAsReifiedTypeArguments - -import kotlin.reflect.typeOf - -inline fun test1() { - typeOf<X>() - typeOf>() - typeOf<Array>() - - typeOf() - - typeOf<Z>() - typeOf?>() - typeOf<Array>() -} - - -class Test2 { - fun test2() { - typeOf<W>() - typeOf>() - typeOf<Array>() - } -} - - -inline fun f() { - typeOf() -} - -fun test3() { - // We don't report anything here because we can't know in frontend how the corresponding type parameter is used in f - f>() -} diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/reflection/typeOfWithNonReifiedParameter.txt b/compiler/testData/diagnostics/testsWithJsStdLib/reflection/typeOfWithNonReifiedParameter.txt deleted file mode 100644 index 1fd20e67728..00000000000 --- a/compiler/testData/diagnostics/testsWithJsStdLib/reflection/typeOfWithNonReifiedParameter.txt +++ /dev/null @@ -1,13 +0,0 @@ -package - -public inline fun f(): kotlin.Unit -public inline fun test1(): kotlin.Unit -public fun test3(): kotlin.Unit - -public final class Test2 { - public constructor Test2() - public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int - public final fun test2(): kotlin.Unit - public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String -} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java index ffae5cf73d2..a9836316b29 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java @@ -1122,10 +1122,5 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes public void testReflectionApi() throws Exception { runTest("compiler/testData/diagnostics/testsWithJsStdLib/reflection/reflectionApi.kt"); } - - @TestMetadata("typeOfWithNonReifiedParameter.kt") - public void testTypeOfWithNonReifiedParameter() throws Exception { - runTest("compiler/testData/diagnostics/testsWithJsStdLib/reflection/typeOfWithNonReifiedParameter.kt"); - } } } diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/JsPlatformConfigurator.kt b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/JsPlatformConfigurator.kt index 050122ad9be..f7097f5a3c3 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/JsPlatformConfigurator.kt +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/JsPlatformConfigurator.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2020 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.resolve @@ -23,7 +12,6 @@ import org.jetbrains.kotlin.js.analyze.JsNativeDiagnosticSuppressor import org.jetbrains.kotlin.js.naming.NameSuggestion import org.jetbrains.kotlin.js.resolve.diagnostics.* import org.jetbrains.kotlin.resolve.PlatformConfiguratorBase -import org.jetbrains.kotlin.resolve.calls.checkers.TypeOfChecker import org.jetbrains.kotlin.resolve.checkers.ExpectedActualDeclarationChecker import org.jetbrains.kotlin.resolve.deprecation.CoroutineCompatibilitySupport import org.jetbrains.kotlin.types.DynamicTypesAllowed @@ -43,7 +31,6 @@ object JsPlatformConfigurator : PlatformConfiguratorBase( JsModuleCallChecker, JsDynamicCallChecker, JsDefinedExternallyCallChecker, - TypeOfChecker, ), identifierChecker = JsIdentifierChecker ) {