Add CallChecker prohibiting non-local suspension calls
Effectively suspension point is local <=> return to relevant lambda is allowed in place
This commit is contained in:
@@ -801,6 +801,9 @@ public interface Errors {
|
|||||||
DiagnosticFactory0<PsiElement> NON_LOCAL_RETURN_IN_DISABLED_INLINE = DiagnosticFactory0.create(ERROR);
|
DiagnosticFactory0<PsiElement> NON_LOCAL_RETURN_IN_DISABLED_INLINE = DiagnosticFactory0.create(ERROR);
|
||||||
|
|
||||||
|
|
||||||
|
DiagnosticFactory0<KtElement> NON_LOCAL_SUSPENSION_POINT = DiagnosticFactory0.create(ERROR);
|
||||||
|
|
||||||
|
|
||||||
// Error sets
|
// Error sets
|
||||||
ImmutableSet<? extends DiagnosticFactory<?>> UNRESOLVED_REFERENCE_DIAGNOSTICS = ImmutableSet.of(
|
ImmutableSet<? extends DiagnosticFactory<?>> UNRESOLVED_REFERENCE_DIAGNOSTICS = ImmutableSet.of(
|
||||||
UNRESOLVED_REFERENCE, NAMED_PARAMETER_NOT_FOUND, UNRESOLVED_REFERENCE_WRONG_RECEIVER);
|
UNRESOLVED_REFERENCE, NAMED_PARAMETER_NOT_FOUND, UNRESOLVED_REFERENCE_WRONG_RECEIVER);
|
||||||
|
|||||||
+1
@@ -753,6 +753,7 @@ public class DefaultErrorMessages {
|
|||||||
MAP.put(NON_LOCAL_RETURN_NOT_ALLOWED, "Can''t inline ''{0}'' here: it may contain non-local returns. Add ''crossinline'' modifier to parameter declaration ''{0}''", ELEMENT_TEXT);
|
MAP.put(NON_LOCAL_RETURN_NOT_ALLOWED, "Can''t inline ''{0}'' here: it may contain non-local returns. Add ''crossinline'' modifier to parameter declaration ''{0}''", ELEMENT_TEXT);
|
||||||
MAP.put(INLINE_CALL_CYCLE, "The ''{0}'' invocation is a part of inline cycle", NAME);
|
MAP.put(INLINE_CALL_CYCLE, "The ''{0}'' invocation is a part of inline cycle", NAME);
|
||||||
MAP.put(NON_LOCAL_RETURN_IN_DISABLED_INLINE, "Non-local returns are not allowed with inlining disabled");
|
MAP.put(NON_LOCAL_RETURN_IN_DISABLED_INLINE, "Non-local returns are not allowed with inlining disabled");
|
||||||
|
MAP.put(NON_LOCAL_SUSPENSION_POINT, "Suspension functions can be called only within coroutine body");
|
||||||
|
|
||||||
MAP.setImmutable();
|
MAP.setImmutable();
|
||||||
|
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ private val DEFAULT_DECLARATION_CHECKERS = listOf(
|
|||||||
|
|
||||||
private val DEFAULT_CALL_CHECKERS = listOf(CapturingInClosureChecker(), InlineCheckerWrapper(), ReifiedTypeParameterSubstitutionChecker(),
|
private val DEFAULT_CALL_CHECKERS = listOf(CapturingInClosureChecker(), InlineCheckerWrapper(), ReifiedTypeParameterSubstitutionChecker(),
|
||||||
SafeCallChecker(), InvokeConventionChecker(), CallReturnsArrayOfNothingChecker(),
|
SafeCallChecker(), InvokeConventionChecker(), CallReturnsArrayOfNothingChecker(),
|
||||||
ConstructorHeaderCallChecker, ProtectedConstructorCallChecker)
|
ConstructorHeaderCallChecker, ProtectedConstructorCallChecker, CoroutineSuspendCallChecker)
|
||||||
private val DEFAULT_TYPE_CHECKERS = emptyList<AdditionalTypeChecker>()
|
private val DEFAULT_TYPE_CHECKERS = emptyList<AdditionalTypeChecker>()
|
||||||
private val DEFAULT_VALIDATORS = listOf(DeprecatedSymbolValidator(), OperatorValidator(), InfixValidator())
|
private val DEFAULT_VALIDATORS = listOf(DeprecatedSymbolValidator(), OperatorValidator(), InfixValidator())
|
||||||
|
|
||||||
|
|||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.resolve.calls.checkers
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||||
|
import org.jetbrains.kotlin.diagnostics.Errors
|
||||||
|
import org.jetbrains.kotlin.psi.KtExpression
|
||||||
|
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
|
||||||
|
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||||
|
import org.jetbrains.kotlin.resolve.coroutine.CoroutineReceiverValue
|
||||||
|
import org.jetbrains.kotlin.resolve.inline.InlineUtil
|
||||||
|
|
||||||
|
object CoroutineSuspendCallChecker : CallChecker {
|
||||||
|
override fun check(resolvedCall: ResolvedCall<*>, context: BasicCallResolutionContext) {
|
||||||
|
val descriptor = resolvedCall.candidateDescriptor as? FunctionDescriptor ?: return
|
||||||
|
if (!descriptor.isSuspend || descriptor.initialSignatureDescriptor == null) return
|
||||||
|
|
||||||
|
val dispatchReceiverOwner = (resolvedCall.dispatchReceiver as? CoroutineReceiverValue)?.declarationDescriptor ?: return
|
||||||
|
val callElement = resolvedCall.call.callElement as KtExpression
|
||||||
|
|
||||||
|
if (!InlineUtil.checkNonLocalReturnUsage(dispatchReceiverOwner, callElement, context.trace)) {
|
||||||
|
context.trace.report(Errors.NON_LOCAL_SUSPENSION_POINT.on(resolvedCall.call.calleeExpression ?: callElement))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||||
|
// !CHECK_TYPE
|
||||||
|
class Controller {
|
||||||
|
suspend fun suspendHere(a: String, x: Continuation<Int>) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class A {
|
||||||
|
suspend fun suspendHere(a: Int, x: Continuation<Int>) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun builder(coroutine c: Controller.() -> Continuation<Unit>) {}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
builder {
|
||||||
|
suspendHere("")
|
||||||
|
|
||||||
|
with(A()) {
|
||||||
|
suspendHere("")
|
||||||
|
// This test checks that suspending functions
|
||||||
|
// that are not from coroutine controller can't be called by suspending convention
|
||||||
|
suspendHere(1<!NO_VALUE_FOR_PARAMETER!>)<!>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
package
|
||||||
|
|
||||||
|
public fun builder(/*0*/ coroutine c: Controller.() -> kotlin.coroutines.Continuation<kotlin.Unit>): kotlin.Unit
|
||||||
|
public fun test(): kotlin.Unit
|
||||||
|
|
||||||
|
public final class A {
|
||||||
|
public constructor A()
|
||||||
|
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 final suspend fun suspendHere(/*0*/ a: kotlin.Int, /*1*/ x: kotlin.coroutines.Continuation<kotlin.Int>): kotlin.Unit
|
||||||
|
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||||
|
}
|
||||||
|
|
||||||
|
public final class Controller {
|
||||||
|
public constructor Controller()
|
||||||
|
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 final suspend fun suspendHere(/*0*/ a: kotlin.String, /*1*/ x: kotlin.coroutines.Continuation<kotlin.Int>): kotlin.Unit
|
||||||
|
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||||
|
|
||||||
|
class Controller<T> {
|
||||||
|
suspend fun suspendHere(x: Continuation<Int>) {
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun another(a: T, x: Continuation<Int>) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T> builder(coroutine c: Controller<T>.() -> Continuation<Unit>) { }
|
||||||
|
|
||||||
|
inline fun run(x: () -> Unit) {}
|
||||||
|
|
||||||
|
inline fun runCross(crossinline x: () -> Unit) {}
|
||||||
|
|
||||||
|
fun noinline(x: () -> Unit) {}
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
var result = 1
|
||||||
|
builder<String> {
|
||||||
|
suspendHere()
|
||||||
|
another("")
|
||||||
|
another(<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>)
|
||||||
|
|
||||||
|
result += suspendHere()
|
||||||
|
|
||||||
|
run {
|
||||||
|
result += suspendHere()
|
||||||
|
run {
|
||||||
|
suspendHere()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
runCross {
|
||||||
|
result += <!NON_LOCAL_SUSPENSION_POINT!>suspendHere<!>()
|
||||||
|
runCross {
|
||||||
|
<!NON_LOCAL_SUSPENSION_POINT!>suspendHere<!>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
noinline {
|
||||||
|
result += <!NON_LOCAL_SUSPENSION_POINT!>suspendHere<!>()
|
||||||
|
noinline {
|
||||||
|
<!NON_LOCAL_SUSPENSION_POINT!>suspendHere<!>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class A {
|
||||||
|
fun bar() {
|
||||||
|
<!NON_LOCAL_SUSPENSION_POINT!>suspendHere<!>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
object : Any() {
|
||||||
|
fun baz() {
|
||||||
|
<!NON_LOCAL_SUSPENSION_POINT!>suspendHere<!>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
builder<Int> {
|
||||||
|
suspendHere()
|
||||||
|
|
||||||
|
another(1)
|
||||||
|
<!NON_LOCAL_SUSPENSION_POINT!>another<!>("")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package
|
||||||
|
|
||||||
|
public fun </*0*/ T> builder(/*0*/ coroutine c: Controller<T>.() -> kotlin.coroutines.Continuation<kotlin.Unit>): kotlin.Unit
|
||||||
|
public fun foo(): kotlin.Unit
|
||||||
|
public fun noinline(/*0*/ x: () -> kotlin.Unit): kotlin.Unit
|
||||||
|
public inline fun run(/*0*/ x: () -> kotlin.Unit): kotlin.Unit
|
||||||
|
public inline fun runCross(/*0*/ crossinline x: () -> kotlin.Unit): kotlin.Unit
|
||||||
|
|
||||||
|
public final class Controller</*0*/ T> {
|
||||||
|
public constructor Controller</*0*/ T>()
|
||||||
|
public final suspend fun another(/*0*/ a: T, /*1*/ x: kotlin.coroutines.Continuation<kotlin.Int>): kotlin.Unit
|
||||||
|
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 final suspend fun suspendHere(/*0*/ x: kotlin.coroutines.Continuation<kotlin.Int>): kotlin.Unit
|
||||||
|
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||||
|
}
|
||||||
@@ -3867,12 +3867,24 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
|||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/coroutines"), Pattern.compile("^(.+)\\.kt$"), true);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/coroutines"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("irrelevantSuspendDeclarations.kt")
|
||||||
|
public void testIrrelevantSuspendDeclarations() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/coroutines/irrelevantSuspendDeclarations.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("lambdaExpectedType.kt")
|
@TestMetadata("lambdaExpectedType.kt")
|
||||||
public void testLambdaExpectedType() throws Exception {
|
public void testLambdaExpectedType() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/coroutines/lambdaExpectedType.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/coroutines/lambdaExpectedType.kt");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("nonLocalSuspension.kt")
|
||||||
|
public void testNonLocalSuspension() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/coroutines/nonLocalSuspension.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("suspendFunctions.kt")
|
@TestMetadata("suspendFunctions.kt")
|
||||||
public void testSuspendFunctions() throws Exception {
|
public void testSuspendFunctions() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/coroutines/suspendFunctions.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/coroutines/suspendFunctions.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user