Skip to content

Commit

Permalink
Approximate MatchTypes with lub of case bodies, if non-recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Feb 22, 2024
1 parent 898ce33 commit 8f2b6cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
13 changes: 12 additions & 1 deletion compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,17 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
if tp1 ne tp1norm then recur(tp1norm, tp2)
else either(recur(tp11, tp2), recur(tp12, tp2))
case tp1: MatchType =>
def compareUpper =
val lub1 = tp1.cases.foldLeft(defn.NothingType: Type): (acc, case1) =>
if acc.exists then
val rhs = case1.resultType match { case defn.MatchCase(_, body) => body }
val isRecursive = rhs.existsPart(_.isInstanceOf[LazyRef])
if isRecursive then NoType else lub(acc, rhs)
else acc
if lub1.exists then
recur(lub1, tp2)
else
recur(tp1.underlying, tp2)
def compareMatch = tp2 match {
case tp2: MatchType =>
// we allow a small number of scrutinee types to be widened:
Expand All @@ -1047,7 +1058,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
tp1.cases.corresponds(tp2.cases)(isSubType)
case _ => false
}
(!caseLambda.exists || canWidenAbstract) && recur(tp1.underlying, tp2) || compareMatch
(!caseLambda.exists || canWidenAbstract) && compareUpper || compareMatch
case tp1: AnnotatedType if tp1.isRefining =>
isNewSubType(tp1.parent)
case JavaArrayType(elem1) =>
Expand Down
11 changes: 11 additions & 0 deletions tests/pos/i19710.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.util.NotGiven

type HasName1 = [n] =>> [x] =>> x match {
case n => true
case _ => false
}
@main def Test = {
summon[HasName1["foo"]["foo"] =:= true]
summon[NotGiven[HasName1["foo"]["bar"] =:= true]]
summon[Tuple.Filter[(1, "foo", 2, "bar"), HasName1["foo"]] =:= Tuple1["foo"]] // error
}

0 comments on commit 8f2b6cf

Please sign in to comment.