From 24367e0ad8db01532c0d02fcb5ff08f1b699217a Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Tue, 6 Feb 2024 13:49:37 +0100 Subject: [PATCH] [FIR] Implement isHiddenEverywhereBesideSuperCalls logic for constructors #KT-61448 Fixed --- .../kotlin/fir/scopes/jvm/JvmMappedScope.kt | 34 +++++++++++++------ .../fir/resolve/calls/ResolutionStages.kt | 2 +- .../throwableConstructor.fir.kt | 8 +++-- .../throwableConstructor.kt | 4 +++ 4 files changed, 34 insertions(+), 14 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 f87806fff37..c862734579b 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 @@ -286,15 +286,19 @@ class JvmMappedScope( newReturnType = substitutor.substituteOrSelf(oldFunction.returnTypeRef.coneType), newSource = oldFunction.source, ).apply { - if (jdkMemberStatus == JDKMemberStatus.HIDDEN) { - isHiddenEverywhereBesideSuperCalls = HiddenEverywhereBesideSuperCallsStatus.HIDDEN - } else if (jdkMemberStatus == JDKMemberStatus.HIDDEN_IN_DECLARING_CLASS_ONLY) { - isHiddenEverywhereBesideSuperCalls = HiddenEverywhereBesideSuperCallsStatus.HIDDEN_IN_DECLARING_CLASS_ONLY - } + setHiddenAttributeIfNecessary(jdkMemberStatus) } return newSymbol } + private fun FirCallableDeclaration.setHiddenAttributeIfNecessary(jdkMemberStatus: JDKMemberStatus) { + if (jdkMemberStatus == JDKMemberStatus.HIDDEN) { + isHiddenEverywhereBesideSuperCalls = HiddenEverywhereBesideSuperCallsStatus.HIDDEN + } else if (jdkMemberStatus == JDKMemberStatus.HIDDEN_IN_DECLARING_CLASS_ONLY) { + isHiddenEverywhereBesideSuperCalls = HiddenEverywhereBesideSuperCallsStatus.HIDDEN_IN_DECLARING_CLASS_ONLY + } + } + override fun processDirectOverriddenFunctionsWithBaseScope( functionSymbol: FirNamedFunctionSymbol, processor: (FirNamedFunctionSymbol, FirTypeScope) -> ProcessorAction @@ -334,7 +338,13 @@ class JvmMappedScope( if (javaCtor.isTrivialCopyConstructor()) return@processor if (firKotlinClassConstructors.any { javaCtor.isShadowedBy(it) }) return@processor - val newSymbol = mappedSymbolCache.mappedConstructors.getValue(javaCtorSymbol, this) + val jvmDescriptor = javaCtorSymbol.fir.computeJvmDescriptor() + val jdkMemberStatus = getJdkMethodStatus(jvmDescriptor) + + if (jdkMemberStatus == JDKMemberStatus.DROP) return@processor + + val newSymbol = mappedSymbolCache.mappedConstructors.getValue(javaCtorSymbol, this to jdkMemberStatus) + processor(newSymbol) } @@ -343,7 +353,7 @@ class JvmMappedScope( private fun FirDeclaration.isDeprecated(): Boolean = symbol.getDeprecation(session, callSite = null) != null - private fun createMappedConstructor(symbol: FirConstructorSymbol): FirConstructorSymbol { + private fun createMappedConstructor(symbol: FirConstructorSymbol, jdkMemberStatus: JDKMemberStatus): FirConstructorSymbol { val oldConstructor = symbol.fir val classId = firKotlinClass.classId val newSymbol = FirConstructorSymbol(CallableId(classId, classId.shortClassName)) @@ -361,7 +371,9 @@ class JvmMappedScope( isExpect = false, deferredReturnTypeCalculation = null, newSource = oldConstructor.source, - ) + ).apply { + setHiddenAttributeIfNecessary(jdkMemberStatus) + } return newSymbol } @@ -398,9 +410,9 @@ class JvmMappedScope( scope.createHiddenFakeFunction(name) } - val mappedConstructors: FirCache = - cachesFactory.createCache { symbol, scope -> - scope.createMappedConstructor(symbol) + val mappedConstructors: FirCache> = + cachesFactory.createCache { symbol, (scope, jdkMemberStatus) -> + scope.createMappedConstructor(symbol, jdkMemberStatus) } } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt index 2e8eb93e28c..15cf4cb33e2 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt @@ -774,9 +774,9 @@ internal object CheckHiddenDeclaration : ResolutionStage() { sink.reportDiagnostic(CallToDeprecatedOverrideOfHidden) } - if (symbol.fir.dispatchReceiverType == null || symbol !is FirNamedFunctionSymbol) return false val isSuperCall = callInfo.callSite.isSuperCall(session) if (symbol.isHidden(isSuperCall, isOverridden = false) == CallToPotentiallyHiddenSymbolResult.Hidden) return true + if (symbol.fir.dispatchReceiverType == null || symbol !is FirNamedFunctionSymbol) return false val scope = candidate.originScope as? FirTypeScope ?: return false diff --git a/compiler/testData/diagnostics/tests/testWithModifiedMockJdk/throwableConstructor.fir.kt b/compiler/testData/diagnostics/tests/testWithModifiedMockJdk/throwableConstructor.fir.kt index 918ba3b6380..876a989fa6a 100644 --- a/compiler/testData/diagnostics/tests/testWithModifiedMockJdk/throwableConstructor.fir.kt +++ b/compiler/testData/diagnostics/tests/testWithModifiedMockJdk/throwableConstructor.fir.kt @@ -1,6 +1,10 @@ // !JDK_KIND: MODIFIED_MOCK_JDK -abstract class A : Throwable(1.0) {} +// K2 difference is in accordance with KT-65438 where we defined that members in the "grey list" +// (i.e. neither explicitly visible nor hidden) are hidden in the declaring class and since constructors are not inheritable, +// they're always hidden. + +abstract class A : Throwable(1.0) {} fun foo() { - Throwable(1.5) + Throwable(1.5) } diff --git a/compiler/testData/diagnostics/tests/testWithModifiedMockJdk/throwableConstructor.kt b/compiler/testData/diagnostics/tests/testWithModifiedMockJdk/throwableConstructor.kt index 61b73c357c8..6503af19f31 100644 --- a/compiler/testData/diagnostics/tests/testWithModifiedMockJdk/throwableConstructor.kt +++ b/compiler/testData/diagnostics/tests/testWithModifiedMockJdk/throwableConstructor.kt @@ -1,4 +1,8 @@ // !JDK_KIND: MODIFIED_MOCK_JDK +// K2 difference is in accordance with KT-65438 where we defined that members in the "grey list" +// (i.e. neither explicitly visible nor hidden) are hidden in the declaring class and since constructors are not inheritable, +// they're always hidden. + abstract class A : Throwable(1.0) {} fun foo() {