Skip to content

Commit 3208516

Browse files
committed
Enable GADT matching for pattern values
So far, only typed patterns an dunapply had GADT matching. i1737.scala shows that we need to do the same thing for objects.
1 parent 781fb87 commit 3208516

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
18301830
ctx.typeComparer.GADTused = false
18311831
if (ctx.mode is Mode.Pattern) {
18321832
tree match {
1833-
case _: RefTree | _: Literal if !isVarPattern(tree) =>
1833+
case _: RefTree | _: Literal
1834+
if !isVarPattern(tree) &&
1835+
!(tree.tpe <:< pt)(ctx.addMode(Mode.GADTflexible)) =>
18341836
checkCanEqual(pt, wtp, tree.pos)(ctx.retractMode(Mode.Pattern))
18351837
case _ =>
18361838
}

tests/pos/i1737.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Test {
2+
sealed trait Foo[A]
3+
case object FooI extends Foo[Int]
4+
case class FooS(b: Boolean) extends Foo[String]
5+
6+
def algFoo[A](foo: Foo[A]): A =
7+
foo match {
8+
case FooI => 42
9+
case FooS(b) => "foo"
10+
}
11+
}

0 commit comments

Comments
 (0)