Skip to content

Commit

Permalink
Merge pull request #14132 from dwijnand/constrain-wildcards
Browse files Browse the repository at this point in the history
Extract GADT constraints from wildcard type arguments
  • Loading branch information
dwijnand authored Jan 24, 2022
2 parents 26dbd77 + 2f16cc7 commit 1595cae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
13 changes: 7 additions & 6 deletions compiler/src/dotty/tools/dotc/core/PatternTypeConstrainer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,13 @@ trait PatternTypeConstrainer { self: TypeComparer =>
val variance = param.paramVarianceSign
if variance != 0 && !assumeInvariantRefinement then true
else if argS.isInstanceOf[TypeBounds] || argP.isInstanceOf[TypeBounds] then
// Passing TypeBounds to isSubType on LHS or RHS does the
// incorrect thing and infers unsound constraints, while simply
// returning true is sound. However, I believe that it should
// still be possible to extract useful constraints here.
// TODO extract GADT information out of wildcard type arguments
true
// This line was added here as a quick fix for issue #13998,
// to extract GADT constraints from wildcard type arguments.
// The proper fix would involve inspecting the bounds right here and performing the
// correct subtyping checks, the ones that are already performed by `isSubType` below,
// for the same reasons for which we stopped using `SkolemType` here to begin with
// (commit 10fe5374dc2d).
isSubType(SkolemType(patternTp), scrutineeTp)
else {
var res = true
if variance < 1 then res &&= isSubType(argS, argP)
Expand Down
10 changes: 10 additions & 0 deletions tests/pos/i13998.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
case class Box[V](value: V)
object Box:
def apply[A](a: A): Box[A] = new Box[A](a)
def unapply[U](b: Box[U]): Box[U] = b

class Test:
def value: Box[_ <: String] = Box("text")

def test: String = value match
case Box(text) => text: String

0 comments on commit 1595cae

Please sign in to comment.