[FIR] Don't set lambda parameter type to Nothing by default

... when the expected type is not a function type.
Instead set it to a new type variable.
This fixes a bunch of false negative CANNOT_INFER_PARAMETER_TYPE.

#KT-59882 Fixed
This commit is contained in:
Kirill Rakhman
2024-02-23 11:33:09 +01:00
committed by Space Team
parent 9857bdc891
commit c3c2f6f90a
22 changed files with 48 additions and 94 deletions
@@ -38,12 +38,12 @@ FILE: kotlinSam.kt
}
))
R|/foo1|(SAM(R|<local>/f|))
R|/foo2<Inapplicable(INAPPLICABLE): /foo2>#|(<L> = foo2@fun <anonymous>(x: R|kotlin/Nothing|): R|kotlin/Boolean| <inline=Unknown> {
R|/foo2<Inapplicable(INAPPLICABLE): /foo2>#|(<L> = foo2@fun <anonymous>(x: <ERROR TYPE REF: Cannot infer type for parameter x>): <ERROR TYPE REF: Cannot infer type variable TypeVariable(_L)> <inline=Unknown> {
^ CMP(>, R|<local>/x|.<Unresolved name: compareTo>#(Int(1)))
}
)
R|/foo2<Inapplicable(INAPPLICABLE): /foo2>#|(R|<local>/f|)
R|/foo3<Inapplicable(INAPPLICABLE): /foo3>#|(<L> = foo3@fun <anonymous>(x: R|kotlin/Nothing|): R|kotlin/Boolean| <inline=Unknown> {
R|/foo3<Inapplicable(INAPPLICABLE): /foo3>#|(<L> = foo3@fun <anonymous>(x: <ERROR TYPE REF: Cannot infer type for parameter x>): <ERROR TYPE REF: Cannot infer type variable TypeVariable(_L)> <inline=Unknown> {
^ CMP(>, R|<local>/x|.<Unresolved name: compareTo>#(Int(1)))
}
)
@@ -29,10 +29,10 @@ fun main() {
foo1 { x -> x > 1 }
foo1(f)
foo2 <!ARGUMENT_TYPE_MISMATCH!>{ x -> x <!UNRESOLVED_REFERENCE!>><!> 1 }<!>
foo2 <!ARGUMENT_TYPE_MISMATCH!>{ <!CANNOT_INFER_PARAMETER_TYPE!>x<!> -> x <!UNRESOLVED_REFERENCE!>><!> 1 }<!>
foo2(<!ARGUMENT_TYPE_MISMATCH!>f<!>)
foo3 <!ARGUMENT_TYPE_MISMATCH!>{ x -> x <!UNRESOLVED_REFERENCE!>><!> 1 }<!>
foo3 <!ARGUMENT_TYPE_MISMATCH!>{ <!CANNOT_INFER_PARAMETER_TYPE!>x<!> -> x <!UNRESOLVED_REFERENCE!>><!> 1 }<!>
foo3(<!ARGUMENT_TYPE_MISMATCH!>f<!>)
foo4 { x -> x > 1 }
@@ -2,7 +2,7 @@ FILE: main.kt
public final fun foo(m: R|MyRunnable|): R|kotlin/Unit| {
}
public final fun main(): R|kotlin/Unit| {
Q|JavaUsage|.R|/JavaUsage.foo*s<Inapplicable(INAPPLICABLE): /JavaUsage.foo>#|(<L> = foo@fun <anonymous>(x: R|kotlin/Nothing|): R|kotlin/Boolean| <inline=Unknown> {
Q|JavaUsage|.R|/JavaUsage.foo*s<Inapplicable(INAPPLICABLE): /JavaUsage.foo>#|(<L> = foo@fun <anonymous>(x: <ERROR TYPE REF: Cannot infer type for parameter x>): <ERROR TYPE REF: Cannot infer type variable TypeVariable(_L)> <inline=Unknown> {
^ CMP(>, R|<local>/x|.<Unresolved name: compareTo>#(Int(1)))
}
)
@@ -19,7 +19,7 @@ fun foo(m: MyRunnable) {}
fun main() {
JavaUsage.foo <!ARGUMENT_TYPE_MISMATCH!>{
x -> x <!UNRESOLVED_REFERENCE!>><!> 1
<!CANNOT_INFER_PARAMETER_TYPE!>x<!> -> x <!UNRESOLVED_REFERENCE!>><!> 1
}<!>
JavaUsage.foo(<!ARGUMENT_TYPE_MISMATCH!>{ <!UNRESOLVED_REFERENCE!>it<!> > 1 }<!>)
@@ -108,8 +108,7 @@ object ConeConstraintSystemUtilContext : ConstraintSystemUtilContext {
index: Int
): TypeVariableMarker {
return ConeTypeVariableForLambdaParameterType(
PostponedArgumentInputTypesResolver.TYPE_VARIABLE_NAME_PREFIX_FOR_LAMBDA_PARAMETER_TYPE + index,
index
PostponedArgumentInputTypesResolver.TYPE_VARIABLE_NAME_PREFIX_FOR_LAMBDA_PARAMETER_TYPE + index
)
}
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.addSubtypeConstraintIfCompatible
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintKind
import org.jetbrains.kotlin.types.model.typeConstructor
import org.jetbrains.kotlin.utils.addToStdlib.runIf
fun Candidate.preprocessLambdaArgument(
csBuilder: ConstraintSystemBuilder,
@@ -120,17 +121,18 @@ private fun extractLambdaInfo(
argument.returnType
?: typeVariable.defaultType
val defaultType = when (candidate?.symbol?.origin) {
FirDeclarationOrigin.DynamicScope -> ConeDynamicType.create(session)
else -> session.builtinTypes.nothingType.type
val defaultType = runIf(candidate?.symbol?.origin == FirDeclarationOrigin.DynamicScope) { ConeDynamicType.create(session) }
val parameters = argument.valueParameters.mapIndexed { i, it ->
it.returnTypeRef.coneTypeSafe<ConeKotlinType>()
?: defaultType
?: ConeTypeVariableForLambdaParameterType("_P$i").apply { csBuilder.registerVariable(this) }.defaultType
}
val parameters = argument.valueParameters.map {
it.returnTypeRef.coneTypeSafe<ConeKotlinType>() ?: defaultType
}
val contextReceivers = argument.contextReceivers.map {
it.typeRef.coneTypeSafe<ConeKotlinType>() ?: defaultType
val contextReceivers = argument.contextReceivers.mapIndexed { i, it ->
it.typeRef.coneTypeSafe<ConeKotlinType>()
?: defaultType
?: ConeTypeVariableForLambdaParameterType("_C$i").apply { csBuilder.registerVariable(this) }.defaultType
}
val newTypeVariableUsed = returnType == typeVariable.defaultType
@@ -11,7 +11,7 @@ import org.jetbrains.kotlin.fir.types.ConeTypeVariable
import org.jetbrains.kotlin.name.SpecialNames
class ConeTypeVariableForPostponedAtom(name: String) : ConeTypeVariable(name)
class ConeTypeVariableForLambdaParameterType(name: String, val index: Int) : ConeTypeVariable(name)
class ConeTypeVariableForLambdaParameterType(name: String) : ConeTypeVariable(name)
class ConeTypeVariableForLambdaReturnType(val argument: FirAnonymousFunction, name: String) : ConeTypeVariable(name)
class ConeTypeParameterBasedTypeVariable(