Replace resolution error for suspend-conversion with call checker error

This commit is contained in:
Mikhail Zarechenskiy
2020-06-01 01:52:56 +03:00
parent eaeaf3c8a3
commit a5203428a4
18 changed files with 265 additions and 22 deletions
@@ -23212,6 +23212,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionCompatibility.kt");
}
@TestMetadata("suspendConversionCompatibilityInDisabledMode.kt")
public void testSuspendConversionCompatibilityInDisabledMode() throws Exception {
runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionCompatibilityInDisabledMode.kt");
}
@TestMetadata("suspendConversionDisabled.kt")
public void testSuspendConversionDisabled() throws Exception {
runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionDisabled.kt");
@@ -49,7 +49,7 @@ private val DEFAULT_CALL_CHECKERS = listOf(
PrimitiveNumericComparisonCallChecker, LambdaWithSuspendModifierCallChecker,
UselessElvisCallChecker(), ResultTypeWithNullableOperatorsChecker(), NullableVarargArgumentCallChecker,
NamedFunAsExpressionChecker, ContractNotAllowedCallChecker, ReifiedTypeParameterSubstitutionChecker(),
MissingDependencySupertypeChecker.ForCalls, AbstractClassInstantiationChecker,
MissingDependencySupertypeChecker.ForCalls, AbstractClassInstantiationChecker, SuspendConversionCallChecker
)
private val DEFAULT_TYPE_CHECKERS = emptyList<AdditionalTypeChecker>()
private val DEFAULT_CLASSIFIER_USAGE_CHECKERS = listOf(
@@ -0,0 +1,59 @@
/*
* 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.calls.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.builtins.isSuspendFunctionType
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtCallableReferenceExpression
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.CallableReferenceKotlinCallArgument
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCallArgument
import org.jetbrains.kotlin.resolve.calls.tower.NewResolvedCallImpl
import org.jetbrains.kotlin.resolve.calls.tower.psiCallArgument
import org.jetbrains.kotlin.resolve.calls.tower.psiExpression
object SuspendConversionCallChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
if (resolvedCall !is NewResolvedCallImpl<*>) return
if (context.languageVersionSettings.supportsFeature(LanguageFeature.SuspendConversion)) return
val argumentsWithSuspendConversion = resolvedCall.resolvedCallAtom.argumentsWithSuspendConversion
for (argumentWithSuspendConversion in argumentsWithSuspendConversion.keys) {
context.trace.report(
Errors.UNSUPPORTED_FEATURE.on(
argumentWithSuspendConversion.psiCallArgument.valueArgument.asElement(),
LanguageFeature.SuspendConversion to context.languageVersionSettings
)
)
}
for ((parameter, argument) in resolvedCall.argumentMappingByOriginal) {
if (!parameter.type.isSuspendFunctionType) continue
if (argument !is ResolvedCallArgument.SimpleArgument) continue
val callArgument = argument.callArgument
if (callArgument !is CallableReferenceKotlinCallArgument) continue
val callableReferenceExpression = callArgument.psiExpression as? KtCallableReferenceExpression ?: continue
val argumentCall = callableReferenceExpression.callableReference.getResolvedCall(context.trace.bindingContext) ?: continue
val target = argumentCall.resultingDescriptor as? FunctionDescriptor ?: continue
if (!target.isSuspend) {
context.trace.report(
Errors.UNSUPPORTED_FEATURE.on(
callableReferenceExpression,
LanguageFeature.SuspendConversion to context.languageVersionSettings
)
)
}
}
}
}
@@ -366,10 +366,7 @@ class CallableReferencesCandidateFactory(
mappedArguments
val suspendConversionStrategy =
if (
callComponents.languageVersionSettings.supportsFeature(LanguageFeature.SuspendConversion) &&
!descriptor.isSuspend && expectedType?.isSuspendFunctionType == true
) {
if (!descriptor.isSuspend && expectedType?.isSuspendFunctionType == true) {
SuspendConversionStrategy.SUSPEND_CONVERSION
} else {
SuspendConversionStrategy.NO_CONVERSION
@@ -18,8 +18,6 @@ object SuspendTypeConversions : ParameterTypeConversion {
argument: KotlinCallArgument,
expectedParameterType: UnwrappedType
): Boolean {
if (!candidate.callComponents.languageVersionSettings.supportsFeature(LanguageFeature.SuspendConversion)) return true
if (argument !is SimpleKotlinCallArgument) return true
val argumentType = argument.receiver.stableType
@@ -25,4 +25,19 @@ object Test2 {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>result<!>
}
}
}
}
object Test3 {
fun <T> foo(f: suspend () -> T): T = TODO()
suspend fun bar(x: Int = 42): Int = 0
object Scope {
fun bar(x: Int = 42): String = ""
fun test() {
val result = foo(::bar)
result
}
}
}
@@ -25,4 +25,19 @@ object Test2 {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>result<!>
}
}
}
object Test3 {
fun <T> foo(f: suspend () -> T): T = TODO()
suspend fun bar(x: Int = 42): Int = 0
object Scope {
fun bar(x: Int = 42): String = ""
fun test() {
val result = foo(::bar)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>result<!>
}
}
}
@@ -34,3 +34,21 @@ public object Test2 {
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public object Test3 {
private constructor Test3()
public final suspend fun bar(/*0*/ x: kotlin.Int = ...): kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun </*0*/ T> foo(/*0*/ f: suspend () -> T): T
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public object Scope {
private constructor Scope()
public final fun bar(/*0*/ x: kotlin.Int = ...): kotlin.String
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 fun test(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -0,0 +1,36 @@
// !LANGUAGE: -SuspendConversion
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
object Test1 {
fun <T> foo(f: suspend () -> T): T = TODO()
suspend fun bar(x: Int = 42): Int = 0
object Scope {
fun bar(x: Int = 42): String = ""
fun test() {
val result = foo(::bar)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>result<!>
}
}
}
object Test2 {
fun <T> foo(f: suspend () -> T): T = TODO()
suspend fun bar(): Int = 0
object Scope1 {
suspend fun bar(x: Int = 42): Double = 0.0
object Scope2 {
fun bar(x: Int = 42): String = ""
fun test() {
val result = foo(::bar)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>result<!>
}
}
}
}
@@ -0,0 +1,36 @@
// !LANGUAGE: -SuspendConversion
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
object Test1 {
fun <T> foo(f: suspend () -> T): T = TODO()
suspend fun bar(x: Int = 42): Int = 0
object Scope {
fun bar(x: Int = 42): String = ""
fun test() {
val result = foo(<!UNSUPPORTED_FEATURE!>::bar<!>)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>result<!>
}
}
}
object Test2 {
fun <T> foo(f: suspend () -> T): T = TODO()
suspend fun bar(): Int = 0
object Scope1 {
suspend fun bar(x: Int = 42): Double = 0.0
object Scope2 {
fun bar(x: Int = 42): String = ""
fun test() {
val result = foo(<!COMPATIBILITY_WARNING!>::bar<!>)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>result<!>
}
}
}
}
@@ -0,0 +1,45 @@
package
public object Test1 {
private constructor Test1()
public final suspend fun bar(/*0*/ x: kotlin.Int = ...): kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun </*0*/ T> foo(/*0*/ f: suspend () -> T): T
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public object Scope {
private constructor Scope()
public final fun bar(/*0*/ x: kotlin.Int = ...): kotlin.String
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 fun test(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public object Test2 {
private constructor Test2()
public final suspend fun bar(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun </*0*/ T> foo(/*0*/ f: suspend () -> T): T
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public object Scope1 {
private constructor Scope1()
public final suspend fun bar(/*0*/ x: kotlin.Int = ...): kotlin.Double
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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public object Scope2 {
private constructor Scope2()
public final fun bar(/*0*/ x: kotlin.Int = ...): kotlin.String
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 fun test(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
}
@@ -5,6 +5,8 @@ fun foo1(f: suspend () -> String) {}
fun foo2(f: suspend (Int) -> String) {}
fun foo3(f: suspend () -> Unit) {}
fun bar(): String = ""
fun test(
f0: suspend () -> String,
f1: () -> String,
@@ -14,10 +16,12 @@ fun test(
foo1 { "str" }
foo1(f0)
<!INAPPLICABLE_CANDIDATE!>foo1<!>(f1)
<!INAPPLICABLE_CANDIDATE!>foo2<!>(f2)
<!INAPPLICABLE_CANDIDATE!>foo3<!>(f3)
foo1(f1)
foo2(f2)
foo3(f3)
<!INAPPLICABLE_CANDIDATE!>foo1<!>(f2)
<!INAPPLICABLE_CANDIDATE!>foo1<!>(f3)
foo1(::bar)
foo1(f2)
foo1(f3)
}
@@ -5,6 +5,8 @@ fun foo1(f: suspend () -> String) {}
fun foo2(f: suspend (Int) -> String) {}
fun foo3(f: suspend () -> Unit) {}
fun bar(): String = ""
fun test(
f0: suspend () -> String,
f1: () -> String,
@@ -14,10 +16,12 @@ fun test(
foo1 { "str" }
foo1(f0)
foo1(<!TYPE_MISMATCH!>f1<!>)
foo2(<!TYPE_MISMATCH!>f2<!>)
foo3(<!TYPE_MISMATCH!>f3<!>)
foo1(<!UNSUPPORTED_FEATURE!>f1<!>)
foo2(<!UNSUPPORTED_FEATURE!>f2<!>)
foo3(<!UNSUPPORTED_FEATURE!>f3<!>)
foo1(<!TYPE_MISMATCH!>f2<!>)
foo1(<!TYPE_MISMATCH!>f3<!>)
foo1(<!UNSUPPORTED_FEATURE!>::bar<!>)
foo1(<!TYPE_MISMATCH, UNSUPPORTED_FEATURE!>f2<!>)
foo1(<!TYPE_MISMATCH, UNSUPPORTED_FEATURE!>f3<!>)
}
@@ -1,5 +1,6 @@
package
public fun bar(): kotlin.String
public fun foo1(/*0*/ f: suspend () -> kotlin.String): kotlin.Unit
public fun foo2(/*0*/ f: suspend (kotlin.Int) -> kotlin.String): kotlin.Unit
public fun foo3(/*0*/ f: suspend () -> kotlin.Unit): kotlin.Unit
@@ -21,7 +21,7 @@ fun foo() {
builder { 1 }
val x = { 1 }
builder(<!TYPE_MISMATCH!>x<!>)
builder(<!NI;UNSUPPORTED_FEATURE, OI;TYPE_MISMATCH!>x<!>)
builder({1} <!UNCHECKED_CAST!>as (suspend () -> Int)<!>)
var i: Int = 1
@@ -32,7 +32,7 @@ fun foo() {
genericBuilder<Int> { <!NI;TYPE_MISMATCH, TYPE_MISMATCH!>""<!> }
val y = { 1 }
<!OI;TYPE_INFERENCE_PARAMETER_CONSTRAINT_ERROR!>genericBuilder<!>(<!TYPE_MISMATCH!>y<!>)
<!OI;TYPE_INFERENCE_PARAMETER_CONSTRAINT_ERROR!>genericBuilder<!>(<!NI;UNSUPPORTED_FEATURE, OI;TYPE_MISMATCH!>y<!>)
unitBuilder {}
unitBuilder { <!UNUSED_EXPRESSION!>1<!> }
@@ -8,12 +8,12 @@ fun ambiguous(sfn: suspend () -> Unit) = sfn
fun ambiguous(fn: () -> Unit) = fn
fun test1(sfn: suspend () -> Unit) = useFn(<!TYPE_MISMATCH!>sfn<!>)
fun test2(fn: () -> Unit) = useSuspendFn(<!TYPE_MISMATCH!>fn<!>)
fun test2(fn: () -> Unit) = useSuspendFn(<!UNSUPPORTED_FEATURE!>fn<!>)
fun test3(sfn: suspend () -> Unit) = useSuspendFn(sfn)
fun test4(): suspend () -> Unit = useSuspendFn {}
fun test5() = useSuspendFn {}
fun test5(sfn: suspend () -> Unit) = ambiguous(sfn)
fun test6(fn: () -> Unit) = ambiguous(fn)
fun test6(fn: () -> Unit) = <!COMPATIBILITY_WARNING!>ambiguous(fn)<!>
fun test7(): () -> Unit = <!OVERLOAD_RESOLUTION_AMBIGUITY!>ambiguous<!> {}
@@ -23294,6 +23294,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionCompatibility.kt");
}
@TestMetadata("suspendConversionCompatibilityInDisabledMode.kt")
public void testSuspendConversionCompatibilityInDisabledMode() throws Exception {
runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionCompatibilityInDisabledMode.kt");
}
@TestMetadata("suspendConversionDisabled.kt")
public void testSuspendConversionDisabled() throws Exception {
runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionDisabled.kt");
@@ -23214,6 +23214,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionCompatibility.kt");
}
@TestMetadata("suspendConversionCompatibilityInDisabledMode.kt")
public void testSuspendConversionCompatibilityInDisabledMode() throws Exception {
runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionCompatibilityInDisabledMode.kt");
}
@TestMetadata("suspendConversionDisabled.kt")
public void testSuspendConversionDisabled() throws Exception {
runTest("compiler/testData/diagnostics/tests/suspendConversion/suspendConversionDisabled.kt");