Skip to content

Commit 1e30fc2

Browse files
authored
Merge pull request #11711 from dotty-staging/i11565
GADTs: widen only top-level variant params
2 parents 75b08ac + 2d8849a commit 1e30fc2

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

compiler/src/dotty/tools/dotc/core/PatternTypeConstrainer.scala

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,14 @@ trait PatternTypeConstrainer { self: TypeComparer =>
189189
case _ => false
190190
}
191191

192-
def widenVariantParams = new TypeMap {
193-
def apply(tp: Type) = mapOver(tp) match {
194-
case tp @ AppliedType(tycon, args) =>
195-
val args1 = args.zipWithConserve(tycon.typeParams)((arg, tparam) =>
196-
if (tparam.paramVarianceSign != 0) TypeBounds.empty else arg
197-
)
198-
tp.derivedAppliedType(tycon, args1)
199-
case tp =>
200-
tp
201-
}
192+
def widenVariantParams(tp: Type) = tp match {
193+
case tp @ AppliedType(tycon, args) =>
194+
val args1 = args.zipWithConserve(tycon.typeParams)((arg, tparam) =>
195+
if (tparam.paramVarianceSign != 0) TypeBounds.empty else arg
196+
)
197+
tp.derivedAppliedType(tycon, args1)
198+
case tp =>
199+
tp
202200
}
203201

204202
val widePt =

tests/neg/i11565.gadt.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@main def test: Unit = {
2+
trait TyCon[+A]
3+
trait S[T]
4+
trait P[T] extends S[TyCon[T]] {
5+
def consume(t: T): Unit
6+
}
7+
8+
def patmat(s: S[TyCon[Int]]) = s match {
9+
case p: P[t] =>
10+
p.consume("Hi") // error
11+
}
12+
13+
patmat(new P[Int] {
14+
override def consume(t: Int): Unit = t + 1
15+
})
16+
}

tests/neg/invariant-gadt.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
object `invariant-gadt` {
22
case class Invariant[T](value: T)
33

4+
def soundInPrinciple[T](i: Invariant[T]) : Int = i match {
5+
case _: Invariant[Int] => i.value
6+
}
7+
48
def unsound0[T](t: T): T = Invariant(t) match {
59
case Invariant(_: Int) =>
610
(0: Any) // error

0 commit comments

Comments
 (0)