Fix SAM conversion when lambda is the last expression of another lambda

#KT-39691 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2020-07-20 10:36:37 +03:00
parent a15d3c76b6
commit 4083111dab
6 changed files with 79 additions and 0 deletions
@@ -45,12 +45,29 @@ object SamTypeConversions : ParameterTypeConversion {
override fun conversionIsNeededBeforeSubtypingCheck(argument: KotlinCallArgument): Boolean {
return when (argument) {
is SubKotlinCallArgument -> {
val stableType = argument.receiver.stableType
hasNonAnalyzedLambdaAsReturnType(argument.callResult.subResolvedAtoms, stableType)
}
is SimpleKotlinCallArgument -> argument.receiver.stableType.isFunctionType
is LambdaKotlinCallArgument, is CallableReferenceKotlinCallArgument -> true
else -> false
}
}
private fun hasNonAnalyzedLambdaAsReturnType(subResolvedAtoms: List<ResolvedAtom>?, type: UnwrappedType): Boolean {
subResolvedAtoms?.forEach {
if (it is LambdaWithTypeVariableAsExpectedTypeAtom) {
if (it.expectedType.constructor == type.constructor) return true
}
val hasNonAnalyzedLambda = hasNonAnalyzedLambdaAsReturnType(it.subResolvedAtoms, type)
if (hasNonAnalyzedLambda) return true
}
return false
}
override fun conversionIsNeededAfterSubtypingCheck(argument: KotlinCallArgument): Boolean {
return argument is SimpleKotlinCallArgument && argument.receiver.stableType.isFunctionTypeOrSubtype
}