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 ab87f8c041e..571bb24c5ae 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 @@ -10319,6 +10319,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/enum/entries"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @Test + @TestMetadata("conflictingPropertyEntriesAndReference.kt") + public void testConflictingPropertyEntriesAndReference() throws Exception { + runTest("compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.kt"); + } + @Test @TestMetadata("entriesPropertyAsExtensionClash.kt") public void testEntriesPropertyAsExtensionClash() 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 ec0f66d803e..b938ab6fe03 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 @@ -10325,6 +10325,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/enum/entries"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @Test + @TestMetadata("conflictingPropertyEntriesAndReference.kt") + public void testConflictingPropertyEntriesAndReference() throws Exception { + runTest("compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.kt"); + } + @Test @TestMetadata("entriesPropertyAsExtensionClash.kt") public void testEntriesPropertyAsExtensionClash() 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 bf6dba2d3ac..185cab7039d 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 @@ -10319,6 +10319,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/enum/entries"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @Test + @TestMetadata("conflictingPropertyEntriesAndReference.kt") + public void testConflictingPropertyEntriesAndReference() throws Exception { + runTest("compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.kt"); + } + @Test @TestMetadata("entriesPropertyAsExtensionClash.kt") public void testEntriesPropertyAsExtensionClash() throws Exception { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index fb40bbf8591..25cb21b9142 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -144,6 +144,7 @@ public interface Errors { DiagnosticFactory0 DEPRECATED_ACCESS_TO_ENUM_ENTRY_COMPANION_PROPERTY = DiagnosticFactory0.create(WARNING); DiagnosticFactory0 DEPRECATED_ACCESS_TO_ENTRY_PROPERTY_FROM_ENUM = DiagnosticFactory0.create(WARNING); + DiagnosticFactory0 DEPRECATED_ACCESS_TO_ENUM_ENTRY_PROPERTY_AS_REFERENCE = DiagnosticFactory0.create(WARNING); DiagnosticFactory0 DEPRECATED_DECLARATION_OF_ENUM_ENTRY = DiagnosticFactory0.create(WARNING); DiagnosticFactory2 DEPRECATED_RESOLVE_WITH_AMBIGUOUS_ENUM_ENTRY = DiagnosticFactory2.create(WARNING); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index 08e7afbc9f2..60e71d8dcaa 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -75,6 +75,7 @@ public class DefaultErrorMessages { MAP.put(DEPRECATED_ACCESS_TO_ENUM_COMPANION_PROPERTY, "Ambiguous access to companion''s property ''{0}'' in enum is deprecated. Please, add explicit Companion qualifier to the class name", NAME); MAP.put(DEPRECATED_ACCESS_TO_ENUM_ENTRY_COMPANION_PROPERTY, "Ambiguous access to companion''s property 'entries' in enum is deprecated. Please, add explicit Companion qualifier to the class name"); MAP.put(DEPRECATED_ACCESS_TO_ENTRY_PROPERTY_FROM_ENUM, "Ambiguous access to property 'entries' from enum is deprecated. Please, add explicit qualifier to the call"); + MAP.put(DEPRECATED_ACCESS_TO_ENUM_ENTRY_PROPERTY_AS_REFERENCE, "Ambiguous access to property 'entries' is deprecated. Please, specify type of the referenced expression explicitly"); MAP.put(DEPRECATED_DECLARATION_OF_ENUM_ENTRY, "Conflicting declarations: enum entry 'entries' and the property 'Enum.entries' (KT-48872). Please, rename the enum entry declaration"); MAP.put(DEPRECATED_RESOLVE_WITH_AMBIGUOUS_ENUM_ENTRY, "Ambiguous access to property ''{0}'' is deprecated because similar enum entry ''{1}'' is available. Please add explicit named import or use fully qualified name", FQ_NAME, FQ_NAME); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/CustomEnumEntriesMigrationCallChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/CustomEnumEntriesMigrationCallChecker.kt index ef992352a93..c34ca8ee373 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/CustomEnumEntriesMigrationCallChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/CustomEnumEntriesMigrationCallChecker.kt @@ -7,9 +7,11 @@ package org.jetbrains.kotlin.resolve.calls.checkers import com.intellij.psi.PsiElement import org.jetbrains.kotlin.builtins.StandardNames +import org.jetbrains.kotlin.builtins.isFunctionType import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.diagnostics.Errors +import org.jetbrains.kotlin.psi.KtCallableReferenceExpression import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtDotQualifiedExpression import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf @@ -17,6 +19,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.resolve.descriptorUtil.isCompanionObject import org.jetbrains.kotlin.resolve.scopes.receivers.ClassValueReceiver +import org.jetbrains.kotlin.types.TypeUtils object CustomEnumEntriesMigrationCallChecker : CallChecker { override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) { @@ -30,6 +33,8 @@ object CustomEnumEntriesMigrationCallChecker : CallChecker { context.trace.report(Errors.DEPRECATED_ACCESS_TO_ENUM_ENTRY_COMPANION_PROPERTY.on(reportOn)) } else if (descriptor.isCallToExternalEntriesInsideEnum(reportOn)) { context.trace.report(Errors.DEPRECATED_ACCESS_TO_ENTRY_PROPERTY_FROM_ENUM.on(reportOn)) + } else if (descriptor.isReferenceToMemberEntriesWithoutExpectedFunctionalType(reportOn, context)) { + context.trace.report(Errors.DEPRECATED_ACCESS_TO_ENUM_ENTRY_PROPERTY_AS_REFERENCE.on(reportOn)) } } @@ -53,4 +58,14 @@ object CustomEnumEntriesMigrationCallChecker : CallChecker { contextExpression.parent !is KtDotQualifiedExpression && contextExpression.parentsWithSelf.any { it is KtClass && it.isEnum() } } + + private fun PropertyDescriptor.isReferenceToMemberEntriesWithoutExpectedFunctionalType( + expression: PsiElement, + context: CallCheckerContext + ): Boolean { + val expectedType = context.resolutionContext.expectedType + return expression.parent is KtCallableReferenceExpression && + DescriptorUtils.isEnumClass(containingDeclaration) && + (TypeUtils.noExpectedType(expectedType) || !expectedType.isFunctionType) + } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.fir.kt b/compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.fir.kt new file mode 100644 index 00000000000..3bd620c7a9f --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.fir.kt @@ -0,0 +1,14 @@ +// !LANGUAGE: -EnumEntries + +enum class E { + ; + + val entries: Int = 0 +} + +fun test() { + E::entries + val ref = E::entries + val refType: (E) -> Int = E::entries + val refTypeWithAnyExpectedType: Any = E::entries +} diff --git a/compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.kt b/compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.kt new file mode 100644 index 00000000000..9cdaf0266d3 --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.kt @@ -0,0 +1,14 @@ +// !LANGUAGE: -EnumEntries + +enum class E { + ; + + val entries: Int = 0 +} + +fun test() { + E::entries + val ref = E::entries + val refType: (E) -> Int = E::entries + val refTypeWithAnyExpectedType: Any = E::entries +} diff --git a/compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.txt b/compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.txt new file mode 100644 index 00000000000..a7d2a5a0fa5 --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.txt @@ -0,0 +1,21 @@ +package + +public fun test(): kotlin.Unit + +public final enum class E : kotlin.Enum { + private constructor E() + public final val entries: kotlin.Int = 0 + @kotlin.internal.IntrinsicConstEvaluation public final override /*1*/ /*fake_override*/ val name: kotlin.String + public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int + protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: E): 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): E + 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 68b48584334..fe2ed0d8959 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 @@ -10325,6 +10325,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/enum/entries"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @Test + @TestMetadata("conflictingPropertyEntriesAndReference.kt") + public void testConflictingPropertyEntriesAndReference() throws Exception { + runTest("compiler/testData/diagnostics/tests/enum/entries/conflictingPropertyEntriesAndReference.kt"); + } + @Test @TestMetadata("entriesPropertyAsExtensionClash.kt") public void testEntriesPropertyAsExtensionClash() throws Exception {