[Commonizer] CirTypeDistance: Use property for 'unreachable'

^KT-48288
This commit is contained in:
sebastian.sellmair
2021-09-15 14:11:31 +02:00
committed by Space
parent 31445d1471
commit 24ac122e88
2 changed files with 18 additions and 17 deletions
@@ -55,7 +55,7 @@ value class CirTypeDistance(private val value: Int) : Comparable<CirTypeDistance
} }
companion object { companion object {
fun unreachable(): CirTypeDistance = CirTypeDistance(Int.MAX_VALUE) val unreachable: CirTypeDistance = CirTypeDistance(Int.MAX_VALUE)
} }
} }
@@ -87,14 +87,14 @@ internal fun typeDistance(
} }
private fun forwardTypeDistance(from: CirClassOrTypeAliasType, to: CirEntityId): CirTypeDistance { private fun forwardTypeDistance(from: CirClassOrTypeAliasType, to: CirEntityId): CirTypeDistance {
if (from !is CirTypeAliasType) return unreachable() if (from !is CirTypeAliasType) return unreachable
var iteration = 0 var iteration = 0
var underlyingType: CirClassOrTypeAliasType? = from.underlyingType var underlyingType: CirClassOrTypeAliasType? = from.underlyingType
while (true) { while (true) {
iteration++ iteration++
val capturedUnderlyingType = underlyingType ?: return unreachable() val capturedUnderlyingType = underlyingType ?: return unreachable
if (capturedUnderlyingType.classifierId == to) return CirTypeDistance(iteration) if (capturedUnderlyingType.classifierId == to) return CirTypeDistance(iteration)
underlyingType = (capturedUnderlyingType as? CirTypeAliasType)?.underlyingType underlyingType = (capturedUnderlyingType as? CirTypeAliasType)?.underlyingType
} }
@@ -109,14 +109,14 @@ private fun backwardsTypeDistance(
return classifiers.backwardsTypeDistance(targetIndex, from, resolvedDependencyClassifier) return classifiers.backwardsTypeDistance(targetIndex, from, resolvedDependencyClassifier)
} }
val resolvedClassifier = classifiers.classifierIndices[targetIndex].findClassifier(to) ?: return unreachable() val resolvedClassifier = classifiers.classifierIndices[targetIndex].findClassifier(to) ?: return unreachable
return classifiers.backwardsTypeDistance(targetIndex, from, resolvedClassifier) return classifiers.backwardsTypeDistance(targetIndex, from, resolvedClassifier)
} }
private fun CirKnownClassifiers.backwardsTypeDistance( private fun CirKnownClassifiers.backwardsTypeDistance(
targetIndex: Int, from: CirEntityId, to: CirProvided.Classifier targetIndex: Int, from: CirEntityId, to: CirProvided.Classifier
): CirTypeDistance { ): CirTypeDistance {
if (to !is CirProvided.TypeAlias) return unreachable() if (to !is CirProvided.TypeAlias) return unreachable
if (to.underlyingType.classifierId == from) return CirTypeDistance(-1) if (to.underlyingType.classifierId == from) return CirTypeDistance(-1)
return backwardsTypeDistance(this, targetIndex, from, to.underlyingType.classifierId) - 1 return backwardsTypeDistance(this, targetIndex, from, to.underlyingType.classifierId) - 1
} }
@@ -124,7 +124,7 @@ private fun CirKnownClassifiers.backwardsTypeDistance(
private fun CirKnownClassifiers.backwardsTypeDistance( private fun CirKnownClassifiers.backwardsTypeDistance(
targetIndex: Int, from: CirEntityId, to: CirClassifier targetIndex: Int, from: CirEntityId, to: CirClassifier
): CirTypeDistance { ): CirTypeDistance {
if (to !is CirTypeAlias) return unreachable() if (to !is CirTypeAlias) return unreachable
if (to.underlyingType.classifierId == from) return CirTypeDistance(-1) if (to.underlyingType.classifierId == from) return CirTypeDistance(-1)
return backwardsTypeDistance(this, targetIndex, from, to.underlyingType.classifierId) - 1 return backwardsTypeDistance(this, targetIndex, from, to.underlyingType.classifierId) - 1
} }
@@ -299,21 +299,22 @@ class CirTypeDistanceTest : KtInlineSourceCommonizerTestCase() {
} }
fun `test unreachable distance`() { fun `test unreachable distance`() {
assertUnreachable(CirTypeDistance.unreachable() + CirTypeDistance.unreachable()) assertUnreachable(CirTypeDistance.unreachable + CirTypeDistance.unreachable)
assertUnreachable(CirTypeDistance.unreachable() + 1) assertUnreachable(CirTypeDistance.unreachable + 1)
assertUnreachable(CirTypeDistance.unreachable().inc()) assertUnreachable(CirTypeDistance.unreachable.inc())
assertUnreachable(CirTypeDistance.unreachable() + CirTypeDistance(1)) assertUnreachable(CirTypeDistance.unreachable + CirTypeDistance(1))
assertUnreachable(CirTypeDistance(1) + CirTypeDistance.unreachable()) assertUnreachable(CirTypeDistance(1) + CirTypeDistance.unreachable)
assertUnreachable(CirTypeDistance.unreachable() - 1) assertUnreachable(CirTypeDistance.unreachable - 1)
assertUnreachable(CirTypeDistance.unreachable().dec()) assertUnreachable(CirTypeDistance.unreachable.dec())
assertUnreachable(CirTypeDistance.unreachable() - CirTypeDistance(1)) assertUnreachable(CirTypeDistance.unreachable - CirTypeDistance(1))
assertUnreachable(CirTypeDistance(1) - CirTypeDistance.unreachable()) assertUnreachable(CirTypeDistance(1) - CirTypeDistance.unreachable)
assertUnreachable(CirTypeDistance.unreachable().absoluteValue) assertUnreachable(CirTypeDistance.unreachable.absoluteValue)
assertEquals(CirTypeDistance(Int.MAX_VALUE), CirTypeDistance.unreachable)
} }
} }
private fun assertUnreachable(typeDistance: CirTypeDistance) { private fun assertUnreachable(typeDistance: CirTypeDistance) {
assertEquals(CirTypeDistance.unreachable(), typeDistance) assertEquals(CirTypeDistance.unreachable, typeDistance)
assertTrue(typeDistance.isNotReachable) assertTrue(typeDistance.isNotReachable)
assertFalse(typeDistance.isReachable) assertFalse(typeDistance.isReachable)
assertFalse(typeDistance.isPositive) assertFalse(typeDistance.isPositive)