From faa5e463966add5c8c5f6232eaedf15e1811d78f Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Fri, 23 Apr 2021 18:18:05 +0300 Subject: [PATCH] FIR: Fix inference case with flexible captured types Previsously, errors have been ignored because we ignored errors raised from the completion phase See the comment above the createConstraintPartForLowerBoundAndFlexibleTypeVariable --- .../kotlin/fir/types/ConeInferenceContext.kt | 3 +++ .../AbstractTypeCheckerContextForConstraintSystem.kt | 6 +----- .../kotlin/types/model/TypeSystemContext.kt | 12 +++++++++++- .../kotlin/types/checker/ClassicTypeSystemContext.kt | 7 +++++++ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt index a81566b2e94..ca5e4478281 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt @@ -492,4 +492,7 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo } return intersectionType.withAlternative(secondCandidate) } + + override fun SimpleTypeMarker.createConstraintPartForLowerBoundAndFlexibleTypeVariable(): KotlinTypeMarker = + createFlexibleType(this.makeSimpleTypeDefinitelyNotNullOrNotNull(), this.withNullability(true)) } diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt index 7f0dab2369f..aea1640064e 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt @@ -237,11 +237,7 @@ abstract class AbstractTypeCheckerContextForConstraintSystem(override val typeSy when (subType) { is SimpleTypeMarker -> // Foo <: T! -- (Foo!! .. Foo) <: T - if (subType.isMarkedNullable()) { - subType // prefer nullable type to flexible one: `Foo? <: (T..T?)` => lowerConstraint = `Foo?` - } else { - createFlexibleType(subType, subType.withNullability(true)) - } + subType.createConstraintPartForLowerBoundAndFlexibleTypeVariable() is FlexibleTypeMarker -> // (Foo..Bar) <: T! -- (Foo!! .. Bar) <: T diff --git a/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt b/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt index 35219ad211f..7681aac48cb 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt @@ -7,7 +7,6 @@ package org.jetbrains.kotlin.types.model import org.jetbrains.kotlin.types.AbstractTypeCheckerContext import org.jetbrains.kotlin.types.Variance -import kotlin.collections.ArrayList import kotlin.contracts.ExperimentalContracts import kotlin.contracts.contract @@ -238,6 +237,17 @@ interface TypeSystemInferenceExtensionContext : TypeSystemContext, TypeSystemBui @OptIn(ExperimentalStdlibApi::class) fun KotlinTypeMarker.extractTypeVariables() = buildSet { extractTypeVariables(this) } + + /** + * For case Foo <: (T..T?) return LowerBound for new constraint LowerBound <: T + * In FE 1.0, in case nullable it was just Foo?, so constraint was Foo? <: T + * But it's not 100% correct because prevent having not-nullable upper constraint on T while initial (Foo? <: (T..T?)) is not violated + * + * In FIR, we try to have a correct one: (Foo!!..Foo?) <: T + * + * In future once we have only FIR (or FE 1.0 behavior is fixed) this method should be inlined to the use-site + */ + fun SimpleTypeMarker.createConstraintPartForLowerBoundAndFlexibleTypeVariable(): KotlinTypeMarker } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt index cf1798b3d6e..1b5330b0a46 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt @@ -741,6 +741,13 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy override fun getKFunctionTypeConstructor(parametersNumber: Int, isSuspend: Boolean): TypeConstructorMarker { return getKFunctionDescriptor(builtIns, parametersNumber, isSuspend).typeConstructor } + + override fun SimpleTypeMarker.createConstraintPartForLowerBoundAndFlexibleTypeVariable(): KotlinTypeMarker = + if (this.isMarkedNullable()) { + this + } else { + createFlexibleType(this, this.withNullability(true)) + } } fun TypeVariance.convertVariance(): Variance {