Skip to content

Fix #2971: Soudness issue with variance and higher kinded types #2975

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 2 commits into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
// of an `AndType` can lead to a cascade of subtyping checks
// This twist is needed to make collection/generic/ParFactory.scala compile
fourthTry(tp1, tp2) || compareRefinedSlow
case tp1: HKTypeLambda =>
// HKTypeLambdas do not have members.
fourthTry(tp1, tp2)
case _ =>
compareRefinedSlow || fourthTry(tp1, tp2)
}
Expand Down
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ object Types {
case _ => this1.symbol eq sym
}
case this1: RefinedOrRecType => this1.parent.isRef(sym)
case this1: HKApply => this1.superType.isRef(sym)
case this1: HKApply =>
val this2 = this1.dealias
if (this2 ne this1) this2.isRef(sym)
else this1.underlying.isRef(sym)
case _ => false
}

Expand Down
20 changes: 20 additions & 0 deletions tests/neg/i2971.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
case class Foo[+X[_]](will: X[Int]) {
def foo[Y[_]](right: Foo[Y]) = Foo.doFoo(this, right)
}

class A[X] { def crash = true }
class B[X]

object Foo {
def doFoo[X[_]](left: Foo[X], right: Foo[X]): Foo[X] = right

def main(args: Array[String]): Unit = {
val fooA = Foo(new A[Int])
val fooB = Foo(new B[Int])
// The type for this is inferred correctly to Foo[A|B]
val fine = doFoo(fooA, fooB)
// This throws a ClassCastException because fooB isn't a Foo[A]
val oops: Foo[A] = fooA.foo(fooB) // error: found: Foo[B], required: Foo[A]
println(oops.will.crash)
}
}