Skip to content

GADTs: widen only top-level variant params #11711

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 1 commit into from
Mar 12, 2021
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
18 changes: 8 additions & 10 deletions compiler/src/dotty/tools/dotc/core/PatternTypeConstrainer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,14 @@ trait PatternTypeConstrainer { self: TypeComparer =>
case _ => false
}

def widenVariantParams = new TypeMap {
def apply(tp: Type) = mapOver(tp) match {
case tp @ AppliedType(tycon, args) =>
val args1 = args.zipWithConserve(tycon.typeParams)((arg, tparam) =>
if (tparam.paramVarianceSign != 0) TypeBounds.empty else arg
)
tp.derivedAppliedType(tycon, args1)
case tp =>
tp
}
def widenVariantParams(tp: Type) = tp match {
case tp @ AppliedType(tycon, args) =>
val args1 = args.zipWithConserve(tycon.typeParams)((arg, tparam) =>
if (tparam.paramVarianceSign != 0) TypeBounds.empty else arg
)
tp.derivedAppliedType(tycon, args1)
case tp =>
tp
}

val widePt =
Expand Down
16 changes: 16 additions & 0 deletions tests/neg/i11565.gadt.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@main def test: Unit = {
trait TyCon[+A]
trait S[T]
trait P[T] extends S[TyCon[T]] {
def consume(t: T): Unit
}

def patmat(s: S[TyCon[Int]]) = s match {
case p: P[t] =>
p.consume("Hi") // error
}

patmat(new P[Int] {
override def consume(t: Int): Unit = t + 1
})
}
4 changes: 4 additions & 0 deletions tests/neg/invariant-gadt.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
object `invariant-gadt` {
case class Invariant[T](value: T)

def soundInPrinciple[T](i: Invariant[T]) : Int = i match {
case _: Invariant[Int] => i.value
}

def unsound0[T](t: T): T = Invariant(t) match {
case Invariant(_: Int) =>
(0: Any) // error
Expand Down