Skip to content

Commit ca6934b

Browse files
committed
Refactor and optimize mergeEntries
Also move test for `eq` first.
1 parent 23464fd commit ca6934b

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

compiler/src/dotty/tools/dotc/core/OrderingConstraint.scala

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -519,32 +519,21 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
519519
def mergeParams(ps1: List[TypeParamRef], ps2: List[TypeParamRef]) =
520520
(ps1 /: ps2)((ps1, p2) => if (ps1.contains(p2)) ps1 else p2 :: ps1)
521521

522-
@tailrec
523-
def mergeEntries(e1: Type, e2: Type, retry: Boolean = true): Type = e1 match {
524-
case e1: TypeBounds =>
525-
e2 match {
526-
case e2: TypeBounds => e1 & e2
527-
case _ if e1 contains e2 => e2
528-
case _ => mergeError
529-
}
530-
case tv1: TypeVar =>
531-
e2 match {
532-
case tv2: TypeVar if tv1.instanceOpt eq tv2.instanceOpt => e1
533-
case _ => mergeError
534-
}
522+
// Must be symmetric
523+
def mergeEntries(e1: Type, e2: Type): Type =
524+
(e1, e2) match {
535525
case _ if e1 eq e2 => e1
536-
case _ if retry =>
537-
// mergeEntries must be commutative
538-
mergeEntries(e2, e1, false)
526+
case (e1: TypeBounds, e2: TypeBounds) => e1 & e2
527+
case (e1: TypeBounds, _) if e1 contains e2 => e2
528+
case (_, e2: TypeBounds) if e2 contains e1 => e1
529+
case (tv1: TypeVar, tv2: TypeVar) if tv1.instanceOpt eq tv2.instanceOpt => e1
539530
case _ =>
540-
mergeError
541-
}
542-
543-
def mergeError = throw new AssertionError(i"cannot merge $this with $other")
531+
throw new AssertionError(i"cannot merge $this with $other, mergeEntries($e1, $e2) failed")
532+
}
544533

545534
val that = other.asInstanceOf[OrderingConstraint]
546535
new OrderingConstraint(
547-
merge(this.boundsMap, that.boundsMap, mergeEntries(_, _)),
536+
merge(this.boundsMap, that.boundsMap, mergeEntries),
548537
merge(this.lowerMap, that.lowerMap, mergeParams),
549538
merge(this.upperMap, that.upperMap, mergeParams))
550539
}

0 commit comments

Comments
 (0)