Skip to content

Fix #6314: match type unsoundness #6319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 17, 2019
31 changes: 11 additions & 20 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1945,19 +1945,6 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
true
}
case (AppliedType(tycon1, args1), AppliedType(tycon2, args2)) if tycon1 == tycon2 =>
// Unboxed xs.zip(ys).zip(zs).forall { case ((a, b), c) => f(a, b, c) }
def zip_zip_forall[A, B, C](xs: List[A], ys: List[B], zs: List[C])(f: (A, B, C) => Boolean): Boolean = {
xs match {
case x :: xs => ys match {
case y :: ys => zs match {
case z :: zs => f(x, y, z) && zip_zip_forall(xs, ys, zs)(f)
case _ => true
}
case _ => true
}
case _ => true
}
}
def covariantIntersecting(tp1: Type, tp2: Type, tparam: TypeParamInfo): Boolean = {
intersecting(tp1, tp2) || {
// We still need to proof that `Nothing` is not a valid
Expand All @@ -1981,7 +1968,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
}
}

zip_zip_forall(args1, args2, tycon1.typeParams) {
(args1, args2, tycon1.typeParams).zipped.forall {
(arg1, arg2, tparam) =>
val v = tparam.paramVariance
if (v > 0)
Expand All @@ -1994,7 +1981,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
covariantIntersecting(arg1, arg2, tparam) && (isSameType(arg1, arg2) || {
// We can only trust a "no" from `isSameType` when both
// `arg1` and `arg2` are fully instantiated.
val fullyInstantiated = new TypeAccumulator[Boolean] {
def fullyInstantiated(tp: Type): Boolean = new TypeAccumulator[Boolean] {
override def apply(x: Boolean, t: Type) =
x && {
t match {
Expand All @@ -2003,9 +1990,8 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
case _ => foldOver(x, t)
}
}
}
!(fullyInstantiated.apply(true, arg1) &&
fullyInstantiated.apply(true, arg2))
}.apply(true, tp)
!(fullyInstantiated(arg1) && fullyInstantiated(arg2))
})
}
case (tp1: HKLambda, tp2: HKLambda) =>
Expand All @@ -2019,10 +2005,15 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
intersecting(tp1.tp1, tp2) || intersecting(tp1.tp2, tp2)
case (_, tp2: OrType) =>
intersecting(tp1, tp2.tp1) || intersecting(tp1, tp2.tp2)
case (tp1: AndType, tp2: AndType) =>
intersecting(tp1.tp1, tp2.tp1) && intersecting(tp1.tp2, tp2.tp2) ||
intersecting(tp1.tp1, tp2.tp2) && intersecting(tp1.tp2, tp2.tp1)
case (tp1: AndType, _) =>
intersecting(tp1.tp1, tp2) && intersecting(tp1.tp2, tp2) && intersecting(tp1.tp1, tp1.tp2)
intersecting(tp1.tp1, tp2) && intersecting(tp1.tp2, tp2) ||
intersecting(tp1.tp2, tp2) && intersecting(tp1.tp1, tp2)
case (_, tp2: AndType) =>
intersecting(tp1, tp2.tp1) && intersecting(tp1, tp2.tp2) && intersecting(tp2.tp1, tp2.tp2)
intersecting(tp1, tp2.tp1) && intersecting(tp1, tp2.tp2) ||
intersecting(tp1, tp2.tp2) && intersecting(tp1, tp2.tp1)
case (tp1: TypeProxy, tp2: TypeProxy) =>
intersecting(tp1.underlying, tp2) && intersecting(tp1, tp2.underlying)
case (tp1: TypeProxy, _) =>
Expand Down
2 changes: 1 addition & 1 deletion library/src-3.x/scala/compiletime/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ package object compiletime {
inline def constValue[T]: T = ???

type S[X <: Int] <: Int
}
}
14 changes: 14 additions & 0 deletions tests/neg/6314.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
object G {
final class X
final class Y

trait Test {
type Type
val i: Bar[Y & Type] = 1 // error
}

type Bar[A] = A match {
case X & Y => String
case Y => Int
}
}
2 changes: 1 addition & 1 deletion tests/run/tuples1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ object Test extends App {
val us0: 0 = size(())
val x3s1: 3 = size0(x3)
val us1: 0 = size0(())
}
}