From 6b1d9c4d568ddb0a0099560aee177bd6773d30d7 Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Thu, 13 Apr 2023 18:34:21 +0200 Subject: [PATCH] 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 --- .../kotlin/fir/scopes/jvm/JvmMappedScope.kt | 45 +++---------------- 1 file changed, 7 insertions(+), 38 deletions(-) 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 6e596ef1066..a1d6002486b 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 @@ -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.($arguments)V" } - - listOf( - "", - "Ljava/lang/String;Ljava/lang/Throwable;", - "Ljava/lang/Throwable;", - "Ljava/lang/String;" - ).mapTo(this) { arguments -> "java/lang/Throwable.($arguments)V" } - - // TODO: remove after the end of getDeclaringClass/declaringClass deprecation period - add("java/lang/Enum.(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) }