Skip to content

Commit c84c102

Browse files
committed
Replace topoSortDistinctsBy with topoSortDistinctsWith
Forward port of scala-js/scala-js@7648022
1 parent 6922c1d commit c84c102

File tree

1 file changed

+18
-35
lines changed

1 file changed

+18
-35
lines changed

compiler/src/dotty/tools/backend/sjs/JSExportsGen.scala

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,22 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
556556
genExportSameArgcRec(jsName, formalArgsRegistry, alts, paramIndex + 1, static, maxArgc)
557557
} else {
558558
// Sort them so that, e.g., isInstanceOf[String] comes before isInstanceOf[Object]
559-
val sortedAltsByTypeTest = topoSortDistinctsBy(altsByTypeTest)(_._1)
559+
val sortedAltsByTypeTest = topoSortDistinctsWith(altsByTypeTest) { (lhs, rhs) =>
560+
(lhs._1, rhs._1) match {
561+
// NoTypeTest is always last
562+
case (_, NoTypeTest) => true
563+
case (NoTypeTest, _) => false
564+
565+
case (PrimitiveTypeTest(_, rank1), PrimitiveTypeTest(_, rank2)) =>
566+
rank1 <= rank2
567+
568+
case (InstanceOfTypeTest(t1), InstanceOfTypeTest(t2)) =>
569+
t1 <:< t2
570+
571+
case (_: PrimitiveTypeTest, _: InstanceOfTypeTest) => true
572+
case (_: InstanceOfTypeTest, _: PrimitiveTypeTest) => false
573+
}
574+
}
560575

561576
val defaultCase = genThrowTypeError()
562577

@@ -944,46 +959,14 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
944959

945960
private case object NoTypeTest extends RTTypeTest
946961

947-
private object RTTypeTest {
948-
given PartialOrdering[RTTypeTest] with {
949-
override def tryCompare(lhs: RTTypeTest, rhs: RTTypeTest): Option[Int] = {
950-
if (lteq(lhs, rhs)) if (lteq(rhs, lhs)) Some(0) else Some(-1)
951-
else if (lteq(rhs, lhs)) Some(1) else None
952-
}
953-
954-
override def lteq(lhs: RTTypeTest, rhs: RTTypeTest): Boolean = {
955-
(lhs, rhs) match {
956-
// NoTypeTest is always last
957-
case (_, NoTypeTest) => true
958-
case (NoTypeTest, _) => false
959-
960-
case (PrimitiveTypeTest(_, rank1), PrimitiveTypeTest(_, rank2)) =>
961-
rank1 <= rank2
962-
963-
case (InstanceOfTypeTest(t1), InstanceOfTypeTest(t2)) =>
964-
t1 <:< t2
965-
966-
case (_: PrimitiveTypeTest, _: InstanceOfTypeTest) => true
967-
case (_: InstanceOfTypeTest, _: PrimitiveTypeTest) => false
968-
}
969-
}
970-
971-
override def equiv(lhs: RTTypeTest, rhs: RTTypeTest): Boolean = {
972-
lhs == rhs
973-
}
974-
}
975-
}
976-
977962
/** Very simple O(n²) topological sort for elements assumed to be distinct. */
978-
private def topoSortDistinctsBy[A <: AnyRef, B](coll: List[A])(f: A => B)(
979-
using ord: PartialOrdering[B]): List[A] = {
980-
963+
private def topoSortDistinctsWith[A <: AnyRef](coll: List[A])(lteq: (A, A) => Boolean): List[A] = {
981964
@tailrec
982965
def loop(coll: List[A], acc: List[A]): List[A] = {
983966
if (coll.isEmpty) acc
984967
else if (coll.tail.isEmpty) coll.head :: acc
985968
else {
986-
val (lhs, rhs) = coll.span(x => !coll.forall(y => (x eq y) || !ord.lteq(f(x), f(y))))
969+
val (lhs, rhs) = coll.span(x => !coll.forall(y => (x eq y) || !lteq(x, y)))
987970
assert(!rhs.isEmpty, s"cycle while ordering $coll")
988971
loop(lhs ::: rhs.tail, rhs.head :: acc)
989972
}

0 commit comments

Comments
 (0)