From b77e5a21ecc64b085001e54b00b2f8879dc1088b Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 22 Apr 2020 18:21:49 +0200 Subject: [PATCH] Report error if fun interface method has JVM name getFunctionDelegate This is needed so that it wouldn't clash with the corresponding member from the kotlin/jvm/internal/FunctionAdapter interface, which all fun interface wrappers will implement to get proper equals/hashCode. The workaround is to rename the fun interface method. #KT-33455 --- ...irOldFrontendDiagnosticsTestGenerated.java | 5 ++ .../FunctionDelegateMemberNameClashChecker.kt | 29 +++++++++++ .../diagnostics/DefaultErrorMessagesJvm.java | 3 ++ .../resolve/jvm/diagnostics/ErrorsJvm.java | 2 + .../jvm/platform/JvmPlatformConfigurator.kt | 3 +- .../functionDelegateClashOnJvm.fir.kt | 32 ++++++++++++ .../functionDelegateClashOnJvm.kt | 32 ++++++++++++ .../functionDelegateClashOnJvm.txt | 50 +++++++++++++++++++ .../checkers/DiagnosticsTestGenerated.java | 5 ++ .../DiagnosticsUsingJavacTestGenerated.java | 5 ++ 10 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/FunctionDelegateMemberNameClashChecker.kt create mode 100644 compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.fir.kt create mode 100644 compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.kt create mode 100644 compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.txt diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java index b4d197bdead..167f2dc9bb3 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -7944,6 +7944,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/funInterface/funIsNotInheritedFromBaseInterface.kt"); } + @TestMetadata("functionDelegateClashOnJvm.kt") + public void testFunctionDelegateClashOnJvm() throws Exception { + runTest("compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.kt"); + } + @TestMetadata("genericSubstitutionForFunInterface.kt") public void testGenericSubstitutionForFunInterface() throws Exception { runTest("compiler/testData/diagnostics/tests/funInterface/genericSubstitutionForFunInterface.kt"); diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/FunctionDelegateMemberNameClashChecker.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/FunctionDelegateMemberNameClashChecker.kt new file mode 100644 index 00000000000..878fc645547 --- /dev/null +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/FunctionDelegateMemberNameClashChecker.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2010-2020 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 org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.psi.KtDeclaration +import org.jetbrains.kotlin.psi.KtNamedDeclaration +import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker +import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext +import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm + +object FunctionDelegateMemberNameClashChecker : DeclarationChecker { + override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) { + if (descriptor !is CallableMemberDescriptor) return + val container = descriptor.containingDeclaration + if (container !is ClassDescriptor || !container.isFun) return + if (descriptor.extensionReceiverParameter != null || descriptor.valueParameters.isNotEmpty()) return + + if (descriptor is FunctionDescriptor && descriptor.name.asString() == "getFunctionDelegate" || + descriptor is PropertyDescriptor && descriptor.name.asString() == "functionDelegate" + ) { + val reportOn = (declaration as? KtNamedDeclaration)?.nameIdentifier ?: declaration + context.trace.report(ErrorsJvm.FUNCTION_DELEGATE_MEMBER_NAME_CLASH.on(reportOn)) + } + } +} diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/DefaultErrorMessagesJvm.java b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/DefaultErrorMessagesJvm.java index 0c335a60bdb..15271da50e8 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/DefaultErrorMessagesJvm.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/DefaultErrorMessagesJvm.java @@ -156,6 +156,9 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension { String spreadOnPolymorphicSignature = "Spread operator is prohibited for arguments to signature-polymorphic calls"; MAP.put(SPREAD_ON_SIGNATURE_POLYMORPHIC_CALL, spreadOnPolymorphicSignature); MAP.put(SPREAD_ON_SIGNATURE_POLYMORPHIC_CALL_ERROR, spreadOnPolymorphicSignature); + + MAP.put(FUNCTION_DELEGATE_MEMBER_NAME_CLASH, + "Functional interface member cannot have this name on JVM because it clashes with an internal member getFunctionDelegate"); } @NotNull diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/ErrorsJvm.java b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/ErrorsJvm.java index a2074ccdc55..367d1049d68 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/ErrorsJvm.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/ErrorsJvm.java @@ -137,6 +137,8 @@ public interface ErrorsJvm { DiagnosticFactory0 SPREAD_ON_SIGNATURE_POLYMORPHIC_CALL = DiagnosticFactory0.create(WARNING); DiagnosticFactory0 SPREAD_ON_SIGNATURE_POLYMORPHIC_CALL_ERROR = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 FUNCTION_DELEGATE_MEMBER_NAME_CLASH = DiagnosticFactory0.create(ERROR); + @SuppressWarnings("UnusedDeclaration") Object _initializer = new Object() { { diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt index 025c8792cda..c0c0b601684 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt @@ -39,7 +39,8 @@ object JvmPlatformConfigurator : PlatformConfiguratorBase( BadInheritedJavaSignaturesChecker, JvmMultifileClassStateChecker, SynchronizedOnInlineMethodChecker, - DefaultCheckerInTailrec + DefaultCheckerInTailrec, + FunctionDelegateMemberNameClashChecker, ), additionalCallCheckers = listOf( diff --git a/compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.fir.kt b/compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.fir.kt new file mode 100644 index 00000000000..cb396db5560 --- /dev/null +++ b/compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.fir.kt @@ -0,0 +1,32 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +fun interface F1 { + val functionDelegate: Function<*>? get() = null + fun invoke() +} + +fun interface F2 { + fun getFunctionDelegate(): Function<*>? = null + fun invoke() +} + +fun interface F3 { + val getFunctionDelegate: Function<*>? get() = null + fun invoke() +} + +fun interface F4 { + fun functionDelegate(): Function<*>? = null + fun invoke() +} + +fun interface F5 { + val functionDelegate: Any? get() = null + fun invoke() +} + +fun interface F6 { + val String.functionDelegate: Function<*>? get() = null + fun getFunctionDelegate(x: Any?): Function<*>? = null + fun invoke() +} diff --git a/compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.kt b/compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.kt new file mode 100644 index 00000000000..32bd0683588 --- /dev/null +++ b/compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.kt @@ -0,0 +1,32 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +fun interface F1 { + val functionDelegate: Function<*>? get() = null + fun invoke() +} + +fun interface F2 { + fun getFunctionDelegate(): Function<*>? = null + fun invoke() +} + +fun interface F3 { + val getFunctionDelegate: Function<*>? get() = null + fun invoke() +} + +fun interface F4 { + fun functionDelegate(): Function<*>? = null + fun invoke() +} + +fun interface F5 { + val functionDelegate: Any? get() = null + fun invoke() +} + +fun interface F6 { + val String.functionDelegate: Function<*>? get() = null + fun getFunctionDelegate(x: Any?): Function<*>? = null + fun invoke() +} diff --git a/compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.txt b/compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.txt new file mode 100644 index 00000000000..f20c54981a2 --- /dev/null +++ b/compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.txt @@ -0,0 +1,50 @@ +package + +public fun interface F1 { + public open val functionDelegate: kotlin.Function<*>? + 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 abstract fun invoke(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public fun interface F2 { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open fun getFunctionDelegate(): kotlin.Function<*>? + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public abstract fun invoke(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public fun interface F3 { + public open val getFunctionDelegate: kotlin.Function<*>? + 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 abstract fun invoke(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public fun interface F4 { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open fun functionDelegate(): kotlin.Function<*>? + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public abstract fun invoke(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public fun interface F5 { + public open val functionDelegate: kotlin.Any? + 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 abstract fun invoke(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public fun interface F6 { + public open val kotlin.String.functionDelegate: kotlin.Function<*>? + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open fun getFunctionDelegate(/*0*/ x: kotlin.Any?): kotlin.Function<*>? + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public abstract fun invoke(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index c5b5b72a3ce..0eb87bf77fc 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -7951,6 +7951,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali runTest("compiler/testData/diagnostics/tests/funInterface/funIsNotInheritedFromBaseInterface.kt"); } + @TestMetadata("functionDelegateClashOnJvm.kt") + public void testFunctionDelegateClashOnJvm() throws Exception { + runTest("compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.kt"); + } + @TestMetadata("genericSubstitutionForFunInterface.kt") public void testGenericSubstitutionForFunInterface() throws Exception { runTest("compiler/testData/diagnostics/tests/funInterface/genericSubstitutionForFunInterface.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 75c4261887e..d23dd13d52d 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -7946,6 +7946,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/funInterface/funIsNotInheritedFromBaseInterface.kt"); } + @TestMetadata("functionDelegateClashOnJvm.kt") + public void testFunctionDelegateClashOnJvm() throws Exception { + runTest("compiler/testData/diagnostics/tests/funInterface/functionDelegateClashOnJvm.kt"); + } + @TestMetadata("genericSubstitutionForFunInterface.kt") public void testGenericSubstitutionForFunInterface() throws Exception { runTest("compiler/testData/diagnostics/tests/funInterface/genericSubstitutionForFunInterface.kt");