Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Avoid -Ycheck error by refining <:< testing
  • Loading branch information
odersky committed Jan 15, 2020
commit 744b5b07a15a94e97f82c31780985eb70de8b236
17 changes: 16 additions & 1 deletion compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
if (tp11.stripTypeVar eq tp12.stripTypeVar) recur(tp11, tp2)
else thirdTry
case tp1 @ OrType(tp11, tp12) =>

def joinOK = tp2.dealiasKeepRefiningAnnots match {
case tp2: AppliedType if !tp2.tycon.typeSymbol.isClass =>
// If we apply the default algorithm for `A[X] | B[Y] <: C[Z]` where `C` is a
Expand All @@ -427,6 +428,12 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
case _ =>
false
}

def containsAnd(tp: Type): Boolean = tp.dealiasKeepRefiningAnnots match
case tp: AndType => true
case OrType(tp1, tp2) => containsAnd(tp1) || containsAnd(tp2)
case _ => false

def widenOK =
(tp2.widenSingletons eq tp2) &&
(tp1.widenSingletons ne tp1) &&
Expand All @@ -435,7 +442,15 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
if (tp2.atoms.nonEmpty && canCompare(tp2.atoms))
tp1.atoms.nonEmpty && tp1.atoms.subsetOf(tp2.atoms)
else
widenOK || joinOK || recur(tp11, tp2) && recur(tp12, tp2)
widenOK
|| joinOK
|| recur(tp11, tp2) && recur(tp12, tp2)
|| containsAnd(tp1) && recur(tp1.join, tp2)
// An & on the left side loses information. Compensate by also trying the join.
// This is less ad-hoc than it looks since we produce joins in type inference,
// and then need to check that they are indeed supertypes of the original types
// under -Ycheck. Test case is i7965.scala.

case tp1: MatchType =>
val reduced = tp1.reduced
if (reduced.exists) recur(reduced, tp2) else thirdTry
Expand Down
4 changes: 3 additions & 1 deletion tests/pos/i7965.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ trait Y
trait Z

abstract class Test {
val x: Has[X] | (Has[Y] & Has[Z])
def x: Has[X] | (Has[Y] & Has[Z])
val y: Has[? >: (X & Y) | (X & Z) <: (X | Y) & (X | Z)] = x

def foo[T <: Has[_]](has: T): T = has
foo(x)
}
Expand Down