Deprecate (V)::a reference resolution to companion in FE 1.0

^KT-45315 Fixed
This commit is contained in:
Denis.Zharkov
2021-06-17 16:20:11 +03:00
committed by TeamCityServer
parent 46b297477c
commit 7645663d12
19 changed files with 152 additions and 15 deletions
@@ -87,7 +87,7 @@ class AnnotationSplitter(
val (targeted, other) = this@AnnotationSplitter.splitAnnotations()
if (target != null) {
targeted[target]?.let((Annotations)::create) ?: Annotations.EMPTY
targeted[target]?.let(Annotations.Companion::create) ?: Annotations.EMPTY
} else {
other
}
@@ -806,6 +806,8 @@ public interface Errors {
DiagnosticFactory0<KtExpression> RESERVED_SYNTAX_IN_CALLABLE_REFERENCE_LHS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtParenthesizedExpression> PARENTHESIZED_COMPANION_LHS_DEPRECATION = DiagnosticFactory0.create(WARNING);
// Type inference
DiagnosticFactory0<KtParameter> CANNOT_INFER_PARAMETER_TYPE = DiagnosticFactory0.create(ERROR);
@@ -444,6 +444,8 @@ public class DefaultErrorMessages {
MAP.put(RESERVED_SYNTAX_IN_CALLABLE_REFERENCE_LHS, "Left-hand side of callable reference matches expression syntax reserved for future releases");
MAP.put(PARENTHESIZED_COMPANION_LHS_DEPRECATION, "Access to companion object through parenthesized class name is deprecated. Please, add explicit Companion qualifier.");
MAP.put(LOCAL_EXTENSION_PROPERTY, "Local extension properties are not allowed");
MAP.put(LOCAL_VARIABLE_WITH_GETTER, "Local variables are not allowed to have getters");
MAP.put(LOCAL_VARIABLE_WITH_SETTER, "Local variables are not allowed to have setters");
@@ -60,7 +60,7 @@ private val DEFAULT_CALL_CHECKERS = listOf(
MissingDependencySupertypeChecker.ForCalls, AbstractClassInstantiationChecker, SuspendConversionCallChecker,
UnitConversionCallChecker, FunInterfaceConstructorReferenceChecker, NullableExtensionOperatorWithSafeCallChecker,
ReferencingToUnderscoreNamedParameterOfCatchBlockChecker, VarargWrongExecutionOrderChecker, SelfCallInNestedObjectConstructorChecker,
NewSchemeOfIntegerOperatorResolutionChecker, EnumEntryVsCompanionPriorityCallChecker,
NewSchemeOfIntegerOperatorResolutionChecker, EnumEntryVsCompanionPriorityCallChecker, CompanionInParenthesesLHSCallChecker,
)
private val DEFAULT_TYPE_CHECKERS = emptyList<AdditionalTypeChecker>()
private val DEFAULT_CLASSIFIER_USAGE_CHECKERS = listOf(
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2021 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.calls.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtCallableReferenceExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtParenthesizedExpression
import org.jetbrains.kotlin.psi.KtReferenceExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
/**
* Deprecate callable references in a form of (SomeClass)::name when SomeClass has a companion
* and `(SomeClass)` is being used just like a value reference to the companion
*/
object CompanionInParenthesesLHSCallChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
val callableReference = resolvedCall.call.callElement.parent as? KtCallableReferenceExpression ?: return
val parenthesizedExpression = callableReference.lhs as? KtParenthesizedExpression ?: return
val unwrappedLhs = parenthesizedExpression.expression ?: return
val expressionReceiver = resolvedCall.call.explicitReceiver as? ExpressionReceiver ?: return
val referencedClass = expressionReceiver.type.constructor.declarationDescriptor as? ClassDescriptor ?: return
if (!referencedClass.isCompanionObject) return
// We should also consider cases like (package.MyClassWithCompanion)::foo
val simpleReference =
((unwrappedLhs as? KtDotQualifiedExpression)?.selectorExpression ?: unwrappedLhs) as? KtReferenceExpression
?: return
if (context.trace.bindingContext[BindingContext.SHORT_REFERENCE_TO_COMPANION_OBJECT, simpleReference] == null) return
context.trace.report(Errors.PARENTHESIZED_COMPANION_LHS_DEPRECATION.on(parenthesizedExpression))
}
}