From 6683e229b42ca53c4743ca67f070098b9b1e933c Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Thu, 13 Apr 2023 18:49:53 +0200 Subject: [PATCH] K2: Simplify JvmMappedScope-related code a bit JvmMappedScope.Signatures became mostly irrelevant and only "useful" for checking if we really need to have the scope mapped Now, the check is rewritten according to K1 semantics ^KT-57694 Fixed --- .../fir/resolve/scopes/JvmMappedScopes.kt | 38 +++++++-------- .../kotlin/fir/scopes/jvm/JvmMappedScope.kt | 47 ++++--------------- 2 files changed, 29 insertions(+), 56 deletions(-) diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/resolve/scopes/JvmMappedScopes.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/resolve/scopes/JvmMappedScopes.kt index 157da54b0d1..3c0e70f9d53 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/resolve/scopes/JvmMappedScopes.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/resolve/scopes/JvmMappedScopes.kt @@ -16,6 +16,8 @@ import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope import org.jetbrains.kotlin.fir.scopes.jvm.JvmMappedScope import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope +import org.jetbrains.kotlin.fir.types.isSomeFunctionType +import org.jetbrains.kotlin.name.StandardClassIds fun wrapScopeWithJvmMapped( klass: FirClass, @@ -32,23 +34,21 @@ fun wrapScopeWithJvmMapped( val symbolProvider = useSiteSession.symbolProvider val javaClass = symbolProvider.getClassLikeSymbolByClassId(javaClassId)?.fir as? FirRegularClass ?: return declaredMemberScope - val preparedSignatures = JvmMappedScope.prepareSignatures(javaClass, JavaToKotlinClassMap.isMutable(kotlinUnsafeFqName)) - return if (preparedSignatures.isNotEmpty()) { - val javaClassUseSiteScope = javaClass.unsubstitutedScope( - useSiteSession, - scopeSession, - withForcedTypeCalculator = false, - memberRequiredPhase = memberRequiredPhase, - ) - JvmMappedScope( - useSiteSession, - klass, - javaClass, - declaredMemberScope, - javaClassUseSiteScope, - preparedSignatures, - ) - } else { - declaredMemberScope - } + + // We don't add additional built-in members to function types and kotlin.Any (see JvmBuiltInsCustomizer.getJavaAnalogue) + if (klass.symbol.toLookupTag().isSomeFunctionType(useSiteSession) || classId == StandardClassIds.Any) return declaredMemberScope + + val javaClassUseSiteScope = javaClass.unsubstitutedScope( + useSiteSession, + scopeSession, + withForcedTypeCalculator = false, + memberRequiredPhase = memberRequiredPhase, + ) + return JvmMappedScope( + useSiteSession, + klass, + javaClass, + declaredMemberScope, + javaClassUseSiteScope, + ) } diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt index a1d6002486b..361536ef350 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt @@ -50,7 +50,6 @@ class JvmMappedScope( private val firJavaClass: FirRegularClass, private val declaredMemberScope: FirContainingNamesAwareScope, private val javaMappedClassUseSiteScope: FirTypeScope, - private val signatures: Signatures, ) : FirTypeScope() { private val functionsCache = mutableMapOf() @@ -228,7 +227,7 @@ class JvmMappedScope( } override fun processDeclaredConstructors(processor: (FirConstructorSymbol) -> Unit) { - javaMappedClassUseSiteScope.processDeclaredConstructors { javaCtorSymbol -> + javaMappedClassUseSiteScope.processDeclaredConstructors processor@{ javaCtorSymbol -> fun FirConstructor.isShadowedBy(ctorFromKotlin: FirConstructorSymbol): Boolean { // assuming already checked for visibility @@ -250,14 +249,15 @@ class JvmMappedScope( // Here the logic is generally the same, but simplified for performance by reordering checks and avoiding checking // for the impossible combinations val javaCtor = javaCtorSymbol.fir - if (javaCtor.status.visibility.isPublicAPI && !javaCtor.isDeprecated() && - javaCtor.computeJvmDescriptor() !in signatures.hiddenConstructors && - !javaCtor.isTrivialCopyConstructor() && - firKotlinClassConstructors.none { javaCtor.isShadowedBy(it) } - ) { - val newSymbol = getOrCreateCopy(javaCtorSymbol) - processor(newSymbol) - } + + if (!javaCtor.status.visibility.isPublicAPI || javaCtor.isDeprecated()) return@processor + val signature = SignatureBuildingComponents.signature(firJavaClass.classId, javaCtor.computeJvmDescriptor()) + if (signature in JvmBuiltInsSignatures.HIDDEN_CONSTRUCTOR_SIGNATURES) return@processor + if (javaCtor.isTrivialCopyConstructor()) return@processor + if (firKotlinClassConstructors.any { javaCtor.isShadowedBy(it) }) return@processor + + val newSymbol = getOrCreateCopy(javaCtorSymbol) + processor(newSymbol) } declaredMemberScope.processDeclaredConstructors(processor) @@ -307,33 +307,6 @@ class JvmMappedScope( } companion object { - data class Signatures(val visibleMethodSignaturesByName: Map>, val hiddenConstructors: Set) { - fun isEmpty() = visibleMethodSignaturesByName.isEmpty() && hiddenConstructors.isEmpty() - fun isNotEmpty() = !isEmpty() - } - - fun prepareSignatures(klass: FirRegularClass, isMutable: Boolean): Signatures { - - val signaturePrefix = klass.symbol.classId.toString() - val visibleMethodsByName = mutableMapOf>() - (JvmBuiltInsSignatures.VISIBLE_METHOD_SIGNATURES).filter { signature -> - signature in JvmBuiltInsSignatures.MUTABLE_METHOD_SIGNATURES == isMutable && - signature.startsWith(signaturePrefix) - }.map { signature -> - // +1 to delete dot before function name - signature.substring(signaturePrefix.length + 1) - }.forEach { - visibleMethodsByName.getOrPut(Name.identifier(it.substringBefore("("))) { mutableSetOf() }.add(it) - } - - val hiddenConstructors = - JvmBuiltInsSignatures.HIDDEN_CONSTRUCTOR_SIGNATURES - .filter { it.startsWith(signaturePrefix) } - .mapTo(mutableSetOf()) { it.substring(signaturePrefix.length + 1) } - - return Signatures(visibleMethodsByName, hiddenConstructors) - } - /** * For fromClass=A, toClass=B classes * @returns {T1 -> F1, T2 -> F2} substitution