Skip to content

Commit b1d1fe8

Browse files
authored
Prioritise sequence-matches over product-sequence-matches (#19260)
2 parents fe55cef + 4b8e180 commit b1d1fe8

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,13 @@ object PatternMatcher {
360360
.map(ref(unappResult).select(_))
361361
matchArgsPlan(selectors, args, onSuccess)
362362
}
363+
else if (isUnapplySeq && unapplySeqTypeElemTp(unapp.tpe.widen.finalResultType).exists) {
364+
unapplySeqPlan(unappResult, args)
365+
}
363366
else if (isUnapplySeq && isProductSeqMatch(unapp.tpe.widen, args.length, unapp.srcPos)) {
364367
val arity = productArity(unapp.tpe.widen, unapp.srcPos)
365368
unapplyProductSeqPlan(unappResult, args, arity)
366369
}
367-
else if (isUnapplySeq && unapplySeqTypeElemTp(unapp.tpe.widen.finalResultType).exists) {
368-
unapplySeqPlan(unappResult, args)
369-
}
370370
else if unappResult.info <:< defn.NonEmptyTupleTypeRef then
371371
val components = (0 until foldApplyTupleType(unappResult.denot.info).length).toList.map(tupleApp(_, ref(unappResult)))
372372
matchArgsPlan(components, args, onSuccess)

tests/pos/i19221.orig.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object Test:
2+
class Custom extends scala.Product1[String]:
3+
def length: Int = ???
4+
def apply(i: Int): Boolean = ???
5+
def drop(n: Int): scala.Seq[Boolean] = ???
6+
def toSeq: scala.Seq[Boolean] = ???
7+
8+
def canEqual(that: Any): Boolean = ???
9+
10+
val _1: String = ???
11+
val _2: String = ???
12+
val _3: Seq[String] = ???
13+
14+
object A:
15+
def unapplySeq(i: Int): Custom = ???
16+
17+
val A(a, rest*) = 1

tests/pos/i19221.scala

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class T1
2+
3+
class P1
4+
final class P2
5+
class P3
6+
7+
class E1
8+
class E2 extends E1
9+
class E3 extends E1
10+
11+
object VarExt:
12+
def unapplySeq(t1: T1): U1 = new U1
13+
14+
class U1 extends Product1[P1]:
15+
def canEqual(that: Any): Boolean = ???
16+
17+
val _1: P1 = new P1
18+
val _2: P2 = new P2
19+
val _3: Seq[P3] = Seq(new P3)
20+
21+
def length: Int = ???
22+
def apply(i: Int): E1 = ???
23+
def drop(n: Int): Seq[E2] = ???
24+
def toSeq: Seq[E3] = ???
25+
26+
class Test:
27+
def m1(t1: T1): Unit = t1 match
28+
case VarExt(c1, cs*) => // CCE: class P1 cannot be cast to class E1
29+
val e1: E1 = c1
30+
val e1s: Seq[E1] = cs
31+
32+
object Main:
33+
def main(args: Array[String]): Unit =
34+
new Test().m1(new T1)
35+

0 commit comments

Comments
 (0)