Skip to content

Commit 7648022

Browse files
committed
Replace topoSortDistinctsBy with topoSortDistinctsWith
1 parent 8a0655b commit 7648022

File tree

1 file changed

+19
-36
lines changed

1 file changed

+19
-36
lines changed

compiler/src/main/scala/org/scalajs/nscplugin/GenJSExports.scala

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,22 @@ trait GenJSExports[G <: Global with Singleton] extends SubComponent {
525525
} else {
526526
// Sort them so that, e.g., isInstanceOf[String]
527527
// comes before isInstanceOf[Object]
528-
val sortedAltsByTypeTest = topoSortDistinctsBy(
529-
altsByTypeTest)(_._1)(RTTypeTest.Ordering)
528+
val sortedAltsByTypeTest = topoSortDistinctsWith(altsByTypeTest) { (lhs, rhs) =>
529+
(lhs._1, rhs._1) match {
530+
// NoTypeTest is always last
531+
case (_, NoTypeTest) => true
532+
case (NoTypeTest, _) => false
533+
534+
case (PrimitiveTypeTest(_, rank1), PrimitiveTypeTest(_, rank2)) =>
535+
rank1 <= rank2
536+
537+
case (InstanceOfTypeTest(t1), InstanceOfTypeTest(t2)) =>
538+
t1 <:< t2
539+
540+
case (_: PrimitiveTypeTest, _: InstanceOfTypeTest) => true
541+
case (_: InstanceOfTypeTest, _: PrimitiveTypeTest) => false
542+
}
543+
}
530544

531545
val defaultCase = genThrowTypeError()
532546

@@ -946,47 +960,16 @@ trait GenJSExports[G <: Global with Singleton] extends SubComponent {
946960

947961
private case object NoTypeTest extends RTTypeTest
948962

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

0 commit comments

Comments
 (0)