[FIR] Check for lambda arguments that expected type is functional type

This commit is contained in:
Dmitriy Novozhilov
2019-11-01 14:09:34 +03:00
parent 297d07f465
commit 9c8d6fb49b
4 changed files with 32 additions and 0 deletions
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.fir.declarations.FirAnonymousFunction
import org.jetbrains.kotlin.fir.expressions.FirCallableReferenceAccess
import org.jetbrains.kotlin.fir.resolve.DoubleColonLHS
import org.jetbrains.kotlin.fir.resolve.constructType
import org.jetbrains.kotlin.fir.resolve.createFunctionalType
import org.jetbrains.kotlin.fir.resolve.firSymbolProvider
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
import org.jetbrains.kotlin.fir.symbols.impl.ConeClassLikeLookupTagImpl
@@ -16,6 +17,8 @@ import org.jetbrains.kotlin.fir.symbols.invoke
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.impl.ConeClassTypeImpl
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.model.ArgumentConstraintPosition
import org.jetbrains.kotlin.resolve.calls.inference.model.SimpleConstraintSystemConstraintPosition
import org.jetbrains.kotlin.resolve.calls.model.PostponedResolvedAtomMarker
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -35,6 +38,9 @@ fun Candidate.preprocessLambdaArgument(
?: extraLambdaInfo(expectedType, expectedTypeRef, argument, csBuilder)
if (expectedType != null) {
// TODO: add SAM conversion processing
val lambdaType = createFunctionalType(resolvedArgument.parameters, resolvedArgument.receiver, resolvedArgument.returnType)
csBuilder.addSubtypeConstraint(lambdaType, expectedType, SimpleConstraintSystemConstraintPosition)
// val lambdaType = createFunctionType(
// csBuilder.builtIns, Annotations.EMPTY, resolvedArgument.receiver,
// resolvedArgument.parameters, null, resolvedArgument.returnType, resolvedArgument.isSuspend
@@ -0,0 +1,8 @@
interface A {
fun foo(b: Boolean = false): A
fun foo(block: () -> Boolean): A
}
fun test(a: A) {
a.foo { true }
}
@@ -0,0 +1,13 @@
FILE: overloadWithDefault.kt
public abstract interface A : R|kotlin/Any| {
public abstract fun foo(b: R|kotlin/Boolean| = Boolean(false)): R|A|
public abstract fun foo(block: R|kotlin/Function0<kotlin/Boolean>|): R|A|
}
public final fun test(a: R|A|): R|kotlin/Unit| {
R|<local>/a|.R|/A.foo|(<L> = foo@fun <anonymous>(): R|kotlin/Boolean| {
Boolean(true)
}
)
}
@@ -235,6 +235,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
runTest("compiler/fir/resolve/testData/resolve/arguments/lambda.kt");
}
@TestMetadata("overloadWithDefault.kt")
public void testOverloadWithDefault() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/arguments/overloadWithDefault.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/arguments/simple.kt");