Add a warning for name shadowing case with Enum.entries

^KT-53153
This commit is contained in:
Mikhail Zarechenskiy
2023-01-02 15:40:21 +01:00
committed by Space Team
parent c70a1b1884
commit cf4b415a20
10 changed files with 179 additions and 1 deletions
@@ -10355,6 +10355,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/tests/enum/entries/genericEntriesPropertyClash.kt");
}
@Test
@TestMetadata("nameShadowingOfExternallyDefinedEntries.kt")
public void testNameShadowingOfExternallyDefinedEntries() throws Exception {
runTest("compiler/testData/diagnostics/tests/enum/entries/nameShadowingOfExternallyDefinedEntries.kt");
}
@Test
@TestMetadata("redeclarationOfEnumEntriesNameWithIntrinsic.kt")
public void testRedeclarationOfEnumEntriesNameWithIntrinsic() throws Exception {
@@ -10361,6 +10361,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
runTest("compiler/testData/diagnostics/tests/enum/entries/genericEntriesPropertyClash.kt");
}
@Test
@TestMetadata("nameShadowingOfExternallyDefinedEntries.kt")
public void testNameShadowingOfExternallyDefinedEntries() throws Exception {
runTest("compiler/testData/diagnostics/tests/enum/entries/nameShadowingOfExternallyDefinedEntries.kt");
}
@Test
@TestMetadata("redeclarationOfEnumEntriesNameWithIntrinsic.kt")
public void testRedeclarationOfEnumEntriesNameWithIntrinsic() throws Exception {
@@ -10355,6 +10355,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
runTest("compiler/testData/diagnostics/tests/enum/entries/genericEntriesPropertyClash.kt");
}
@Test
@TestMetadata("nameShadowingOfExternallyDefinedEntries.kt")
public void testNameShadowingOfExternallyDefinedEntries() throws Exception {
runTest("compiler/testData/diagnostics/tests/enum/entries/nameShadowingOfExternallyDefinedEntries.kt");
}
@Test
@TestMetadata("redeclarationOfEnumEntriesNameWithIntrinsic.kt")
public void testRedeclarationOfEnumEntriesNameWithIntrinsic() throws Exception {
@@ -143,6 +143,7 @@ public interface Errors {
DiagnosticFactory1<PsiElement, PropertyDescriptor> DEPRECATED_ACCESS_TO_ENUM_COMPANION_PROPERTY = DiagnosticFactory1.create(WARNING);
DiagnosticFactory0<PsiElement> DEPRECATED_ACCESS_TO_ENUM_ENTRY_COMPANION_PROPERTY = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<PsiElement> DEPRECATED_ACCESS_TO_ENTRY_PROPERTY_FROM_ENUM = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<KtEnumEntry> DEPRECATED_DECLARATION_OF_ENUM_ENTRY = DiagnosticFactory0.create(WARNING);
DiagnosticFactory2<PsiElement, PropertyDescriptor, ClassDescriptor> DEPRECATED_RESOLVE_WITH_AMBIGUOUS_ENUM_ENTRY = DiagnosticFactory2.create(WARNING);
@@ -74,6 +74,7 @@ public class DefaultErrorMessages {
MAP.put(DEPRECATED_ACCESS_BY_SHORT_NAME, "Access to this type by short name is deprecated, and soon is going to be removed. Please, add explicit qualifier or import", NAME);
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_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);
@@ -10,11 +10,13 @@ import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
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.resolve.scopes.receivers.ReceiverValue
object CustomEnumEntriesMigrationCallChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
@@ -26,6 +28,8 @@ object CustomEnumEntriesMigrationCallChecker : CallChecker {
resolvedCall.isCallViaCompanionOnEnumClassQualifier(descriptor)
) {
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))
}
}
@@ -43,4 +47,10 @@ object CustomEnumEntriesMigrationCallChecker : CallChecker {
return dispatchReceiver.isQualifierFor(grandParent)
}
private fun PropertyDescriptor.isCallToExternalEntriesInsideEnum(contextExpression: PsiElement): Boolean {
return !DescriptorUtils.isEnumClass(this.containingDeclaration) &&
contextExpression.parent !is KtDotQualifiedExpression &&
contextExpression.parentsWithSelf.any { it is KtClass && it.isEnum() }
}
}
@@ -0,0 +1,39 @@
// !LANGUAGE: -EnumEntries
package pckg
val entries = "E"
enum class E {
;
fun foo() {
entries
pckg.entries
}
}
class A {
enum class E {
;
class B {
fun foo() {
entries
pckg.entries
}
}
class C {
val entries = 0
fun foo() {
// technically, this warning is incorrect but I believe it's OK to report anyway
// first, logic in the compiler will be complicated if we'll try to avoid reporting warnings here
// second, this code smells, it'd be better to use qualifiers here anyway
entries
this.entries
}
}
}
}
@@ -0,0 +1,39 @@
// !LANGUAGE: -EnumEntries
package pckg
val entries = "E"
enum class E {
;
fun foo() {
<!DEPRECATED_ACCESS_TO_ENTRY_PROPERTY_FROM_ENUM!>entries<!>
pckg.entries
}
}
class A {
enum class E {
;
class B {
fun foo() {
<!DEPRECATED_ACCESS_TO_ENTRY_PROPERTY_FROM_ENUM!>entries<!>
pckg.entries
}
}
class C {
val entries = 0
fun foo() {
// technically, this warning is incorrect but I believe it's OK to report anyway
// first, logic in the compiler will be complicated if we'll try to avoid reporting warnings here
// second, this code smells, it'd be better to use qualifiers here anyway
<!DEPRECATED_ACCESS_TO_ENTRY_PROPERTY_FROM_ENUM!>entries<!>
this.entries
}
}
}
}
@@ -0,0 +1,64 @@
package
package pckg {
public val entries: kotlin.String = "E"
public final class A {
public constructor A()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final enum class E : kotlin.Enum<pckg.A.E> {
private constructor E()
@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: pckg.A.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<pckg.A.E!>!
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final class B {
public constructor B()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class C {
public constructor C()
public final val entries: kotlin.Int = 0
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun foo(): kotlin.Unit
public open 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): pckg.A.E
public final /*synthesized*/ fun values(): kotlin.Array<pckg.A.E>
}
}
public final enum class E : kotlin.Enum<pckg.E> {
private constructor E()
@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: pckg.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 fun foo(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<pckg.E!>!
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): pckg.E
public final /*synthesized*/ fun values(): kotlin.Array<pckg.E>
}
}
@@ -10361,6 +10361,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/enum/entries/genericEntriesPropertyClash.kt");
}
@Test
@TestMetadata("nameShadowingOfExternallyDefinedEntries.kt")
public void testNameShadowingOfExternallyDefinedEntries() throws Exception {
runTest("compiler/testData/diagnostics/tests/enum/entries/nameShadowingOfExternallyDefinedEntries.kt");
}
@Test
@TestMetadata("redeclarationOfEnumEntriesNameWithIntrinsic.kt")
public void testRedeclarationOfEnumEntriesNameWithIntrinsic() throws Exception {