Prohibit using suspend functions as SAM in fun interfaces
#KT-40978 Fixed
This commit is contained in:
@@ -376,6 +376,7 @@ public interface Errors {
|
||||
DiagnosticFactory0<PsiElement> FUN_INTERFACE_ABSTRACT_METHOD_WITH_TYPE_PARAMETERS = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<PsiElement> FUN_INTERFACE_ABSTRACT_METHOD_WITH_DEFAULT_VALUE = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<PsiElement> FUN_INTERFACE_CONSTRUCTOR_REFERENCE = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<PsiElement> FUN_INTERFACE_WITH_SUSPEND_FUNCTION = DiagnosticFactory0.create(ERROR);
|
||||
|
||||
// Secondary constructors
|
||||
|
||||
|
||||
+1
@@ -718,6 +718,7 @@ public class DefaultErrorMessages {
|
||||
MAP.put(FUN_INTERFACE_ABSTRACT_METHOD_WITH_TYPE_PARAMETERS, "Single abstract member cannot declare type parameters");
|
||||
MAP.put(FUN_INTERFACE_ABSTRACT_METHOD_WITH_DEFAULT_VALUE, "Single abstract member cannot declare default values");
|
||||
MAP.put(FUN_INTERFACE_CONSTRUCTOR_REFERENCE, "Functional interface constructor references are prohibited");
|
||||
MAP.put(FUN_INTERFACE_WITH_SUSPEND_FUNCTION, "'suspend' modifier is not allowed on a single abstract member");
|
||||
|
||||
MAP.put(VARIANCE_ON_TYPE_PARAMETER_NOT_ALLOWED, "Variance annotations are only allowed for type parameters of classes and interfaces");
|
||||
MAP.put(BOUND_ON_TYPE_ALIAS_PARAMETER_NOT_ALLOWED, "Bounds are not allowed on type alias parameters");
|
||||
|
||||
+16
-2
@@ -6,9 +6,16 @@
|
||||
package org.jetbrains.kotlin.resolve.checkers
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtClass
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue
|
||||
import org.jetbrains.kotlin.resolve.sam.getAbstractMembers
|
||||
import org.jetbrains.kotlin.resolve.source.getPsi
|
||||
@@ -52,6 +59,13 @@ class FunInterfaceDeclarationChecker : DeclarationChecker {
|
||||
context: DeclarationCheckerContext,
|
||||
) {
|
||||
val ktFunction = abstractMember.source.getPsi() as? KtNamedFunction
|
||||
|
||||
if (abstractMember.isSuspend) {
|
||||
val reportOn = ktFunction?.modifierList?.getModifier(KtTokens.SUSPEND_KEYWORD) ?: funInterfaceKeyword
|
||||
context.trace.report(Errors.FUN_INTERFACE_WITH_SUSPEND_FUNCTION.on(reportOn))
|
||||
return
|
||||
}
|
||||
|
||||
if (abstractMember.typeParameters.isNotEmpty()) {
|
||||
val reportOn = ktFunction?.typeParameterList ?: ktFunction?.funKeyword ?: funInterfaceKeyword
|
||||
context.trace.report(Errors.FUN_INTERFACE_ABSTRACT_METHOD_WITH_TYPE_PARAMETERS.on(reportOn))
|
||||
|
||||
+1
@@ -6,6 +6,7 @@ import helpers.*
|
||||
import kotlin.coroutines.*
|
||||
|
||||
fun interface SuspendRunnable {
|
||||
@Suppress("FUN_INTERFACE_WITH_SUSPEND_FUNCTION")
|
||||
suspend fun invoke()
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -71,7 +71,7 @@ interface BaseWithGeneric {
|
||||
fun interface GoodExtensionGeneric : GoodGeneric<String>
|
||||
|
||||
fun interface GoodSuspend {
|
||||
suspend fun invoke()
|
||||
<!FUN_INTERFACE_WITH_SUSPEND_FUNCTION!>suspend<!> fun invoke()
|
||||
}
|
||||
|
||||
class WithNestedFun<K> {
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// !LANGUAGE: +NewInference +SamConversionForKotlinFunctions +SamConversionPerArgument +FunctionalInterfaceConversion
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER -NOTHING_TO_INLINE
|
||||
|
||||
fun interface SuspendRunnable {
|
||||
suspend fun invoke()
|
||||
}
|
||||
|
||||
fun run(r: SuspendRunnable) {}
|
||||
|
||||
suspend fun bar() {}
|
||||
|
||||
fun test() {
|
||||
run(::bar)
|
||||
}
|
||||
+1
-2
@@ -1,9 +1,8 @@
|
||||
// FIR_IDENTICAL
|
||||
// !LANGUAGE: +NewInference +SamConversionForKotlinFunctions +SamConversionPerArgument +FunctionalInterfaceConversion
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER -NOTHING_TO_INLINE
|
||||
|
||||
fun interface SuspendRunnable {
|
||||
suspend fun invoke()
|
||||
<!FUN_INTERFACE_WITH_SUSPEND_FUNCTION!>suspend<!> fun invoke()
|
||||
}
|
||||
|
||||
fun run(r: SuspendRunnable) {}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
|
||||
fun interface SuspendRunnable {
|
||||
suspend fun invoke()
|
||||
<!FUN_INTERFACE_WITH_SUSPEND_FUNCTION!>suspend<!> fun invoke()
|
||||
}
|
||||
|
||||
fun foo(s: SuspendRunnable) {}
|
||||
|
||||
Vendored
+1
-1
@@ -2,7 +2,7 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
|
||||
|
||||
fun interface SuspendRunnable {
|
||||
suspend fun run()
|
||||
<!FUN_INTERFACE_WITH_SUSPEND_FUNCTION!>suspend<!> fun run()
|
||||
}
|
||||
|
||||
object Test1 {
|
||||
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// !LANGUAGE: +SuspendConversion
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
|
||||
fun interface SuspendRunnable {
|
||||
suspend fun invoke()
|
||||
}
|
||||
|
||||
fun foo1(s: SuspendRunnable) {}
|
||||
fun bar1() {}
|
||||
|
||||
fun bar2(s: String = ""): Int = 0
|
||||
|
||||
fun bar3() {}
|
||||
suspend fun bar3(s: String = ""): Int = 0
|
||||
|
||||
fun test() {
|
||||
foo1(::bar1)
|
||||
foo1(::bar2)
|
||||
|
||||
foo1(::bar3) // Should be ambiguity
|
||||
}
|
||||
Vendored
+1
-2
@@ -1,9 +1,8 @@
|
||||
// FIR_IDENTICAL
|
||||
// !LANGUAGE: +SuspendConversion
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
|
||||
fun interface SuspendRunnable {
|
||||
suspend fun invoke()
|
||||
<!FUN_INTERFACE_WITH_SUSPEND_FUNCTION!>suspend<!> fun invoke()
|
||||
}
|
||||
|
||||
fun foo1(s: SuspendRunnable) {}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
|
||||
|
||||
fun interface SuspendRunnable {
|
||||
suspend fun run()
|
||||
<!FUN_INTERFACE_WITH_SUSPEND_FUNCTION!>suspend<!> fun run()
|
||||
}
|
||||
|
||||
fun foo(r: SuspendRunnable) {}
|
||||
|
||||
Reference in New Issue
Block a user