From cb57444e14a1e6757613cfdddc67a9cac84161fc Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Wed, 5 Sep 2018 18:04:05 +0300 Subject: [PATCH] Forbid numbered KSuspendFunction interfaces usage as supertypes #KT-24853 Fixed --- .../src/org/jetbrains/kotlin/diagnostics/Errors.java | 1 + .../kotlin/diagnostics/rendering/DefaultErrorMessages.java | 1 + .../src/org/jetbrains/kotlin/resolve/BodyResolver.java | 3 +++ .../coroutines/kSuspendFunctionAsSupertype.kt | 5 +++++ .../kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java | 5 +++++ .../javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java | 5 +++++ .../src/org/jetbrains/kotlin/builtins/functionTypes.kt | 3 +++ 7 files changed, 23 insertions(+) create mode 100644 compiler/testData/diagnostics/testsWithStdLib/coroutines/kSuspendFunctionAsSupertype.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index 325927f6e62..fa80e63e671 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -291,6 +291,7 @@ public interface Errors { DiagnosticFactory0 SUPERTYPE_NOT_A_CLASS_OR_INTERFACE = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 SUPERTYPE_IS_EXTENSION_FUNCTION_TYPE = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 SUPERTYPE_IS_SUSPEND_FUNCTION_TYPE = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 SUPERTYPE_IS_KSUSPEND_FUNCTION_TYPE = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 MANY_CLASSES_IN_SUPERTYPE_LIST = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 SUPERTYPE_APPEARS_TWICE = 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 08e7dfbccc5..d1e3b361122 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -573,6 +573,7 @@ public class DefaultErrorMessages { MAP.put(SUPERTYPE_IS_EXTENSION_FUNCTION_TYPE, "Extension function type is not allowed as supertypes"); MAP.put(SUPERTYPE_INITIALIZED_IN_INTERFACE, "Interfaces cannot initialize supertypes"); MAP.put(SUPERTYPE_IS_SUSPEND_FUNCTION_TYPE, "Suspend function type is not allowed as supertypes"); + MAP.put(SUPERTYPE_IS_KSUSPEND_FUNCTION_TYPE, "KSuspendFunctionN interfaces are not allowed as supertypes"); MAP.put(CLASS_IN_SUPERTYPE_FOR_ENUM, "Enum class cannot inherit from classes"); MAP.put(CONSTRUCTOR_IN_INTERFACE, "An interface may not have a constructor"); MAP.put(METHOD_OF_ANY_IMPLEMENTED_IN_INTERFACE, "An interface may not implement a method of 'Any'"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/BodyResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/BodyResolver.java index e7cff04b914..ef8e110a91b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/BodyResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/BodyResolver.java @@ -559,6 +559,9 @@ public class BodyResolver { else if (FunctionTypesKt.isSuspendFunctionType(supertype)) { trace.report(SUPERTYPE_IS_SUSPEND_FUNCTION_TYPE.on(typeReference)); } + else if (FunctionTypesKt.isKSuspendFunctionType(supertype)) { + trace.report(SUPERTYPE_IS_KSUSPEND_FUNCTION_TYPE.on(typeReference)); + } if (classDescriptor.getKind() != ClassKind.INTERFACE) { if (supertypeOwner.getKind() == ClassKind.ENUM_CLASS) { diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/kSuspendFunctionAsSupertype.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/kSuspendFunctionAsSupertype.kt new file mode 100644 index 00000000000..532c9f662e8 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/kSuspendFunctionAsSupertype.kt @@ -0,0 +1,5 @@ +// SKIP_TXT + +import kotlin.reflect.* + +class A: KSuspendFunction0 {} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java index b86188806f9..1f45d0368fb 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java @@ -1468,6 +1468,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/irrelevantSuspendDeclarations.kt"); } + @TestMetadata("kSuspendFunctionAsSupertype.kt") + public void testKSuspendFunctionAsSupertype() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/kSuspendFunctionAsSupertype.kt"); + } + @TestMetadata("kt18292.kt") public void testKt18292_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/diagnostics/testsWithStdLib/coroutines/kt18292.kt", "kotlin.coroutines.experimental"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java index cdb624420a6..36e4de4416c 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java @@ -1468,6 +1468,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/irrelevantSuspendDeclarations.kt"); } + @TestMetadata("kSuspendFunctionAsSupertype.kt") + public void testKSuspendFunctionAsSupertype() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/kSuspendFunctionAsSupertype.kt"); + } + @TestMetadata("kt18292.kt") public void testKt18292_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/diagnostics/testsWithStdLib/coroutines/kt18292.kt", "kotlin.coroutines.experimental"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/functionTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/functionTypes.kt index 7c704524dc5..aa216ab67a3 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/functionTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/functionTypes.kt @@ -58,6 +58,9 @@ val KotlinType.isFunctionType: Boolean val KotlinType.isSuspendFunctionType: Boolean get() = constructor.declarationDescriptor?.getFunctionalClassKind() == FunctionClassDescriptor.Kind.SuspendFunction +val KotlinType.isKSuspendFunctionType: Boolean + get() = constructor.declarationDescriptor?.getFunctionalClassKind() == FunctionClassDescriptor.Kind.KSuspendFunction + val KotlinType.isFunctionOrSuspendFunctionType: Boolean get() = isFunctionType || isSuspendFunctionType