From f81c7a1a479b08bad7d138ed477ad15bf517c5ce Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Tue, 3 Oct 2017 19:36:53 +0300 Subject: [PATCH] Add explicit fast path in TypeCheckerContext::anySupertype --- .../types/checker/TypeCheckerContext.kt | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/TypeCheckerContext.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/TypeCheckerContext.kt index edcbb83763a..0ee3302c8ee 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/TypeCheckerContext.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/TypeCheckerContext.kt @@ -70,6 +70,8 @@ open class TypeCheckerContext(val errorTypeEqualsToAnything: Boolean, val allowe predicate: (SimpleType) -> Boolean, supertypesPolicy: (SimpleType) -> SupertypesPolicy ): Boolean { + if (predicate(start)) return true + initialize() val deque = supertypesDeque!! @@ -81,18 +83,17 @@ open class TypeCheckerContext(val errorTypeEqualsToAnything: Boolean, val allowe error("Too many supertypes for type: $start. Supertypes = ${visitedSupertypes.joinToString()}") } val current = deque.pop() - - if (!visitedSupertypes.add(current)) { - continue - } - - if (predicate(current)) { - clear() - return true - } + if (!visitedSupertypes.add(current)) continue val policy = supertypesPolicy(current).takeIf { it != SupertypesPolicy.None } ?: continue - for (supertype in current.constructor.supertypes) deque.add(policy.transformType(supertype)) + for (supertype in current.constructor.supertypes) { + val newType = policy.transformType(supertype) + if (predicate(newType)) { + clear() + return true + } + deque.add(newType) + } } clear() @@ -134,4 +135,4 @@ open class TypeCheckerContext(val errorTypeEqualsToAnything: Boolean, val allowe } val UnwrappedType.isAllowedTypeVariable: Boolean get() = allowedTypeVariable && constructor is NewTypeVariableConstructor -} \ No newline at end of file +}