Skip to content

Commit 2f16cc7

Browse files
committed
Extract GADT constraints from wildcard type arguments
1 parent aea38cf commit 2f16cc7

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,13 @@ trait PatternTypeConstrainer { self: TypeComparer =>
256256
val variance = param.paramVarianceSign
257257
if variance != 0 && !assumeInvariantRefinement then true
258258
else if argS.isInstanceOf[TypeBounds] || argP.isInstanceOf[TypeBounds] then
259-
// Passing TypeBounds to isSubType on LHS or RHS does the
260-
// incorrect thing and infers unsound constraints, while simply
261-
// returning true is sound. However, I believe that it should
262-
// still be possible to extract useful constraints here.
263-
// TODO extract GADT information out of wildcard type arguments
264-
true
259+
// This line was added here as a quick fix for issue #13998,
260+
// to extract GADT constraints from wildcard type arguments.
261+
// The proper fix would involve inspecting the bounds right here and performing the
262+
// correct subtyping checks, the ones that are already performed by `isSubType` below,
263+
// for the same reasons for which we stopped using `SkolemType` here to begin with
264+
// (commit 10fe5374dc2d).
265+
isSubType(SkolemType(patternTp), scrutineeTp)
265266
else {
266267
var res = true
267268
if variance < 1 then res &&= isSubType(argS, argP)

tests/pos/i13998.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
case class Box[V](value: V)
2+
object Box:
3+
def apply[A](a: A): Box[A] = new Box[A](a)
4+
def unapply[U](b: Box[U]): Box[U] = b
5+
6+
class Test:
7+
def value: Box[_ <: String] = Box("text")
8+
9+
def test: String = value match
10+
case Box(text) => text: String

0 commit comments

Comments
 (0)