K2: Adjust how additional built-in constructors from JDK are handled

This change is both makes code more consistent with K1 version
and fixes some issues introduced by previous changes

Before this fix, the test
FirPsiOldFrontendDiagnosticsTestGenerated$Tests$Modifiers$Const.testKCallable_after
was failing

^KT-57694 In Progress
This commit is contained in:
Denis.Zharkov
2023-04-13 18:34:21 +02:00
committed by Space Team
parent e4480a1c52
commit 6b1d9c4d56
@@ -26,8 +26,10 @@ import org.jetbrains.kotlin.fir.scopes.impl.buildSubstitutorForOverridesCheck
import org.jetbrains.kotlin.fir.scopes.impl.declaredMemberScope
import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterLookupTag
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
import org.jetbrains.kotlin.fir.types.coneType
import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl
import org.jetbrains.kotlin.fir.types.lowerBoundIfFlexible
import org.jetbrains.kotlin.load.java.BuiltinSpecialProperties
import org.jetbrains.kotlin.load.java.SpecialGenericSignatures
import org.jetbrains.kotlin.load.java.getPropertyNamesCandidatesByAccessorName
@@ -241,15 +243,14 @@ class JvmMappedScope(
fun FirConstructor.isTrivialCopyConstructor(): Boolean =
valueParameters.singleOrNull()?.let {
val type = substitutor.substituteOrSelf(it.returnTypeRef.coneType)
type == firKotlinClass.defaultType()
(it.returnTypeRef.coneType.lowerBoundIfFlexible() as? ConeClassLikeType)?.lookupTag == firKotlinClass.symbol.toLookupTag()
} ?: false
// In K1 it is handled by JvmBuiltInsCustomizer.getConstructors
// 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 &&
if (javaCtor.status.visibility.isPublicAPI && !javaCtor.isDeprecated() &&
javaCtor.computeJvmDescriptor() !in signatures.hiddenConstructors &&
!javaCtor.isTrivialCopyConstructor() &&
firKotlinClassConstructors.none { javaCtor.isShadowedBy(it) }
@@ -262,6 +263,8 @@ class JvmMappedScope(
declaredMemberScope.processDeclaredConstructors(processor)
}
private fun FirDeclaration.isDeprecated(): Boolean = symbol.getDeprecation(session, callSite = null) != null
private fun getOrCreateCopy(symbol: FirConstructorSymbol): FirConstructorSymbol {
return constructorsCache.getOrPut(symbol) {
val oldConstructor = symbol.fir
@@ -309,40 +312,6 @@ class JvmMappedScope(
fun isNotEmpty() = !isEmpty()
}
// NOTE: No-arg constructors
private val additionalHiddenConstructors = buildSet {
// kotlin.text.String pseudo-constructors should be used instead of java.lang.String constructors
listOf(
"",
"Lkotlin/ByteArray;IILjava/nio/charset/Charset;",
"Lkotlin/ByteArray;Ljava/nio/charset/Charset;",
"Lkotlin/ByteArray;III",
"Lkotlin/ByteArray;II",
"Lkotlin/ByteArray;IILjava/lang/String;",
"Lkotlin/ByteArray;I",
"Lkotlin/ByteArray;",
"Lkotlin/ByteArray;Ljava/lang/String;",
"IILkotlin/CharArray;",
"Lkotlin/CharArray;",
"Lkotlin/CharArray;Z",
"Lkotlin/CharArray;II",
"Lkotlin/IntArray;II",
"Ljava/lang/StringBuffer;",
"Ljava/lang/StringBuilder;",
"Ljava/lang/String;",
).mapTo(this) { arguments -> "java/lang/String.<init>($arguments)V" }
listOf(
"",
"Ljava/lang/String;Ljava/lang/Throwable;",
"Ljava/lang/Throwable;",
"Ljava/lang/String;"
).mapTo(this) { arguments -> "java/lang/Throwable.<init>($arguments)V" }
// TODO: remove after the end of getDeclaringClass/declaringClass deprecation period
add("java/lang/Enum.<init>(Ljava/lang/String;I)V")
}
fun prepareSignatures(klass: FirRegularClass, isMutable: Boolean): Signatures {
val signaturePrefix = klass.symbol.classId.toString()
@@ -358,7 +327,7 @@ class JvmMappedScope(
}
val hiddenConstructors =
(JvmBuiltInsSignatures.HIDDEN_CONSTRUCTOR_SIGNATURES + additionalHiddenConstructors)
JvmBuiltInsSignatures.HIDDEN_CONSTRUCTOR_SIGNATURES
.filter { it.startsWith(signaturePrefix) }
.mapTo(mutableSetOf()) { it.substring(signaturePrefix.length + 1) }