Skip to content

Commit 7db2746

Browse files
committed
Approximate MatchTypes with lub of case bodies, if non-recursive
[Cherry-picked 8f2b6cf][modified]
1 parent 27f0201 commit 7db2746

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,17 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
978978
if (tp1 ne tp1norm) recur(tp1norm, tp2)
979979
else either(recur(tp11, tp2), recur(tp12, tp2))
980980
case tp1: MatchType =>
981+
def compareUpper =
982+
val lub1 = tp1.cases.foldLeft(defn.NothingType: Type): (acc, case1) =>
983+
if acc.exists then
984+
val rhs = case1.resultType match { case defn.MatchCase(_, body) => body }
985+
val isRecursive = rhs.existsPart(_.isInstanceOf[LazyRef])
986+
if isRecursive then NoType else lub(acc, rhs)
987+
else acc
988+
if lub1.exists then
989+
recur(lub1, tp2)
990+
else
991+
recur(tp1.underlying, tp2)
981992
def compareMatch = tp2 match {
982993
case tp2: MatchType =>
983994
isSameType(tp1.scrutinee, tp2.scrutinee) &&

tests/pos/i19710.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import scala.util.NotGiven
2+
3+
type HasName1 = [n] =>> [x] =>> x match {
4+
case n => true
5+
case _ => false
6+
}
7+
@main def Test = {
8+
summon[HasName1["foo"]["foo"] =:= true]
9+
summon[NotGiven[HasName1["foo"]["bar"] =:= true]]
10+
summon[Tuple.Filter[(1, "foo", 2, "bar"), HasName1["foo"]] =:= Tuple1["foo"]] // error
11+
}

0 commit comments

Comments
 (0)