diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt index f58ed7f0e2e..1f80e59aea0 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt @@ -144,6 +144,27 @@ fun Collection.closure(f: (T) -> Collection): Collection { return result } +fun Collection.lazyClosure(f: (T) -> Collection): Sequence = sequence { + if (size == 0) return@sequence + var sizeBeforeIteration = 0 + + yieldAll(this@lazyClosure) + var yieldedCount = size + var elementsToCheck = this@lazyClosure + + while (yieldedCount > sizeBeforeIteration) { + val toAdd = hashSetOf() + elementsToCheck.forEach { + val neighbours = f(it) + yieldAll(neighbours) + yieldedCount += neighbours.size + toAdd.addAll(neighbours) + } + elementsToCheck = toAdd + sizeBeforeIteration = yieldedCount + } +} + fun boundClosure(types: Collection): Collection = types.closure { type -> TypeUtils.getTypeParameterDescriptorOrNull(type)?.upperBounds ?: emptySet() }