From 1ebf0f537650db76037b0e5ff10d340ec0e7ead2 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 20 Dec 2023 11:33:08 +0100 Subject: [PATCH] K2: show all Java function with suspend view if possible This commit fixes two tests related to removed workaround of KT-59818, and also makes processing suspend functions in J/K hierarchy more consistent. Before this commit, when we had Java class "suspend" method (implemented with the help of Continuation) overriding Kotlin suspend fun, the Kotlin suspend fun was visible in outer use-site scope, and the Java method was invisible. Also, we used a special "Java suspend view" just to determine that Java method overrides Kotlin suspend fun and no more. After this commit, Java class "suspend" method will be visible in this hierarchy and Kotlin suspend fun will not. Also, the "suspend" is visible as a synthetic Kotlin suspend fun which is more correct. Related to KT-63233 #KT-59818 Fixed --- .../fir/java/scopes/JavaClassUseSiteMemberScope.kt | 14 +++----------- .../scopes/impl/AbstractFirUseSiteMemberScope.kt | 8 +++++++- .../coroutines/suspendCovariantJavaOverrides.kt | 2 -- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassUseSiteMemberScope.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassUseSiteMemberScope.kt index 3c38d58e2e9..4d44146b699 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassUseSiteMemberScope.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassUseSiteMemberScope.kt @@ -23,8 +23,8 @@ import org.jetbrains.kotlin.fir.java.declarations.FirJavaMethod import org.jetbrains.kotlin.fir.java.declarations.buildJavaMethodCopy import org.jetbrains.kotlin.fir.java.declarations.buildJavaValueParameterCopy import org.jetbrains.kotlin.fir.java.resolveIfJavaType -import org.jetbrains.kotlin.fir.java.syntheticPropertiesStorage import org.jetbrains.kotlin.fir.java.symbols.FirJavaOverriddenSyntheticPropertySymbol +import org.jetbrains.kotlin.fir.java.syntheticPropertiesStorage import org.jetbrains.kotlin.fir.java.toConeKotlinTypeProbablyFlexible import org.jetbrains.kotlin.fir.resolve.defaultType import org.jetbrains.kotlin.fir.resolve.toSymbol @@ -273,8 +273,7 @@ class JavaClassUseSiteMemberScope( if (hasCorrespondingProperty) return false return !doesOverrideRenamedBuiltins() && - !shouldBeVisibleAsOverrideOfBuiltInWithErasedValueParameters() && - !doesOverrideSuspendFunction() + !shouldBeVisibleAsOverrideOfBuiltInWithErasedValueParameters() } private fun FirNamedFunctionSymbol.doesOverrideRenamedBuiltins(): Boolean { @@ -334,14 +333,7 @@ class JavaClassUseSiteMemberScope( } } - private fun FirNamedFunctionSymbol.doesOverrideSuspendFunction(): Boolean { - val suspendView = createSuspendView() ?: return false - return superTypeScopes.any { scope -> - scope.getFunctions(name).any { it.isSuspend && overrideChecker.isOverriddenFunction(suspendView, it.fir) } - } - } - - private fun FirNamedFunctionSymbol.createSuspendView(): FirSimpleFunction? { + override fun FirNamedFunctionSymbol.createSuspendView(): FirSimpleFunction? { val continuationParameter = fir.valueParameters.lastOrNull() ?: return null val owner = ownerClassLookupTag.toSymbol(session)?.fir as? FirJavaClass ?: return null val continuationParameterType = continuationParameter diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/AbstractFirUseSiteMemberScope.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/AbstractFirUseSiteMemberScope.kt index f066392ee5a..fa6b60d0985 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/AbstractFirUseSiteMemberScope.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/AbstractFirUseSiteMemberScope.kt @@ -6,6 +6,8 @@ package org.jetbrains.kotlin.fir.scopes.impl import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction +import org.jetbrains.kotlin.fir.declarations.isJavaOrEnhancement import org.jetbrains.kotlin.fir.declarations.utils.isStatic import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor import org.jetbrains.kotlin.fir.scopes.* @@ -75,7 +77,7 @@ abstract class AbstractFirUseSiteMemberScope( if (!symbol.isVisibleInCurrentClass()) return@processFunctionsByName val directOverridden = computeDirectOverriddenForDeclaredFunction(symbol) directOverriddenFunctions[symbol] = directOverridden - destination += symbol + destination += (symbol.takeIf { it.isJavaOrEnhancement }?.createSuspendView()?.symbol ?: symbol) } } @@ -216,4 +218,8 @@ abstract class AbstractFirUseSiteMemberScope( override fun getClassifierNames(): Set { return classifierNamesCached } + + protected open fun FirNamedFunctionSymbol.createSuspendView(): FirSimpleFunction? { + return null + } } diff --git a/compiler/testData/codegen/box/coroutines/suspendCovariantJavaOverrides.kt b/compiler/testData/codegen/box/coroutines/suspendCovariantJavaOverrides.kt index d198767ecdc..fe3fafbad03 100644 --- a/compiler/testData/codegen/box/coroutines/suspendCovariantJavaOverrides.kt +++ b/compiler/testData/codegen/box/coroutines/suspendCovariantJavaOverrides.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_K2: JVM_IR, JS_IR -// FIR status: wrong ABSTRACT_SUPER_CALL // TARGET_BACKEND: JVM // WITH_STDLIB // WITH_COROUTINES