From e58b5e7d2277551f51c6dd67b11bba8f9898ea96 Mon Sep 17 00:00:00 2001 From: "Anastasia.Nekrasova" Date: Wed, 13 Dec 2023 13:26:51 +0200 Subject: [PATCH] K2: change resolution for deprecated actual declaration This commit addresses a scenario where an 'actual' declaration is marked with a Deprecated annotation at the 'Hidden' level, while the corresponding 'expect' declaration is not. When resolving, 'CheckHiddenDeclaration' marks the 'actual' declaration as unsuccessful, leading to the selection of the 'expect' declaration as the successful candidate. 'FirDeprecationChecker' handles a case where an 'actual' class is annotated with Deprecated, but the 'expect' class is not. During the checking of the 'actual' class constructor, 'CheckHiddenDeclaration' skips it due to the absence of a direct Deprecated annotation. 'FirDeprecationChecker' then identifies this constructor and reports a DEPRECATION_ERROR. 'FirDeprecationChecker' will now be applied to solving problems related to 'actual' declarations with corresponding Deprecated annotations. The process remains the same: 'CheckHiddenDeclaration' will skip the 'actual' declaration, and then 'FirDeprecationChecker' will identify it and report the error. ^KT-61792 Fixed --- ...CompilerTestFE10TestdataTestGenerated.java | 16 ++++++++++ ...sticCompilerFE10TestDataTestGenerated.java | 16 ++++++++++ ...DiagnosticsWithLightTreeTestGenerated.java | 16 ++++++++++ ...endMPPDiagnosticsWithPsiTestGenerated.java | 16 ++++++++++ .../fir/resolve/calls/ResolutionStages.kt | 7 ++-- .../deprecatedAnnotation/hidden.fir.kt | 32 +++++++++++++++++++ .../deprecatedAnnotation/hidden.kt | 32 +++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 16 ++++++++++ 8 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.fir.kt create mode 100644 compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java index 2fd0e4c5245..b04fee82708 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java @@ -25540,6 +25540,22 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia } } + @Nested + @TestMetadata("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation") + @TestDataPath("$PROJECT_ROOT") + public class DeprecatedAnnotation { + @Test + public void testAllFilesPresentInDeprecatedAnnotation() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation"), Pattern.compile("^(.+)\\.(kt|kts)$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true); + } + + @Test + @TestMetadata("hidden.kt") + public void testHidden() throws Exception { + runTest("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.kt"); + } + } + @Nested @TestMetadata("compiler/testData/diagnostics/tests/multiplatform/enum") @TestDataPath("$PROJECT_ROOT") diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java index fc449beba05..02d6e86a9da 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java @@ -25540,6 +25540,22 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated } } + @Nested + @TestMetadata("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation") + @TestDataPath("$PROJECT_ROOT") + public class DeprecatedAnnotation { + @Test + public void testAllFilesPresentInDeprecatedAnnotation() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation"), Pattern.compile("^(.+)\\.(kt|kts)$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true); + } + + @Test + @TestMetadata("hidden.kt") + public void testHidden() throws Exception { + runTest("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.kt"); + } + } + @Nested @TestMetadata("compiler/testData/diagnostics/tests/multiplatform/enum") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithLightTreeTestGenerated.java index 31d534805db..44dc31d1f6a 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithLightTreeTestGenerated.java @@ -1387,6 +1387,22 @@ public class FirOldFrontendMPPDiagnosticsWithLightTreeTestGenerated extends Abst } } + @Nested + @TestMetadata("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation") + @TestDataPath("$PROJECT_ROOT") + public class DeprecatedAnnotation { + @Test + public void testAllFilesPresentInDeprecatedAnnotation() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), TargetBackend.JVM_IR, true); + } + + @Test + @TestMetadata("hidden.kt") + public void testHidden() throws Exception { + runTest("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.kt"); + } + } + @Nested @TestMetadata("compiler/testData/diagnostics/tests/multiplatform/enum") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithPsiTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithPsiTestGenerated.java index 1a3c3a1d3f3..73ffd20d789 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithPsiTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithPsiTestGenerated.java @@ -1387,6 +1387,22 @@ public class FirOldFrontendMPPDiagnosticsWithPsiTestGenerated extends AbstractFi } } + @Nested + @TestMetadata("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation") + @TestDataPath("$PROJECT_ROOT") + public class DeprecatedAnnotation { + @Test + public void testAllFilesPresentInDeprecatedAnnotation() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), TargetBackend.JVM_IR, true); + } + + @Test + @TestMetadata("hidden.kt") + public void testHidden() throws Exception { + runTest("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.kt"); + } + } + @Nested @TestMetadata("compiler/testData/diagnostics/tests/multiplatform/enum") @TestDataPath("$PROJECT_ROOT") 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 30a3ac85254..135d99bf6ba 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 @@ -9,10 +9,7 @@ import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.fir.FirElement import org.jetbrains.kotlin.fir.declarations.* -import org.jetbrains.kotlin.fir.declarations.utils.isFun -import org.jetbrains.kotlin.fir.declarations.utils.isInfix -import org.jetbrains.kotlin.fir.declarations.utils.isOperator -import org.jetbrains.kotlin.fir.declarations.utils.modality +import org.jetbrains.kotlin.fir.declarations.utils.* import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.languageVersionSettings import org.jetbrains.kotlin.fir.matchingParameterFunctionType @@ -760,6 +757,8 @@ internal object CheckCallModifiers : CheckerStage() { internal object CheckHiddenDeclaration : ResolutionStage() { override suspend fun check(candidate: Candidate, callInfo: CallInfo, sink: CheckerSink, context: ResolutionContext) { val symbol = candidate.symbol as? FirCallableSymbol<*> ?: return + /** Actual declarations are checked by [FirDeprecationChecker] */ + if (symbol.isActual) return val deprecation = symbol.getDeprecation(context.session, callInfo.callSite) if (deprecation?.deprecationLevel == DeprecationLevelValue.HIDDEN || isHiddenForThisCallSite(symbol, callInfo, candidate)) { sink.yieldDiagnostic(HiddenCandidate) diff --git a/compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.fir.kt b/compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.fir.kt new file mode 100644 index 00000000000..da3e38a2018 --- /dev/null +++ b/compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.fir.kt @@ -0,0 +1,32 @@ +// MODULE: m1-common +// FILE: common.kt + +expect class A() + +expect class B() + +expect fun foo(test: String) + +fun test() { + A() + B() + foo("") +} + +// MODULE: m2-jvm()()(m1-common) +// FILE: jvm.kt + +@Deprecated("", level = DeprecationLevel.HIDDEN) +actual class A + +actual class B @Deprecated("", level = DeprecationLevel.HIDDEN) actual constructor(){} + +@Deprecated("", level = DeprecationLevel.HIDDEN) +actual fun foo(test: String) { +} + +fun main() { + A() + B() + foo("") +} diff --git a/compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.kt b/compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.kt new file mode 100644 index 00000000000..3bffb119acd --- /dev/null +++ b/compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.kt @@ -0,0 +1,32 @@ +// MODULE: m1-common +// FILE: common.kt + +expect class A() + +expect class B() + +expect fun foo(test: String) + +fun test() { + A() + B() + foo("") +} + +// MODULE: m2-jvm()()(m1-common) +// FILE: jvm.kt + +@Deprecated("", level = DeprecationLevel.HIDDEN) +actual class A + +actual class B @Deprecated("", level = DeprecationLevel.HIDDEN) actual constructor(){} + +@Deprecated("", level = DeprecationLevel.HIDDEN) +actual fun foo(test: String) { +} + +fun main() { + A() + B() + foo("") +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index e2dca6272dc..56c6a538497 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -25540,6 +25540,22 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { } } + @Nested + @TestMetadata("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation") + @TestDataPath("$PROJECT_ROOT") + public class DeprecatedAnnotation { + @Test + public void testAllFilesPresentInDeprecatedAnnotation() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true); + } + + @Test + @TestMetadata("hidden.kt") + public void testHidden() throws Exception { + runTest("compiler/testData/diagnostics/tests/multiplatform/deprecatedAnnotation/hidden.kt"); + } + } + @Nested @TestMetadata("compiler/testData/diagnostics/tests/multiplatform/enum") @TestDataPath("$PROJECT_ROOT")