From def647c0941769e3f7e08ecf242174f74c626e80 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 11 Sep 2020 15:38:04 +0300 Subject: [PATCH] [FIR] Don't constraints for return expressions of lambda if it has Unit return type --- .../builderInferenceAndCoercionToUnit.kt | 9 +++++++++ .../builderInferenceAndCoercionToUnit.txt | 16 ++++++++++++++++ .../FirDiagnosticsWithStdlibTestGenerated.java | 5 +++++ .../resolve/calls/CallableReferenceResolution.kt | 2 +- .../inference/PostponedArgumentsAnalyzer.kt | 6 +++--- 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInferenceAndCoercionToUnit.kt create mode 100644 compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInferenceAndCoercionToUnit.txt diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInferenceAndCoercionToUnit.kt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInferenceAndCoercionToUnit.kt new file mode 100644 index 00000000000..a5cc7aa9a9b --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInferenceAndCoercionToUnit.kt @@ -0,0 +1,9 @@ +class DropDownComponent(val initialValues: List) + +fun test(strings: List) { + val dropDown = DropDownComponent( + initialValues = buildList { + addAll(strings) + } + ) +} diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInferenceAndCoercionToUnit.txt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInferenceAndCoercionToUnit.txt new file mode 100644 index 00000000000..ca56573ca80 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInferenceAndCoercionToUnit.txt @@ -0,0 +1,16 @@ +FILE: builderInferenceAndCoercionToUnit.kt + public final class DropDownComponent : R|kotlin/Any| { + public constructor(initialValues: R|kotlin/collections/List|): R|DropDownComponent| { + super() + } + + public final val initialValues: R|kotlin/collections/List| = R|/initialValues| + public get(): R|kotlin/collections/List| + + } + public final fun test(strings: R|kotlin/collections/List|): R|kotlin/Unit| { + lval dropDown: R|DropDownComponent| = R|/DropDownComponent.DropDownComponent|(initialValues = R|kotlin/collections/buildList|( = buildList@fun R|kotlin/collections/MutableList|.(): R|kotlin/Unit| { + this@R|special/anonymous|.R|FakeOverride|(R|/strings|) + } + )) + } diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithStdlibTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithStdlibTestGenerated.java index c2a69273d2e..29bfdc6f9fc 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithStdlibTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithStdlibTestGenerated.java @@ -847,6 +847,11 @@ public class FirDiagnosticsWithStdlibTestGenerated extends AbstractFirDiagnostic 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") public void testBuilderInferenceFromStdlib() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/builderInferenceFromStdlib.kt"); diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CallableReferenceResolution.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CallableReferenceResolution.kt index 9f017e4e17b..fa420f974f9 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CallableReferenceResolution.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CallableReferenceResolution.kt @@ -294,7 +294,7 @@ private enum class VarargMappingState { private fun FirFunction<*>.indexOf(valueParameter: FirValueParameter): Int = valueParameters.indexOf(valueParameter) -private val ConeKotlinType.isUnit: Boolean +val ConeKotlinType.isUnit: Boolean get() { val type = this.lowerBoundIfFlexible() if (type.isNullable) return false diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt index 67b666c9863..1f8b5a101cf 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt @@ -154,13 +154,14 @@ class PostponedArgumentsAnalyzer( val checkerSink: CheckerSink = CheckerSinkImpl() var hasExpressionInReturnArguments = false + val lambdaReturnType = lambda.returnType.let(::substitute).takeUnless { it.isUnit } returnArguments.forEach { if (it !is FirExpression) return@forEach hasExpressionInReturnArguments = true candidate.resolveArgumentExpression( c.getBuilder(), it, - lambda.returnType.let(::substitute), + lambdaReturnType, lambda.atom.returnTypeRef, // TODO: proper ref checkerSink, context = resolutionContext, @@ -169,8 +170,7 @@ class PostponedArgumentsAnalyzer( ) } - if (!hasExpressionInReturnArguments) { - val lambdaReturnType = lambda.returnType.let(::substitute) + if (!hasExpressionInReturnArguments && lambdaReturnType != null) { /*LambdaArgumentConstraintPosition(lambda)*/ c.getBuilder().addEqualityConstraint(lambdaReturnType, unitType, SimpleConstraintSystemConstraintPosition) }