From b2b5f4a63a35c014188aee561668f488963f8e90 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Mon, 6 Dec 2021 15:40:30 +0300 Subject: [PATCH] [FE 1.0] Don't report deprecation diagnostics about enum on it's own entries ^KT-37975 Fixed --- ...CompilerTestFE10TestdataTestGenerated.java | 6 ++++++ ...irOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 ++++++ .../DeprecatedClassifierUsageChecker.kt | 13 ++++++++++++ .../tests/deprecated/usageOnEnum.kt | 8 +++++++ .../tests/deprecated/usageOnEnum.txt | 21 +++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++++++ .../kotlin/config/LanguageVersionSettings.kt | 1 + 8 files changed, 67 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/deprecated/usageOnEnum.kt create mode 100644 compiler/testData/diagnostics/tests/deprecated/usageOnEnum.txt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 6706a81e23d..9068c94c483 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -8873,6 +8873,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/deprecated/unusedImport.kt"); } + @Test + @TestMetadata("usageOnEnum.kt") + public void testUsageOnEnum() throws Exception { + runTest("compiler/testData/diagnostics/tests/deprecated/usageOnEnum.kt"); + } + @Test @TestMetadata("warningOnConstructorErrorOnClass.kt") public void testWarningOnConstructorErrorOnClass() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index d6c8303f317..d76f7b5e202 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -8873,6 +8873,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/deprecated/unusedImport.kt"); } + @Test + @TestMetadata("usageOnEnum.kt") + public void testUsageOnEnum() throws Exception { + runTest("compiler/testData/diagnostics/tests/deprecated/usageOnEnum.kt"); + } + @Test @TestMetadata("warningOnConstructorErrorOnClass.kt") public void testWarningOnConstructorErrorOnClass() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index bcc96e31ff3..a52b610a5e4 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -8873,6 +8873,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/deprecated/unusedImport.kt"); } + @Test + @TestMetadata("usageOnEnum.kt") + public void testUsageOnEnum() throws Exception { + runTest("compiler/testData/diagnostics/tests/deprecated/usageOnEnum.kt"); + } + @Test @TestMetadata("warningOnConstructorErrorOnClass.kt") public void testWarningOnConstructorErrorOnClass() throws Exception { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/DeprecatedClassifierUsageChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/DeprecatedClassifierUsageChecker.kt index 02a04c738bb..6e848b35e63 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/DeprecatedClassifierUsageChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/DeprecatedClassifierUsageChecker.kt @@ -17,14 +17,27 @@ package org.jetbrains.kotlin.resolve.checkers import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.descriptors.ClassifierDescriptor +import org.jetbrains.kotlin.descriptors.ConstructorDescriptor +import org.jetbrains.kotlin.psi.KtEnumEntrySuperclassReferenceExpression import org.jetbrains.kotlin.psi.KtThisExpression +import org.jetbrains.kotlin.psi.synthetics.findClassDescriptor +import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall import org.jetbrains.kotlin.resolve.deprecation.createDeprecationDiagnostic class DeprecatedClassifierUsageChecker : ClassifierUsageChecker { override fun check(targetDescriptor: ClassifierDescriptor, element: PsiElement, context: ClassifierUsageCheckerContext) { if (element.parent is KtThisExpression) return + if (context.languageVersionSettings.supportsFeature(LanguageFeature.NoDeprecationOnDeprecatedEnumEntries) && element is KtEnumEntrySuperclassReferenceExpression) { + val referencedEnum = + (element.getResolvedCall(context.trace.bindingContext)?.resultingDescriptor as? ConstructorDescriptor)?.constructedClass + if (referencedEnum == targetDescriptor) { + return + } + } + for (deprecation in context.deprecationResolver.getDeprecations(targetDescriptor)) { context.trace.report(createDeprecationDiagnostic(element, deprecation, context.languageVersionSettings)) } diff --git a/compiler/testData/diagnostics/tests/deprecated/usageOnEnum.kt b/compiler/testData/diagnostics/tests/deprecated/usageOnEnum.kt new file mode 100644 index 00000000000..43cb5cea32a --- /dev/null +++ b/compiler/testData/diagnostics/tests/deprecated/usageOnEnum.kt @@ -0,0 +1,8 @@ +// FIR_IDENTICAL +// LANGUAGE: +NoDeprecationOnDeprecatedEnumEntries +// ISSUE: KT-37975 + +@Deprecated("") +enum class Foo(val x: Int) { + A(42) +} diff --git a/compiler/testData/diagnostics/tests/deprecated/usageOnEnum.txt b/compiler/testData/diagnostics/tests/deprecated/usageOnEnum.txt new file mode 100644 index 00000000000..992f38685ed --- /dev/null +++ b/compiler/testData/diagnostics/tests/deprecated/usageOnEnum.txt @@ -0,0 +1,21 @@ +package + +@kotlin.Deprecated(message = "") public final enum class Foo : kotlin.Enum { + enum entry A + + private constructor Foo(/*0*/ x: kotlin.Int) + public final override /*1*/ /*fake_override*/ val name: kotlin.String + public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int + public final val x: kotlin.Int + protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: Foo): kotlin.Int + public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit + public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class! + public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + // Static members + public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): Foo + public final /*synthesized*/ fun values(): kotlin.Array +} 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 4f33ef4895f..9c35fa76023 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 @@ -8879,6 +8879,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/deprecated/unusedImport.kt"); } + @Test + @TestMetadata("usageOnEnum.kt") + public void testUsageOnEnum() throws Exception { + runTest("compiler/testData/diagnostics/tests/deprecated/usageOnEnum.kt"); + } + @Test @TestMetadata("warningOnConstructorErrorOnClass.kt") public void testWarningOnConstructorErrorOnClass() throws Exception { diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 1bc95ea9904..e5ff2e95906 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -246,6 +246,7 @@ enum class LanguageFeature( InlineClassImplementationByDelegation(KOTLIN_1_7), QualifiedSupertypeMayBeExtendedByOtherSupertype(KOTLIN_1_7), YieldIsNoMoreReserved(KOTLIN_1_7), + NoDeprecationOnDeprecatedEnumEntries(KOTLIN_1_7), // KT-37975 // 1.8