From 445195fed196655593c3971ffd377cae9484ac92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Zieli=C5=84ski?= Date: Sun, 26 Oct 2025 13:47:40 +0100 Subject: [PATCH] treat `x*` pattern arg as a Seq of the element type. --- .../src/dotty/tools/dotc/transform/patmat/Space.scala | 7 ++++++- tests/warn/i23459.check | 6 ++++++ tests/warn/i23459.scala | 8 ++++++++ tests/warn/i23459b.check | 6 ++++++ tests/warn/i23459b.scala | 8 ++++++++ 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/warn/i23459.check create mode 100644 tests/warn/i23459.scala create mode 100644 tests/warn/i23459b.check create mode 100644 tests/warn/i23459b.scala diff --git a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala index 2a0cbbdbf006..446fd4780acd 100644 --- a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala +++ b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala @@ -382,7 +382,12 @@ object SpaceEngine { project(pat) case Typed(_, tpt) => - Typ(erase(tpt.tpe.stripAnnots, isValue = true, isTyped = true), decomposed = false) + val tptTpe = + if isWildcardStarArg(pat) then + tpt.tpe.translateFromRepeated(toArray = false) + else + tpt.tpe + Typ(erase(tptTpe.stripAnnots, isValue = true, isTyped = true), decomposed = false) case This(_) => Typ(pat.tpe.stripAnnots, decomposed = false) diff --git a/tests/warn/i23459.check b/tests/warn/i23459.check new file mode 100644 index 000000000000..bbe9bf5a4ec7 --- /dev/null +++ b/tests/warn/i23459.check @@ -0,0 +1,6 @@ +-- [E092] Pattern Match Unchecked Warning: tests/warn/i23459.scala:7:14 ------------------------------------------------ +7 | case Test(x*) => println(x) // warn + | ^ + |the type test for Seq[Int]* cannot be checked at runtime because its type arguments can't be determined from Seq[Int] + | + | longer explanation available when compiling with `-explain` diff --git a/tests/warn/i23459.scala b/tests/warn/i23459.scala new file mode 100644 index 000000000000..af6a01e2ebce --- /dev/null +++ b/tests/warn/i23459.scala @@ -0,0 +1,8 @@ +case class Test() +object Test: + def unapply(t: Test): Some[Seq[Int]] = Some(Seq(1, 2)) + +@main def run(): Unit = + Test() match + case Test(x*) => println(x) // warn + diff --git a/tests/warn/i23459b.check b/tests/warn/i23459b.check new file mode 100644 index 000000000000..cd82f2005906 --- /dev/null +++ b/tests/warn/i23459b.check @@ -0,0 +1,6 @@ +-- [E092] Pattern Match Unchecked Warning: tests/warn/i23459b.scala:7:14 ----------------------------------------------- +7 | case Test(_*) => () // warn + | ^ + |the type test for Seq[Int]* cannot be checked at runtime because its type arguments can't be determined from Seq[Int] + | + | longer explanation available when compiling with `-explain` diff --git a/tests/warn/i23459b.scala b/tests/warn/i23459b.scala new file mode 100644 index 000000000000..792afafe038e --- /dev/null +++ b/tests/warn/i23459b.scala @@ -0,0 +1,8 @@ +case class Test() +object Test: + def unapply(t: Test): Some[Seq[Int]] = Some(Seq(1, 2)) + +@main def run(): Unit = + Test() match + case Test(_*) => () // warn +