[FIR] Don't constraints for return expressions of lambda if it has Unit return type

This commit is contained in:
Dmitriy Novozhilov
2020-09-11 15:38:04 +03:00
parent 8e0d0d2fd8
commit def647c094
5 changed files with 34 additions and 4 deletions
@@ -0,0 +1,9 @@
class DropDownComponent<T : Any>(val initialValues: List<T>)
fun test(strings: List<String>) {
val dropDown = DropDownComponent(
initialValues = buildList {
addAll(strings)
}
)
}
@@ -0,0 +1,16 @@
FILE: builderInferenceAndCoercionToUnit.kt
public final class DropDownComponent<T : R|kotlin/Any|> : R|kotlin/Any| {
public constructor<T : R|kotlin/Any|>(initialValues: R|kotlin/collections/List<T>|): R|DropDownComponent<T>| {
super<R|kotlin/Any|>()
}
public final val initialValues: R|kotlin/collections/List<T>| = R|<local>/initialValues|
public get(): R|kotlin/collections/List<T>|
}
public final fun test(strings: R|kotlin/collections/List<kotlin/String>|): R|kotlin/Unit| {
lval dropDown: R|DropDownComponent<kotlin/String>| = R|/DropDownComponent.DropDownComponent|<R|kotlin/String|>(initialValues = R|kotlin/collections/buildList|<R|kotlin/String|>(<L> = buildList@fun R|kotlin/collections/MutableList<kotlin/String>|.<anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
this@R|special/anonymous|.R|FakeOverride<kotlin/collections/MutableList.addAll: R|kotlin/Boolean|>|(R|<local>/strings|)
}
))
}
@@ -847,6 +847,11 @@ public class FirDiagnosticsWithStdlibTestGenerated extends AbstractFirDiagnostic
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInference.kt"); runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInference.kt");
} }
@TestMetadata("builderInferenceAndCoercionToUnit.kt")
public void testBuilderInferenceAndCoercionToUnit() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInferenceAndCoercionToUnit.kt");
}
@TestMetadata("builderInferenceFromStdlib.kt") @TestMetadata("builderInferenceFromStdlib.kt")
public void testBuilderInferenceFromStdlib() throws Exception { public void testBuilderInferenceFromStdlib() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInferenceFromStdlib.kt"); runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInferenceFromStdlib.kt");
@@ -294,7 +294,7 @@ private enum class VarargMappingState {
private fun FirFunction<*>.indexOf(valueParameter: FirValueParameter): Int = valueParameters.indexOf(valueParameter) private fun FirFunction<*>.indexOf(valueParameter: FirValueParameter): Int = valueParameters.indexOf(valueParameter)
private val ConeKotlinType.isUnit: Boolean val ConeKotlinType.isUnit: Boolean
get() { get() {
val type = this.lowerBoundIfFlexible() val type = this.lowerBoundIfFlexible()
if (type.isNullable) return false if (type.isNullable) return false
@@ -154,13 +154,14 @@ class PostponedArgumentsAnalyzer(
val checkerSink: CheckerSink = CheckerSinkImpl() val checkerSink: CheckerSink = CheckerSinkImpl()
var hasExpressionInReturnArguments = false var hasExpressionInReturnArguments = false
val lambdaReturnType = lambda.returnType.let(::substitute).takeUnless { it.isUnit }
returnArguments.forEach { returnArguments.forEach {
if (it !is FirExpression) return@forEach if (it !is FirExpression) return@forEach
hasExpressionInReturnArguments = true hasExpressionInReturnArguments = true
candidate.resolveArgumentExpression( candidate.resolveArgumentExpression(
c.getBuilder(), c.getBuilder(),
it, it,
lambda.returnType.let(::substitute), lambdaReturnType,
lambda.atom.returnTypeRef, // TODO: proper ref lambda.atom.returnTypeRef, // TODO: proper ref
checkerSink, checkerSink,
context = resolutionContext, context = resolutionContext,
@@ -169,8 +170,7 @@ class PostponedArgumentsAnalyzer(
) )
} }
if (!hasExpressionInReturnArguments) { if (!hasExpressionInReturnArguments && lambdaReturnType != null) {
val lambdaReturnType = lambda.returnType.let(::substitute)
/*LambdaArgumentConstraintPosition(lambda)*/ /*LambdaArgumentConstraintPosition(lambda)*/
c.getBuilder().addEqualityConstraint(lambdaReturnType, unitType, SimpleConstraintSystemConstraintPosition) c.getBuilder().addEqualityConstraint(lambdaReturnType, unitType, SimpleConstraintSystemConstraintPosition)
} }