From 5bf489327df366310d7d065b8cbb5c016b7bdf79 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 19 Apr 2019 10:49:21 +0300 Subject: [PATCH] Minor fix of around flexible and not-null FIR types (by semoro) --- .../src/org/jetbrains/kotlin/fir/types/ConeTypes.kt | 5 ++++- .../fir/resolve/substitution/ConeSubstitutor.kt | 11 +++++++---- .../org/jetbrains/kotlin/fir/types/ConeTypeContext.kt | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypes.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypes.kt index 47a58b457ca..4236cac0883 100644 --- a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypes.kt +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypes.kt @@ -107,7 +107,7 @@ abstract class ConeTypeParameterType : ConeLookupTagBasedType() { -class ConeFlexibleType(val lowerBound: ConeLookupTagBasedType, val upperBound: ConeLookupTagBasedType) : ConeKotlinType(), +class ConeFlexibleType(val lowerBound: ConeKotlinType, val upperBound: ConeKotlinType) : ConeKotlinType(), FlexibleTypeMarker { override val typeArguments: Array get() = emptyArray() @@ -116,6 +116,9 @@ class ConeFlexibleType(val lowerBound: ConeLookupTagBasedType, val upperBound: C get() = lowerBound.nullability.takeIf { it == upperBound.nullability } ?: ConeNullability.UNKNOWN } +fun ConeKotlinType.upperBoundIfFlexible() = (this as? ConeFlexibleType)?.upperBound ?: this +fun ConeKotlinType.lowerBoundIfFlexible() = (this as? ConeFlexibleType)?.lowerBound ?: this + class ConeCapturedTypeConstructor(val projection: ConeKotlinTypeProjection, var supertypes: List? = null) : TypeConstructorMarker { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/substitution/ConeSubstitutor.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/substitution/ConeSubstitutor.kt index 7361f5d812b..87963633212 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/substitution/ConeSubstitutor.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/substitution/ConeSubstitutor.kt @@ -57,7 +57,7 @@ abstract class AbstractConeSubstitutor : ConeSubstitutor { override fun substituteOrNull(type: ConeKotlinType): ConeKotlinType? { val newType = substituteType(type) - return (newType ?: type).substituteRecursive() ?: newType + return (newType ?: type.substituteRecursive()) ?: newType } private fun ConeKotlinType.substituteRecursive(): ConeKotlinType? { @@ -78,10 +78,13 @@ abstract class AbstractConeSubstitutor : ConeSubstitutor { } private fun ConeFlexibleType.substituteBounds(): ConeFlexibleType? { - val newLowerBound = substituteOrNull(lowerBound) as ConeLookupTagBasedType? - val newUpperBound = substituteOrNull(upperBound) as ConeLookupTagBasedType? + val newLowerBound = substituteOrNull(lowerBound) + val newUpperBound = substituteOrNull(upperBound) if (newLowerBound != null || newUpperBound != null) { - return ConeFlexibleType(newLowerBound ?: lowerBound, newUpperBound ?: upperBound) + return ConeFlexibleType( + newLowerBound?.lowerBoundIfFlexible() ?: lowerBound, + newUpperBound?.upperBoundIfFlexible() ?: upperBound + ) } return null } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt index 32bf5727a06..da8fd6fdeff 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt @@ -86,12 +86,12 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext { override fun FlexibleTypeMarker.upperBound(): SimpleTypeMarker { require(this is ConeFlexibleType) - return this.upperBound + return this.upperBound as SimpleTypeMarker } override fun FlexibleTypeMarker.lowerBound(): SimpleTypeMarker { require(this is ConeFlexibleType) - return this.lowerBound + return this.lowerBound as SimpleTypeMarker } override fun SimpleTypeMarker.asCapturedType(): CapturedTypeMarker? {