diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/JvmRuntimeVersionsConsistencyChecker.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/JvmRuntimeVersionsConsistencyChecker.kt index 48aa3a183b5..5aa6d22ded3 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/JvmRuntimeVersionsConsistencyChecker.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/JvmRuntimeVersionsConsistencyChecker.kt @@ -358,5 +358,5 @@ object JvmRuntimeVersionsConsistencyChecker { } private fun Manifest.getKotlinLanguageVersion(): MavenComparableVersion = - (mainAttributes.getValue(KOTLIN_VERSION_ATTRIBUTE)?.let((ApiVersion)::parse) ?: ApiVersion.KOTLIN_1_0).version + (mainAttributes.getValue(KOTLIN_VERSION_ATTRIBUTE)?.let(ApiVersion.Companion::parse) ?: ApiVersion.KOTLIN_1_0).version } 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 c94950461fc..991d6b49b59 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 @@ -2231,6 +2231,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/callableReference/correctInfoAfterArrayLikeCall.kt"); } + @Test + @TestMetadata("deprecatedCompanionReceiverInParentheses.kt") + public void testDeprecatedCompanionReceiverInParentheses() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/deprecatedCompanionReceiverInParentheses.kt"); + } + @Test @TestMetadata("ea81649_errorPropertyLHS.kt") public void testEa81649_errorPropertyLHS() 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 006721dd9d8..55fafac46d3 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 @@ -2231,6 +2231,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/callableReference/correctInfoAfterArrayLikeCall.kt"); } + @Test + @TestMetadata("deprecatedCompanionReceiverInParentheses.kt") + public void testDeprecatedCompanionReceiverInParentheses() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/deprecatedCompanionReceiverInParentheses.kt"); + } + @Test @TestMetadata("ea81649_errorPropertyLHS.kt") public void testEa81649_errorPropertyLHS() throws Exception { diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/incremental/IncrementalPackagePartProvider.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/incremental/IncrementalPackagePartProvider.kt index 170c98bd1bd..90bc4dc3cae 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/incremental/IncrementalPackagePartProvider.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/incremental/IncrementalPackagePartProvider.kt @@ -57,6 +57,6 @@ class IncrementalPackagePartProvider( } override fun getAllOptionalAnnotationClasses(): List = - moduleMappings.flatMap((JvmPackagePartProviderBase)::getAllOptionalAnnotationClasses) + + moduleMappings.flatMap(JvmPackagePartProviderBase.Companion::getAllOptionalAnnotationClasses) + parent.getAllOptionalAnnotationClasses() } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationSplitter.kt b/compiler/frontend/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationSplitter.kt index 68c6acb0e40..d7aad7c7dc3 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationSplitter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationSplitter.kt @@ -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 } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index 2a2e4682b7f..fdbd9e8dfec 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -806,6 +806,8 @@ public interface Errors { DiagnosticFactory0 RESERVED_SYNTAX_IN_CALLABLE_REFERENCE_LHS = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 PARENTHESIZED_COMPANION_LHS_DEPRECATION = DiagnosticFactory0.create(WARNING); + // Type inference DiagnosticFactory0 CANNOT_INFER_PARAMETER_TYPE = DiagnosticFactory0.create(ERROR); 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 7919f6459db..c3a18ccd3f1 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -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"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt index 7532a18a259..6f8757040b7 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt @@ -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() private val DEFAULT_CLASSIFIER_USAGE_CHECKERS = listOf( diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/CompanionInParenthesesLHSCallChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/CompanionInParenthesesLHSCallChecker.kt new file mode 100644 index 00000000000..97a53618ff4 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/CompanionInParenthesesLHSCallChecker.kt @@ -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)) + } +} diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt index 00372e14b51..4d2d3294b97 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt @@ -102,7 +102,7 @@ internal val propertiesPhase = makeIrFilePhase( description = "Move fields and accessors for properties to their classes, " + "replace calls to default property accessors with field accesses, " + "remove unused accessors and create synthetic methods for property annotations", - stickyPostconditions = setOf((PropertiesLowering)::checkNoProperties) + stickyPostconditions = setOf(PropertiesLowering.Companion::checkNoProperties) ) internal val IrClass.isGeneratedLambdaClass: Boolean diff --git a/compiler/testData/diagnostics/tests/callableReference/bound/companionObject.kt b/compiler/testData/diagnostics/tests/callableReference/bound/companionObject.kt index 58734c704a7..0d4a4d180e1 100644 --- a/compiler/testData/diagnostics/tests/callableReference/bound/companionObject.kt +++ b/compiler/testData/diagnostics/tests/callableReference/bound/companionObject.kt @@ -27,10 +27,10 @@ fun test() { val r4 = test.C.Companion::foo checkSubtype<() -> String>(r4) - val r5 = (C)::foo + val r5 = (C)::foo checkSubtype<() -> String>(r5) - val r6 = (test.C)::foo + val r6 = (test.C)::foo checkSubtype<() -> String>(r6) val c = C.Companion diff --git a/compiler/testData/diagnostics/tests/callableReference/deprecatedCompanionReceiverInParentheses.fir.kt b/compiler/testData/diagnostics/tests/callableReference/deprecatedCompanionReceiverInParentheses.fir.kt new file mode 100644 index 00000000000..9a76ef5900f --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/deprecatedCompanionReceiverInParentheses.fir.kt @@ -0,0 +1,34 @@ +// !CHECK_TYPE +// SKIP_TXT + +// FILE: lib.kt +package test.abc + +class V { + companion object +} + +val V.a: String + get() = "1" + +val V.Companion.a: Int + get() = 1 + +// FILE: main.kt +import test.abc.V +import test.abc.a +import kotlin.reflect.KProperty0 +import kotlin.reflect.KProperty1 + +fun case() { + (V)::a checkType { _>() } + (V)::a checkType { _>() } + + (test.abc.V)::a checkType { _>() } + (test.abc.V)::a checkType { _>() } + + V::a checkType { _>() } + V.Companion::a checkType { _>() } + + (V.Companion)::a checkType { _>() } +} diff --git a/compiler/testData/diagnostics/tests/callableReference/deprecatedCompanionReceiverInParentheses.kt b/compiler/testData/diagnostics/tests/callableReference/deprecatedCompanionReceiverInParentheses.kt new file mode 100644 index 00000000000..09fb51f7ed0 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/deprecatedCompanionReceiverInParentheses.kt @@ -0,0 +1,34 @@ +// !CHECK_TYPE +// SKIP_TXT + +// FILE: lib.kt +package test.abc + +class V { + companion object +} + +val V.a: String + get() = "1" + +val V.Companion.a: Int + get() = 1 + +// FILE: main.kt +import test.abc.V +import test.abc.a +import kotlin.reflect.KProperty0 +import kotlin.reflect.KProperty1 + +fun case() { + (V)::a checkType { _>() } + (V)::a checkType { _>() } + + (test.abc.V)::a checkType { _>() } + (test.abc.V)::a checkType { _>() } + + V::a checkType { _>() } + V.Companion::a checkType { _>() } + + (V.Companion)::a checkType { _>() } +} diff --git a/compiler/testData/diagnostics/tests/sourceCompatibility/noBoundCallableReferences/boundCallableReference.kt b/compiler/testData/diagnostics/tests/sourceCompatibility/noBoundCallableReferences/boundCallableReference.kt index 179f1912661..f20cc1a19e1 100644 --- a/compiler/testData/diagnostics/tests/sourceCompatibility/noBoundCallableReferences/boundCallableReference.kt +++ b/compiler/testData/diagnostics/tests/sourceCompatibility/noBoundCallableReferences/boundCallableReference.kt @@ -21,5 +21,5 @@ val ok4 = E.Entry::hashCode fun hashCode() {} val fail3 = ""::hashCode -val fail4 = (C)::hashCode +val fail4 = (C)::hashCode val fail5 = (C.Companion)::hashCode 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 f0171dccfb8..65f1629de19 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 @@ -2237,6 +2237,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/callableReference/correctInfoAfterArrayLikeCall.kt"); } + @Test + @TestMetadata("deprecatedCompanionReceiverInParentheses.kt") + public void testDeprecatedCompanionReceiverInParentheses() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/deprecatedCompanionReceiverInParentheses.kt"); + } + @Test @TestMetadata("ea81649_errorPropertyLHS.kt") public void testEa81649_errorPropertyLHS() throws Exception { diff --git a/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/resolving-callable-references/resolving-callable-references-not-used-as-arguments-to-a-call/p-1/pos/1.2.kt b/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/resolving-callable-references/resolving-callable-references-not-used-as-arguments-to-a-call/p-1/pos/1.2.kt index 91088cb34e4..b1f80638768 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/resolving-callable-references/resolving-callable-references-not-used-as-arguments-to-a-call/p-1/pos/1.2.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/resolving-callable-references/resolving-callable-references-not-used-as-arguments-to-a-call/p-1/pos/1.2.kt @@ -23,7 +23,7 @@ import libCase1.* import kotlin.text.format fun case1() { - val y2 : () ->String =(String)::format + val y2 : () ->String =(String)::format } // FILE: LibCase1.kt @@ -78,7 +78,7 @@ import libCase3.format import kotlin.text.* fun case3() { - val y1 =(String)::format + val y1 =(String)::format ")!>y1 val y2 =""::format diff --git a/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/resolving-callable-references/resolving-callable-references-not-used-as-arguments-to-a-call/p-1/pos/1.3.kt b/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/resolving-callable-references/resolving-callable-references-not-used-as-arguments-to-a-call/p-1/pos/1.3.kt index 0c96b921c44..3c9a28c939b 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/resolving-callable-references/resolving-callable-references-not-used-as-arguments-to-a-call/p-1/pos/1.3.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/overload-resolution/resolving-callable-references/resolving-callable-references-not-used-as-arguments-to-a-call/p-1/pos/1.3.kt @@ -22,12 +22,12 @@ package testsCase1 class Case() { fun case(v: V) { - val va: () -> String = (V)::a + val va: () -> String = (V)::a - val vb: () -> String = (V)::b + val vb: () -> String = (V)::b val va1: () -> String = v::a - val vb1: () -> String = (V)::b + val vb1: () -> String = (V)::b } @@ -47,4 +47,4 @@ class V { companion object { const val b: String = "1" } -} \ No newline at end of file +} diff --git a/compiler/tests/org/jetbrains/kotlin/util/KotlinVersionsTest.kt b/compiler/tests/org/jetbrains/kotlin/util/KotlinVersionsTest.kt index 8222d39eb72..03fbae4d93f 100644 --- a/compiler/tests/org/jetbrains/kotlin/util/KotlinVersionsTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/util/KotlinVersionsTest.kt @@ -61,7 +61,7 @@ class KotlinVersionsTest : KtUsefulTestCase() { versions.add( ForTestCompileRuntime.runtimeJarClassLoader().loadClass(KotlinVersion::class.qualifiedName!!) - .getDeclaredField((KotlinVersion)::CURRENT.name) + .getDeclaredField(KotlinVersion.Companion::CURRENT.name) .get(null) .toString() .toVersion("KotlinVersion.CURRENT") diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 4291c8590df..9612357d646 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -2232,6 +2232,11 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag } @Test + @TestMetadata("deprecatedCompanionReceiverInParentheses.kt") + public void testDeprecatedCompanionReceiverInParentheses() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/deprecatedCompanionReceiverInParentheses.kt"); + } + @TestMetadata("ea81649_errorPropertyLHS.kt") public void testEa81649_errorPropertyLHS() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/ea81649_errorPropertyLHS.kt");