Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritise sequence-matches over product-sequence-matches #19260

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,13 @@ object PatternMatcher {
.map(ref(unappResult).select(_))
matchArgsPlan(selectors, args, onSuccess)
}
else if (isUnapplySeq && unapplySeqTypeElemTp(unapp.tpe.widen.finalResultType).exists) {
unapplySeqPlan(unappResult, args)
}
else if (isUnapplySeq && isProductSeqMatch(unapp.tpe.widen, args.length, unapp.srcPos)) {
val arity = productArity(unapp.tpe.widen, unapp.srcPos)
unapplyProductSeqPlan(unappResult, args, arity)
}
else if (isUnapplySeq && unapplySeqTypeElemTp(unapp.tpe.widen.finalResultType).exists) {
unapplySeqPlan(unappResult, args)
}
else if unappResult.info <:< defn.NonEmptyTupleTypeRef then
val components = (0 until foldApplyTupleType(unappResult.denot.info).length).toList.map(tupleApp(_, ref(unappResult)))
matchArgsPlan(components, args, onSuccess)
Expand Down
17 changes: 17 additions & 0 deletions tests/pos/i19221.orig.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
object Test:
class Custom extends scala.Product1[String]:
def length: Int = ???
def apply(i: Int): Boolean = ???
def drop(n: Int): scala.Seq[Boolean] = ???
def toSeq: scala.Seq[Boolean] = ???

def canEqual(that: Any): Boolean = ???

val _1: String = ???
val _2: String = ???
val _3: Seq[String] = ???

object A:
def unapplySeq(i: Int): Custom = ???

val A(a, rest*) = 1
35 changes: 35 additions & 0 deletions tests/pos/i19221.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class T1

class P1
final class P2
class P3

class E1
class E2 extends E1
class E3 extends E1

object VarExt:
def unapplySeq(t1: T1): U1 = new U1

class U1 extends Product1[P1]:
def canEqual(that: Any): Boolean = ???

val _1: P1 = new P1
val _2: P2 = new P2
val _3: Seq[P3] = Seq(new P3)

def length: Int = ???
def apply(i: Int): E1 = ???
def drop(n: Int): Seq[E2] = ???
def toSeq: Seq[E3] = ???

class Test:
def m1(t1: T1): Unit = t1 match
case VarExt(c1, cs*) => // CCE: class P1 cannot be cast to class E1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this test be a run test, in order to exercise this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do. At some point I was curious as to what the code would do if I made it compile without the TypeTestCasts compile error, and it indeed threw. But this part of the code I just left as potentially useful debugging if returned to. But we can also make it actually part of the test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not actually part of testing #19221 per se, then it can be left as is.

val e1: E1 = c1
val e1s: Seq[E1] = cs

object Main:
def main(args: Array[String]): Unit =
new Test().m1(new T1)