Prohibit inheritance of Java members containing FunctionN types
#KT-25855 Fixed
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. 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.builtins.jvm.JavaToKotlinClassMap
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.load.java.DeprecationCausedByFunctionN
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
||||
import org.jetbrains.kotlin.resolve.DEPRECATED_FUNCTION_KEY
|
||||
import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker
|
||||
import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult
|
||||
|
||||
object BadInheritedJavaSignaturesChecker : DeclarationChecker {
|
||||
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
|
||||
if (descriptor !is ClassDescriptor) return
|
||||
|
||||
val badSignatureOverriddenDescriptor =
|
||||
descriptor.unsubstitutedMemberScope.getContributedDescriptors().firstNotNullResult(::findFirstBadJavaSignatureOverridden)
|
||||
|
||||
if (badSignatureOverriddenDescriptor != null) {
|
||||
val reportOn =
|
||||
when (declaration) {
|
||||
is KtClass -> declaration.nameIdentifier ?: declaration.getClassOrInterfaceKeyword()
|
||||
is KtObjectDeclaration -> declaration.getObjectKeyword()
|
||||
else -> null
|
||||
} ?: declaration
|
||||
|
||||
val renderedDescriptor = DescriptorRenderer.COMPACT_WITH_SHORT_TYPES.render(badSignatureOverriddenDescriptor)
|
||||
context.trace.report(
|
||||
Errors.UNSUPPORTED.on(
|
||||
reportOn,
|
||||
"Inheritance of a Java member referencing '${JavaToKotlinClassMap.FUNCTION_N_FQ_NAME}': $renderedDescriptor"
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun findFirstBadJavaSignatureOverridden(descriptor: DeclarationDescriptor): DeclarationDescriptor? {
|
||||
if (descriptor !is CallableDescriptor) return null
|
||||
|
||||
return descriptor.overriddenDescriptors.firstOrNull {
|
||||
overridden -> overridden.getUserData(DEPRECATED_FUNCTION_KEY) is DeprecationCausedByFunctionN
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -46,7 +46,8 @@ object JvmPlatformConfigurator : PlatformConfigurator(
|
||||
JvmSyntheticApplicabilityChecker(),
|
||||
StrictfpApplicabilityChecker(),
|
||||
ExpectedActualDeclarationChecker,
|
||||
JvmAnnotationsTargetNonExistentAccessorChecker()
|
||||
JvmAnnotationsTargetNonExistentAccessorChecker(),
|
||||
BadInheritedJavaSignaturesChecker
|
||||
),
|
||||
|
||||
additionalCallCheckers = listOf(
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
|
||||
// FILE: A.java
|
||||
|
||||
import kotlin.jvm.functions.FunctionN;
|
||||
|
||||
public class A {
|
||||
public void foo(FunctionN<?> w) { }
|
||||
}
|
||||
|
||||
// FILE: main.kt
|
||||
|
||||
class <!UNSUPPORTED(Inheritance of a Java member referencing 'kotlin.jvm.functions.FunctionN': fun foo\(w: FunctionN<*>!\): Unit defined in A)!>B<!> : A()
|
||||
|
||||
fun foo() {
|
||||
<!UNSUPPORTED(Inheritance of a Java member referencing 'kotlin.jvm.functions.FunctionN': fun foo\(w: FunctionN<*>!\): Unit defined in A)!>object<!> : A() {}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package
|
||||
|
||||
public fun foo(): kotlin.Unit
|
||||
|
||||
public open class A {
|
||||
public constructor A()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open fun foo(/*0*/ w: kotlin.jvm.functions.FunctionN<*>!): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public final class B : A {
|
||||
public constructor B()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun foo(/*0*/ w: kotlin.jvm.functions.FunctionN<*>!): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
+5
@@ -2622,6 +2622,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
|
||||
runTest("compiler/testData/diagnostics/testsWithStdLib/java/functionN.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inheritedFunctionN.kt")
|
||||
public void testInheritedFunctionN() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/testsWithStdLib/java/inheritedFunctionN.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("patternCompileCallableReference.kt")
|
||||
public void testPatternCompileCallableReference() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/testsWithStdLib/java/patternCompileCallableReference.kt");
|
||||
|
||||
compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java
Generated
+5
@@ -2622,6 +2622,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
|
||||
runTest("compiler/testData/diagnostics/testsWithStdLib/java/functionN.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inheritedFunctionN.kt")
|
||||
public void testInheritedFunctionN() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/testsWithStdLib/java/inheritedFunctionN.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("patternCompileCallableReference.kt")
|
||||
public void testPatternCompileCallableReference() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/testsWithStdLib/java/patternCompileCallableReference.kt");
|
||||
|
||||
Reference in New Issue
Block a user