From 0ea6b32c018fe0bd11700b62d0cee7b2615c8f60 Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Tue, 8 Dec 2020 13:26:37 -0800 Subject: [PATCH] NI: allow lower bound of flexible type for coercion-to-Unit Example from box/inference/coercionToUnitForLambdaReturnTypeWithFlexibleConstraint // FILE: TestJ.java public class TestJ { public static In materialize() { return null; } } // FILE: test.kt class In fun inferred(e: In?, l: () -> T): T = l() fun box() { inferred(TestJ.materialize(), { null }) } `materialize` has flexible type, both for `In` and `T`. When analyzing `{ null }`, collected type constraints include: ft <: T (from ft>, In>?>) By allowing the lower bound of flexible type, FIR resolution can visit `{ null }` with the expected type Unit, which will lead to proper coercion to Unit at the end. --- .../resolve/calls/inference/model/NewConstraintSystemImpl.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt index 76f82239400..a8b6598eeaa 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt @@ -437,6 +437,9 @@ class NewConstraintSystemImpl( override fun hasUpperOrEqualUnitConstraint(type: KotlinTypeMarker): Boolean { checkState(State.BUILDING, State.COMPLETION, State.FREEZED) val constraints = storage.notFixedTypeVariables[type.typeConstructor()]?.constraints ?: return false - return constraints.any { (it.kind == ConstraintKind.UPPER || it.kind == ConstraintKind.EQUALITY) && it.type.isUnit() } + return constraints.any { + (it.kind == ConstraintKind.UPPER || it.kind == ConstraintKind.EQUALITY) && + it.type.lowerBoundIfFlexible().isUnit() + } } }