Make computation of indexed JavaTypeQualifiers eager

This commit is contained in:
Denis Zharkov
2015-07-02 12:53:48 +03:00
parent c01c59d562
commit 1469a0aa6f
2 changed files with 10 additions and 5 deletions
@@ -119,16 +119,20 @@ fun JetType.computeIndexedQualifiersForOverride(fromSupertypes: Collection<JetTy
// Note that `this` is flexible here, so it's equal to it's bounds
val onlyHeadTypeConstructor = isCovariant && fromSupertypes.any { !JetTypeChecker.DEFAULT.equalTypes(it, this) }
return fun(index: Int): JavaTypeQualifiers {
val treeSize = if (onlyHeadTypeConstructor) 1 else indexedThisType.size()
val computedResult = Array(treeSize) {
index ->
val isHeadTypeConstructor = index == 0
if (!isHeadTypeConstructor && onlyHeadTypeConstructor) return JavaTypeQualifiers.NONE
assert(isHeadTypeConstructor || !onlyHeadTypeConstructor) { "Only head type constructors should be computed" }
val qualifiers = indexedThisType.getOrDefault(index, { return JavaTypeQualifiers.NONE })
val qualifiers = indexedThisType[index]
val verticalSlice = indexedFromSupertypes.map { it.getOrDefault(index, { null }) }.filterNotNull()
// Only the head type constructor is safely co-variant
return qualifiers.computeQualifiersForOverride(verticalSlice, isCovariant && isHeadTypeConstructor)
qualifiers.computeQualifiersForOverride(verticalSlice, isCovariant && isHeadTypeConstructor)
}
return { index -> computedResult.getOrDefault(index) { JavaTypeQualifiers.NONE } }
}
private fun JetType.computeQualifiersForOverride(fromSupertypes: Collection<JetType>, isCovariant: Boolean): JavaTypeQualifiers {
@@ -106,4 +106,5 @@ public fun <T> Collection<T>.toReadOnlyList(): List<T> =
public fun <T: Any> T?.singletonOrEmptyList(): List<T> = if (this != null) Collections.singletonList(this) else Collections.emptyList()
public inline fun <T> List<T>.getOrDefault(index: Int, default: (Int) -> T): T = if (index in 0..size() - 1) this[index] else default(index)
public inline fun <T> List<T>.getOrDefault(index: Int, default: (Int) -> T): T = if (index in 0..size() - 1) this[index] else default(index)
public inline fun <T> Array<T>.getOrDefault(index: Int, default: (Int) -> T): T = if (index in 0..size() - 1) this[index] else default(index)