From c34d2f9daac0ae4b9b5f7b8d40f5161e5c89118d Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 6 Feb 2020 17:41:36 +0300 Subject: [PATCH] [FIR] Fix signature calculation in JvmMappedScope --- .../kotlin/fir/scopes/jvm/DescriptorUtils.kt | 9 +++++++-- .../kotlin/fir/scopes/jvm/JvmMappedScope.kt | 2 ++ .../testData/resolve/stdlib/j+k/MapCompute.kt | 4 ++-- .../testData/resolve/stdlib/j+k/MapCompute.txt | 8 ++++---- .../testData/resolve/stdlib/j+k/MyException.kt | 6 +++--- .../testData/resolve/stdlib/j+k/MyException.txt | 12 ++++++------ .../fir/resolve/testData/resolve/stdlib/j+k/MyMap.kt | 4 ++-- .../resolve/testData/resolve/stdlib/j+k/MyMap.txt | 4 ++-- .../tests/targetedBuiltIns/getOrDefault.fir.kt | 4 ++-- 9 files changed, 30 insertions(+), 23 deletions(-) diff --git a/compiler/fir/jvm/src/org/jetbrains/kotlin/fir/scopes/jvm/DescriptorUtils.kt b/compiler/fir/jvm/src/org/jetbrains/kotlin/fir/scopes/jvm/DescriptorUtils.kt index a3cbe0960ab..0356f9cdc1b 100644 --- a/compiler/fir/jvm/src/org/jetbrains/kotlin/fir/scopes/jvm/DescriptorUtils.kt +++ b/compiler/fir/jvm/src/org/jetbrains/kotlin/fir/scopes/jvm/DescriptorUtils.kt @@ -8,6 +8,8 @@ package org.jetbrains.kotlin.fir.scopes.jvm import org.jetbrains.kotlin.fir.declarations.FirFunction import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction import org.jetbrains.kotlin.fir.types.* +import org.jetbrains.kotlin.fir.types.impl.FirImplicitAnyTypeRef +import org.jetbrains.kotlin.fir.types.impl.FirImplicitNullableAnyTypeRef import org.jetbrains.kotlin.fir.types.jvm.FirJavaTypeRef import org.jetbrains.kotlin.load.java.structure.JavaClass import org.jetbrains.kotlin.load.java.structure.JavaClassifierType @@ -87,12 +89,15 @@ private fun StringBuilder.appendConeType(coneType: ConeKotlinType) { val representative = coneType.lookupTag.typeParameterSymbol.fir.bounds.firstOrNull { (it as? FirResolvedTypeRef)?.type is ConeClassLikeType } - if (representative == null) { + if (representative == null || representative is FirImplicitNullableAnyTypeRef || representative is FirImplicitAnyTypeRef) { append("Ljava/lang/Object") } else { appendClassLikeType(representative.coneTypeUnsafe()) } - append(coneType.lookupTag.name) + } + is ConeFlexibleType -> { + appendConeType(coneType.lowerBound) + return } } append(";") diff --git a/compiler/fir/jvm/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt b/compiler/fir/jvm/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt index e1412094e42..f29ff210a99 100644 --- a/compiler/fir/jvm/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt +++ b/compiler/fir/jvm/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt @@ -24,6 +24,8 @@ class JvmMappedScope( ?: return declaredMemberScope.processFunctionsByName(name, processor) javaMappedClassUseSiteScope.processFunctionsByName(name) { symbol -> val jvmSignature = symbol.fir.computeJvmDescriptor() + .replace("kotlin/Any", "java/lang/Object") + .replace("kotlin/String", "java/lang/String") if (jvmSignature in whiteListSignatures) { processor(symbol) } diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MapCompute.kt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MapCompute.kt index 14ba2b3c11f..b6a7b3ff044 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MapCompute.kt +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MapCompute.kt @@ -3,9 +3,9 @@ // !JVM_TARGET: 1.8 fun MutableMap>.initAndAdd(key: String, value: D) { - this.compute(key) { _, maybeValues -> + this.compute(key) { _, maybeValues -> val setOfValues = maybeValues ?: mutableSetOf() - setOfValues.add(value) + setOfValues.add(value) setOfValues } } \ No newline at end of file diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MapCompute.txt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MapCompute.txt index a7cc7ba0a84..2afe1118d4a 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MapCompute.txt +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MapCompute.txt @@ -1,16 +1,16 @@ FILE: MapCompute.kt public final fun R|kotlin/collections/MutableMap>|.initAndAdd(key: R|kotlin/String|, value: R|D|): R|kotlin/Unit| { - this@R|/initAndAdd|.#(R|/key|, = compute@fun (_: R|ERROR CLASS: No type for parameter|, maybeValues: R|ERROR CLASS: No type for parameter|): R|ERROR CLASS: Unresolved: | { - lval setOfValues: R|ERROR CLASS: Unresolved: | = when (lval : R|ERROR CLASS: No type for parameter| = R|/maybeValues|) { + this@R|/initAndAdd|.R|FakeOverride?|>|(R|/key|, = compute@fun (_: R|kotlin/String|, maybeValues: R|kotlin/collections/MutableSet|): R|kotlin/collections/MutableSet| { + lval setOfValues: R|kotlin/collections/MutableSet| = when (lval : R|kotlin/collections/MutableSet| = R|/maybeValues|) { ==($subj$, Null(null)) -> { - R|kotlin/collections/mutableSetOf|() + R|kotlin/collections/mutableSetOf|() } else -> { R|/| } } - R|/setOfValues|.#(R|/value|) + R|/setOfValues|.R|FakeOverride|(R|/value|) ^ R|/setOfValues| } ) diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyException.kt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyException.kt index 140e3858a0b..7a3c32ff4a0 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyException.kt +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyException.kt @@ -12,17 +12,17 @@ class MyException : Exception() fun test(e: MyException, stream: PrintStream) { e.printStackTrace() // Cannot be resolved with early J2K mapping due deriving of kotlin.Throwable instead of java.lang.Throwable e.printStackTrace(stream) - val result = e.getLocalizedMessage() + val result = e.getLocalizedMessage() } fun test(e: YourException, stream: PrintStream) { e.printStackTrace() e.printStackTrace(stream) - val result = e.getLocalizedMessage() + val result = e.getLocalizedMessage() } fun test(e: Exception, stream: PrintStream) { e.printStackTrace() e.printStackTrace(stream) - val result = e.getLocalizedMessage() + val result = e.getLocalizedMessage() } \ No newline at end of file diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyException.txt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyException.txt index 8aafa5e5843..9851d0be53c 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyException.txt +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyException.txt @@ -7,16 +7,16 @@ FILE: test.kt } public final fun test(e: R|MyException|, stream: R|java/io/PrintStream|): R|kotlin/Unit| { R|/e|.R|java/lang/Throwable.printStackTrace|() - R|/e|.R|kotlin/printStackTrace|(R|/stream|) - lval result: = R|/e|.#() + R|/e|.R|java/lang/Throwable.printStackTrace|(R|/stream|) + lval result: R|ft!| = R|/e|.R|java/lang/Throwable.getLocalizedMessage|() } public final fun test(e: R|YourException|, stream: R|java/io/PrintStream|): R|kotlin/Unit| { R|/e|.R|java/lang/Throwable.printStackTrace|() - R|/e|.R|kotlin/printStackTrace|(R|/stream|) - lval result: = R|/e|.#() + R|/e|.R|java/lang/Throwable.printStackTrace|(R|/stream|) + lval result: R|ft!| = R|/e|.R|java/lang/Throwable.getLocalizedMessage|() } public final fun test(e: R|kotlin/Exception|, stream: R|java/io/PrintStream|): R|kotlin/Unit| { R|/e|.R|java/lang/Throwable.printStackTrace|() - R|/e|.R|kotlin/printStackTrace|(R|/stream|) - lval result: = R|/e|.#() + R|/e|.R|java/lang/Throwable.printStackTrace|(R|/stream|) + lval result: R|ft!| = R|/e|.R|java/lang/Throwable.getLocalizedMessage|() } diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.kt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.kt index ee8804673bd..440307eb702 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.kt +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.kt @@ -8,7 +8,7 @@ fun test(map: MyMap) { val result = map.getOrPut("key") { "value" } // Cannot be resolved without early J2K mapping // In contrast, should be taken from JDK val otherResult = map.getOrDefault("key", "value") - val anotherResult = map.replace("key", "value") + val anotherResult = map.replace("key", "value") // Java forEach map.forEach { key, value -> println("$key: $value") @@ -27,7 +27,7 @@ fun test(map: MutableMap) { val result = map.getOrPut("key") { "value" } // Cannot be resolved without early J2K mapping // In contrast, should be taken from JDK val otherResult = map.getOrDefault("key", "value") - val anotherResult = map.replace("key", "value") + val anotherResult = map.replace("key", "value") // Java forEach map.forEach { key, value -> println("$key: $value") diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.txt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.txt index d02f6810fe4..e6008bb7b71 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.txt +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.txt @@ -5,7 +5,7 @@ FILE: test.kt } ) lval otherResult: R|ft!| = R|/map|.R|FakeOverride!|>|(String(key), String(value)) - lval anotherResult: = R|/map|.#(String(key), String(value)) + lval anotherResult: R|kotlin/String?| = R|/map|.R|FakeOverride|(String(key), String(value)) R|/map|.R|FakeOverride|( = forEach@fun (key: R|ft!|, value: R|ft!|): R|kotlin/Unit| { R|kotlin/io/println|((R|/key|.R|kotlin/Any.toString|(), String(: ), R|/value|.R|kotlin/Any.toString|())) R|/key|.R|kotlin/String.length| @@ -27,7 +27,7 @@ FILE: test.kt } ) lval otherResult: R|kotlin/String| = R|/map|.R|FakeOverride|(String(key), String(value)) - lval anotherResult: = R|/map|.#(String(key), String(value)) + lval anotherResult: R|kotlin/String?| = R|/map|.R|FakeOverride|(String(key), String(value)) R|/map|.R|FakeOverride|( = forEach@fun (key: R|kotlin/String|, value: R|kotlin/String|): R|kotlin/Unit| { R|kotlin/io/println|((R|/key|.R|kotlin/Any.toString|(), String(: ), R|/value|.R|kotlin/Any.toString|())) R|/key|.R|kotlin/String.length| diff --git a/compiler/testData/diagnostics/tests/targetedBuiltIns/getOrDefault.fir.kt b/compiler/testData/diagnostics/tests/targetedBuiltIns/getOrDefault.fir.kt index bdc7e807df9..0d2469cd512 100644 --- a/compiler/testData/diagnostics/tests/targetedBuiltIns/getOrDefault.fir.kt +++ b/compiler/testData/diagnostics/tests/targetedBuiltIns/getOrDefault.fir.kt @@ -4,12 +4,12 @@ abstract class A : Map fun foo(x: Map, a: A, b: java.util.HashMap) { x.getOrDefault(1, "") - x.getOrDefault("", "") + x.getOrDefault("", "") x.getOrDefault(1, 2) x.getOrDefault("", 2) a.getOrDefault(1, "") - a.getOrDefault("", "") + a.getOrDefault("", "") a.getOrDefault(1, 2) a.getOrDefault("", 2)