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
This commit is contained in:
Denis.Zharkov
2023-04-13 18:49:53 +02:00
committed by Space Team
parent 6b1d9c4d56
commit 6683e229b4
2 changed files with 29 additions and 56 deletions
@@ -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,
)
}
@@ -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<FirNamedFunctionSymbol, FirNamedFunctionSymbol>()
@@ -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<Name, Set<String>>, val hiddenConstructors: Set<String>) {
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<Name, MutableSet<String>>()
(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<T1, T2>, toClass=B<F1, F1> classes
* @returns {T1 -> F1, T2 -> F2} substitution