K1: add deprecation warning for property/field pair with different types
#KT-57905 Fixed
This commit is contained in:
committed by
Space Team
parent
2100f08655
commit
41e551e321
+9
-2
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.load.java.lazy.descriptors.isJavaField
|
||||
import org.jetbrains.kotlin.load.kotlin.computeJvmDescriptorWithoutName
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext
|
||||
@@ -42,8 +43,13 @@ object JvmPropertyVsFieldAmbiguityCallChecker : CallChecker {
|
||||
val hasLateInit = basePropertyDescriptor.selfOrBaseForFakeOverride { it.isLateInit }
|
||||
val hasCustomGetter = basePropertyDescriptor.selfOrBaseForFakeOverride { it.getter?.isDefault == false }
|
||||
val hasCustomSetter = basePropertyDescriptor.selfOrBaseForFakeOverride { it.setter?.isDefault == false }
|
||||
|
||||
val jvmDescriptorForField = resultingDescriptor.computeJvmDescriptorWithoutName()
|
||||
val jvmDescriptorForProperty = alternativePropertyDescriptor.computeJvmDescriptorWithoutName()
|
||||
|
||||
if (!hasLateInit && !hasCustomGetter && !hasCustomSetter &&
|
||||
basePropertyDescriptor.modality == Modality.FINAL
|
||||
basePropertyDescriptor.modality == Modality.FINAL &&
|
||||
jvmDescriptorForField == jvmDescriptorForProperty
|
||||
) return@forEach
|
||||
if (fieldClassDescriptor != null && propertyClassDescriptor != null &&
|
||||
DescriptorUtils.isSubclass(fieldClassDescriptor, propertyClassDescriptor)
|
||||
@@ -58,7 +64,8 @@ object JvmPropertyVsFieldAmbiguityCallChecker : CallChecker {
|
||||
val factory = when {
|
||||
hasCustomGetter -> ErrorsJvm.BASE_CLASS_FIELD_SHADOWS_DERIVED_CLASS_PROPERTY
|
||||
hasLateInit || hasCustomSetter -> ErrorsJvm.BACKING_FIELD_ACCESSED_DUE_TO_PROPERTY_FIELD_CONFLICT
|
||||
else -> ErrorsJvm.BASE_CLASS_FIELD_MAY_SHADOW_DERIVED_CLASS_PROPERTY
|
||||
basePropertyDescriptor.modality != Modality.FINAL -> ErrorsJvm.BASE_CLASS_FIELD_MAY_SHADOW_DERIVED_CLASS_PROPERTY
|
||||
else -> ErrorsJvm.BASE_CLASS_FIELD_WITH_DIFFERENT_SIGNATURE_THAN_DERIVED_CLASS_PROPERTY
|
||||
}
|
||||
context.trace.report(
|
||||
factory.on(
|
||||
|
||||
+6
@@ -250,6 +250,12 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
|
||||
"This behavior will be changed soon in favor of the property. " +
|
||||
"Please use explicit cast to {0} if you wish to preserve current behavior. " +
|
||||
"See https://youtrack.jetbrains.com/issue/KT-55017 for details", STRING, STRING);
|
||||
|
||||
MAP.put(BASE_CLASS_FIELD_WITH_DIFFERENT_SIGNATURE_THAN_DERIVED_CLASS_PROPERTY,
|
||||
"Now field from base class {0} shadows the property with different type from derived class {1}. " +
|
||||
"This behavior will be changed soon in favor of the property. " +
|
||||
"Please use explicit cast to {0} if you wish to preserve current behavior. " +
|
||||
"See https://youtrack.jetbrains.com/issue/KT-55017 for details", STRING, STRING);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -214,6 +214,8 @@ public interface ErrorsJvm {
|
||||
|
||||
DiagnosticFactory2<PsiElement, String, String> BACKING_FIELD_ACCESSED_DUE_TO_PROPERTY_FIELD_CONFLICT = DiagnosticFactory2.create(WARNING);
|
||||
|
||||
DiagnosticFactory2<PsiElement, String, String> BASE_CLASS_FIELD_WITH_DIFFERENT_SIGNATURE_THAN_DERIVED_CLASS_PROPERTY = DiagnosticFactory2.create(WARNING);
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
Object _initializer = new Object() {
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user