From d1263c5dc38161a5b51a04e1c2ef3f6eedb036d9 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Tue, 6 Jun 2017 20:30:17 +0300 Subject: [PATCH] [NI] Coerce to unit for empty lambda block Fixes 'kt3903' --- .../kotlin/resolve/calls/components/KotlinCallCompleter.kt | 7 +++++++ .../calls/inference/model/ConstraintPositionAndErrors.kt | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt index 8c28f00e7a1..b915cc266f7 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt @@ -26,6 +26,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.components.FixationOrderCalc import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutor import org.jetbrains.kotlin.resolve.calls.inference.components.ResultTypeResolver import org.jetbrains.kotlin.resolve.calls.inference.model.ExpectedTypeConstraintPosition +import org.jetbrains.kotlin.resolve.calls.inference.model.LambdaArgumentConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable import org.jetbrains.kotlin.resolve.calls.inference.model.NotEnoughInformationForTypeParameter import org.jetbrains.kotlin.resolve.calls.inference.returnTypeOrNothing @@ -36,6 +37,7 @@ import org.jetbrains.kotlin.resolve.calls.tower.ResolutionCandidateStatus import org.jetbrains.kotlin.types.TypeUtils import org.jetbrains.kotlin.types.UnwrappedType import org.jetbrains.kotlin.types.checker.KotlinTypeChecker +import org.jetbrains.kotlin.types.typeUtil.builtIns import org.jetbrains.kotlin.utils.SmartList import org.jetbrains.kotlin.utils.addIfNotNull @@ -264,6 +266,11 @@ class KotlinCallCompleter( // todo strange code -- why top-level kotlinCall? may be it isn't right outer call CheckArguments.checkArgument(topLevelCallContext, topLevelCall, c.getBuilder(), innerCall, lambda.returnType.let(::substitute)) } + + if (lambda.resultArguments.isEmpty()) { + val unitType = lambda.returnType.builtIns.unitType + c.getBuilder().addSubtypeConstraint(lambda.returnType.let(::substitute), unitType, LambdaArgumentConstraintPosition(lambda)) + } } private fun canWeAnalyzeIt(c: Context, lambda: ResolvedLambdaArgument): Boolean { diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt index 66ae5f9a41c..3f576a252f7 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt @@ -43,6 +43,11 @@ class FixVariableConstraintPosition(val variable: NewTypeVariable) : ConstraintP class KnownTypeParameterConstraintPosition(val typeArgument: KotlinType) : ConstraintPosition() { override fun toString() = "TypeArgument $typeArgument" } +class LambdaArgumentConstraintPosition(val lambdaArgument: ResolvedLambdaArgument) : ConstraintPosition() { + override fun toString(): String { + return "LambdaArgument $lambdaArgument" + } +} class IncorporationConstraintPosition(val from: ConstraintPosition, val initialConstraint: InitialConstraint) : ConstraintPosition() { override fun toString() = "Incorporate $initialConstraint from position $from"