From a3004fa8b5c4d891132c5b8bc1dafd7964a88484 Mon Sep 17 00:00:00 2001 From: noti0na1 <8036790+noti0na1@users.noreply.github.com> Date: Wed, 10 Apr 2024 12:58:51 +0000 Subject: [PATCH] Optimize comparing types in mergeRefinedOrApplied [Cherry-picked 4b711f584e37cd751b813d338a5f65ab09ba9fcb] --- compiler/src/dotty/tools/dotc/core/TypeOps.scala | 4 +++- tests/pos/i19789.scala | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i19789.scala diff --git a/compiler/src/dotty/tools/dotc/core/TypeOps.scala b/compiler/src/dotty/tools/dotc/core/TypeOps.scala index 6f323c1c0e4d..1f070d988318 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeOps.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeOps.scala @@ -255,7 +255,8 @@ object TypeOps: 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 { case RefinedType(parent2, `name1`, rinfo2) => @@ -279,6 +280,7 @@ object TypeOps: } case AndType(tp11, tp12) => mergeRefinedOrApplied(tp11, tp2) & mergeRefinedOrApplied(tp12, tp2) + case tp1: TypeParamRef if tp1 == tp2 => tp1 case _ => fail } } diff --git a/tests/pos/i19789.scala b/tests/pos/i19789.scala new file mode 100644 index 000000000000..24c3bdb1df8f --- /dev/null +++ b/tests/pos/i19789.scala @@ -0,0 +1,5 @@ +type Kinded[F[_]] = F[Any] | F[Nothing] + +def values[F[_]]: Vector[Kinded[F]] = ??? + +def mapValues[F[_], T](f: Kinded[F] => T): Vector[T] = values[F].map { case x => f(x) }