K1: add use-site diagnostic about property shadowing by a field
Related to KT-50082
This commit is contained in:
committed by
teamcity
parent
59bafedd8a
commit
6234da4c86
+77
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.resolve.jvm.checkers
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
|
||||
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.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
|
||||
|
||||
object JvmPropertyVsFieldAmbiguityCallChecker : CallChecker {
|
||||
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
|
||||
if (!context.languageVersionSettings.supportsFeature(LanguageFeature.PreferJavaFieldOverload)) return
|
||||
val resultingDescriptor = resolvedCall.resultingDescriptor as? PropertyDescriptor ?: return
|
||||
if (!resultingDescriptor.isJavaField) return
|
||||
val ownContainingClass = resultingDescriptor.containingDeclaration as? ClassDescriptor ?: return
|
||||
|
||||
val field = DescriptorUtils.unwrapFakeOverride(resultingDescriptor)
|
||||
val fieldClassDescriptor = field.containingDeclaration as? ClassDescriptor
|
||||
if (fieldClassDescriptor === ownContainingClass) return
|
||||
|
||||
// If we have visible alternative property, we leave everything as is (see KT-54393)
|
||||
ownContainingClass.unsubstitutedMemberScope.getContributedVariables(
|
||||
resultingDescriptor.name, NoLookupLocation.FOR_ALREADY_TRACKED
|
||||
).forEach { alternativePropertyDescriptor ->
|
||||
if (alternativePropertyDescriptor !== resultingDescriptor) {
|
||||
val hasLateInit = alternativePropertyDescriptor.isLateInit
|
||||
if (!hasLateInit &&
|
||||
alternativePropertyDescriptor.getter?.isDefault != false &&
|
||||
alternativePropertyDescriptor.setter?.isDefault != false &&
|
||||
alternativePropertyDescriptor.modality == Modality.FINAL
|
||||
) return@forEach
|
||||
val propertyClassDescriptor =
|
||||
DescriptorUtils.unwrapFakeOverride(alternativePropertyDescriptor).containingDeclaration as? ClassDescriptor
|
||||
if (fieldClassDescriptor != null && propertyClassDescriptor != null &&
|
||||
DescriptorUtils.isSubclass(fieldClassDescriptor, propertyClassDescriptor)
|
||||
) return@forEach
|
||||
if (DescriptorVisibilities.isVisible(
|
||||
/* receiver = */ resolvedCall.dispatchReceiver,
|
||||
/* what = */ alternativePropertyDescriptor,
|
||||
/* from = */ context.scope.ownerDescriptor,
|
||||
/* useSpecialRulesForPrivateSealedConstructors = */ false
|
||||
)
|
||||
) {
|
||||
val factory = when {
|
||||
alternativePropertyDescriptor.getter?.isDefault == false ->
|
||||
ErrorsJvm.BASE_CLASS_FIELD_SHADOWS_DERIVED_CLASS_PROPERTY
|
||||
hasLateInit || alternativePropertyDescriptor.setter?.isDefault == false ->
|
||||
ErrorsJvm.BACKING_FIELD_ACCESSED_DUE_TO_PROPERTY_FIELD_CONFLICT
|
||||
else ->
|
||||
ErrorsJvm.BASE_CLASS_FIELD_MAY_SHADOW_DERIVED_CLASS_PROPERTY
|
||||
}
|
||||
context.trace.report(
|
||||
factory.on(
|
||||
reportOn,
|
||||
fieldClassDescriptor?.fqNameSafe?.asString() ?: "unknown class",
|
||||
propertyClassDescriptor?.fqNameSafe?.asString() ?: "unknown class"
|
||||
)
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -237,6 +237,25 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
|
||||
|
||||
MAP.put(REDUNDANT_REPEATABLE_ANNOTATION,
|
||||
"Please, remove the ''{0}'' annotation, as ''{1}'' is already enough", TO_STRING, TO_STRING);
|
||||
|
||||
MAP.put(BASE_CLASS_FIELD_SHADOWS_DERIVED_CLASS_PROPERTY,
|
||||
"Now field from base class {0} shadows the property with custom getter 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);
|
||||
|
||||
MAP.put(BASE_CLASS_FIELD_MAY_SHADOW_DERIVED_CLASS_PROPERTY,
|
||||
"Field from base class {0} may shadow the open property 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);
|
||||
|
||||
MAP.put(BACKING_FIELD_ACCESSED_DUE_TO_PROPERTY_FIELD_CONFLICT,
|
||||
"Property backing field in derived class {1} is accessed instead of the property itself. " +
|
||||
"This happens because of field with the same name in the base class {0}. " +
|
||||
"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,12 @@ public interface ErrorsJvm {
|
||||
|
||||
DiagnosticFactory2<KtAnnotationEntry, FqName, FqName> REDUNDANT_REPEATABLE_ANNOTATION = DiagnosticFactory2.create(WARNING);
|
||||
|
||||
DiagnosticFactory2<PsiElement, String, String> BASE_CLASS_FIELD_SHADOWS_DERIVED_CLASS_PROPERTY = DiagnosticFactory2.create(WARNING);
|
||||
|
||||
DiagnosticFactory2<PsiElement, String, String> BASE_CLASS_FIELD_MAY_SHADOW_DERIVED_CLASS_PROPERTY = DiagnosticFactory2.create(WARNING);
|
||||
|
||||
DiagnosticFactory2<PsiElement, String, String> BACKING_FIELD_ACCESSED_DUE_TO_PROPERTY_FIELD_CONFLICT = DiagnosticFactory2.create(WARNING);
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
Object _initializer = new Object() {
|
||||
{
|
||||
|
||||
+2
-1
@@ -64,7 +64,8 @@ object JvmPlatformConfigurator : PlatformConfiguratorBase(
|
||||
EnumDeclaringClassDeprecationChecker,
|
||||
UpperBoundViolatedInTypealiasConstructorChecker,
|
||||
JvmSyntheticAssignmentChecker,
|
||||
LateinitIntrinsicApplicabilityChecker(isWarningInPre19 = false)
|
||||
LateinitIntrinsicApplicabilityChecker(isWarningInPre19 = false),
|
||||
JvmPropertyVsFieldAmbiguityCallChecker,
|
||||
),
|
||||
|
||||
additionalTypeCheckers = listOf(
|
||||
|
||||
Reference in New Issue
Block a user