FIR: add forgotten ConeIntegerLiteralIntersector (relates to KT-51357)

This commit is contained in:
Mikhail Glukhikh
2022-02-17 11:49:30 +03:00
committed by Space
parent 4902e517d3
commit 6506d35aa6
2 changed files with 37 additions and 2 deletions
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.fir.types
object ConeIntegerLiteralIntersector {
fun findCommonIntersectionType(types: Collection<ConeKotlinType>): ConeKotlinType? {
if (types.isEmpty()) return null
return types.reduce { left: ConeKotlinType?, right: ConeKotlinType? -> fold(left, right) }
}
private fun fold(left: ConeKotlinType?, right: ConeKotlinType?): ConeKotlinType? {
if (left == null || right == null) return null
return when {
left is ConeIntegerLiteralType && right is ConeIntegerLiteralType ->
fold(left, right)
left is ConeIntegerLiteralType -> fold(left, right)
right is ConeIntegerLiteralType -> fold(right, left)
else -> null
}
}
private fun fold(left: ConeIntegerLiteralType, right: ConeIntegerLiteralType): ConeKotlinType? {
return when {
left.possibleTypes.containsAll(right.possibleTypes) -> right
right.possibleTypes.containsAll(left.possibleTypes) -> left
else -> null
}
}
private fun fold(left: ConeIntegerLiteralType, right: ConeKotlinType): ConeKotlinType? =
if (right in left.possibleTypes) right else null
}
@@ -69,8 +69,7 @@ object ConeTypeIntersector {
}
assert(filteredEqualTypes.isNotEmpty(), errorMessage)
// TODO
// IntegerLiteralTypeConstructor.findIntersectionType(filteredEqualTypes)?.let { return it }
ConeIntegerLiteralIntersector.findCommonIntersectionType(filteredEqualTypes)?.let { return it }
/*
* For the case like it(ft(String..String?), String?), where ft(String..String?) == String?, we prefer to _keep_ flexible type.