Skip to content

Commit

Permalink
Optimize comparing types in mergeRefinedOrApplied
Browse files Browse the repository at this point in the history
  • Loading branch information
noti0na1 committed Apr 7, 2024
1 parent 991a3cc commit 97bf522
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -244,44 +244,36 @@ object TypeOps:

def mergeRefinedOrApplied(tp1: Type, tp2: Type): Type = {
def fail = throw new AssertionError(i"Failure to join alternatives $tp1 and $tp2")
def fallback = tp2 match

tp2 match
case AndType(tp21, tp22) =>
mergeRefinedOrApplied(tp1, tp21) & mergeRefinedOrApplied(tp1, tp22)
return mergeRefinedOrApplied(tp1, tp21) & mergeRefinedOrApplied(tp1, tp22)
case _ =>
fail
tp1 match {

if tp1 eq tp2 then tp1
else tp1 match
case tp1 @ RefinedType(parent1, name1, rinfo1) =>
tp2 match {
tp2 match
case RefinedType(parent2, `name1`, rinfo2) =>
tp1.derivedRefinedType(
mergeRefinedOrApplied(parent1, parent2), name1, rinfo1 | rinfo2)
case _ => fallback
}
case _ => fail
case tp1 @ AppliedType(tycon1, args1) =>
tp2 match {
tp2 match
case AppliedType(tycon2, args2) =>
tp1.derivedAppliedType(
mergeRefinedOrApplied(tycon1, tycon2),
TypeComparer.lubArgs(args1, args2, tycon1.typeParams))
case _ => fallback
}
case _ => fail
case tp1 @ TypeRef(pre1, _) =>
tp2 match {
tp2 match
case tp2 @ TypeRef(pre2, _) if tp1.name eq tp2.name =>
tp1.derivedSelect(pre1 | pre2)
case _ => fallback
}
case tp1: TypeParamRef =>
tp2 match {
case tp2: TypeParamRef
if (tp1.binder eq tp2.binder) && (tp1.paramNum == tp2.paramNum) =>
tp1
case _ => fallback
}
case _ => fail
case AndType(tp11, tp12) =>
mergeRefinedOrApplied(tp11, tp2) & mergeRefinedOrApplied(tp12, tp2)
case tp1: TypeParamRef if tp1 == tp2 => tp1
case _ => fail
}
}

def approximateOr(tp1: Type, tp2: Type): Type = {
Expand Down

0 comments on commit 97bf522

Please sign in to comment.