Skip to content

Commit 5b7c0d1

Browse files
committed
Fix #2104: Instantiate unapply result type variables
Type variables in an unapply result should be fully instantiated before nested pattern matches. This is analogous to instantiating the scrutinee type of a match.
1 parent 6472ae3 commit 5b7c0d1

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
920920
val ownType =
921921
if (selType <:< unapplyArgType) {
922922
unapp.println(i"case 1 $unapplyArgType ${ctx.typerState.constraint}")
923+
fullyDefinedType(unapplyArgType, "pattern selector", tree.pos)
923924
selType
924925
} else if (isSubTypeOfParent(unapplyArgType, selType)(ctx.addMode(Mode.GADTflexible))) {
925926
maximizeType(unapplyArgType) match {
@@ -952,7 +953,6 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
952953
ex"Pattern type $unapplyArgType is neither a subtype nor a supertype of selector type $selType",
953954
tree.pos)
954955
}
955-
956956
val dummyArg = dummyTreeOfType(ownType)
957957
val unapplyApp = typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
958958
val unapplyImplicits = unapplyApp match {

tests/pos/i2104.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
case class Pair[A, B](_1: A, _2: B)
2+
3+
trait Cons[+H, +T]
4+
5+
object Cons {
6+
def apply[H, T](h: H, t: T): Cons[H, T] = ???
7+
def unapply[H, T](t: Cons[H, T]): Option[Pair[H, T]] = ???
8+
}
9+
10+
11+
12+
object Test {
13+
def main(args: Array[String]): Unit = {
14+
Cons(Option(1), None) match {
15+
case Cons(Some(i), None) =>
16+
i: Int // error: found: Any(i), requires: Int
17+
assert(i == 1)
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)