From 3e5be0909a483072fa7c5e5713d1fcd757f3fd3e Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Thu, 25 May 2023 11:40:43 +0200 Subject: [PATCH] Abstract away error reportin in topo sort required for scripting support code --- compiler/util/src/org/jetbrains/kotlin/utils/sortUtils.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/util/src/org/jetbrains/kotlin/utils/sortUtils.kt b/compiler/util/src/org/jetbrains/kotlin/utils/sortUtils.kt index 0784cbcceee..02e6ba6fafe 100644 --- a/compiler/util/src/org/jetbrains/kotlin/utils/sortUtils.kt +++ b/compiler/util/src/org/jetbrains/kotlin/utils/sortUtils.kt @@ -16,13 +16,17 @@ package org.jetbrains.kotlin.utils * first in the list of dependencies of `C`. Without a way to find the incoming edge from `B` to `A` while processing `C -> A`, a naive * implementation of Kahn's algorithm might order `A` before `B`. */ -fun topologicalSort(nodes: Iterable, dependencies: A.() -> Iterable): List { +fun topologicalSort( + nodes: Iterable, + reportCycle: (A) -> Nothing = { throw IllegalStateException("Cannot compute a topological sort: The node $it is in a cycle.") }, + dependencies: A.() -> Iterable, +): List { val visiting = mutableSetOf() val visited = mutableSetOf() fun visit(node: A) { if (node in visited) return - if (node in visiting) throw IllegalStateException("Cannot compute a topological sort: The node $node is in a cycle.") + if (node in visiting) reportCycle(node) // Keeping track of the nodes that are being visited allows the algorithm to throw an exception in case of a cycle. The input should // never be cyclic, but this approach gives some additional safety in case of bugs.