Skip to content

Unexpected semantics of non-nested varargs pattern #17443

Closed
@prolativ

Description

@prolativ

Compiler version

3.3.1-RC1-bin-20230508-830230f-NIGHTLY and before

Minimized code

@main def run() =
  val x = List(1) match { case (xs*) => xs }
  val y = x.head
@main def run() =
  val x = List(1) match { case (xs*) => xs; case _ => Seq.empty }
  val y = x.head

Output

Compiling project (Scala 3.3.1-RC1-bin-20230508-830230f-NIGHTLY, JVM)
[warn] /var/folders/rp/f9y80jvs54lcy26rxnt2y2kw0000gn/T/Repro.scala:2:11
[warn] match may not be exhaustive.
[warn] 
[warn] It would fail on pattern case: List(_, _*), Nil
[warn]   val x = List(1) match { case (xs*) => xs }
[warn]           ^^^^^^^
Compiled project (Scala 3.3.1-RC1-bin-20230508-830230f-NIGHTLY, JVM)
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to scala.collection.immutable.List
        at Repro$package$.run(Repro.scala:3)
        at run.main(Repro.scala:1)
[warn] /var/folders/rp/f9y80jvs54lcy26rxnt2y2kw0000gn/T/Repro.scala:2:33
[warn] Unreachable case
[warn]   val x = List(1) match { case (xs*) => xs; case _ => Seq.empty }
[warn]                                 ^^^
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to scala.collection.immutable.List
        at Repro$package$.run(Repro.scala:3)
        at run.main(Repro.scala:1)

Expectation

Most probably patterns like case (xs*) => should be illegal, just as case xs* => is. @odersky Can you confirm?

In the code snippets above the type of x gets inferred to Seq[List[Int]] but its value is List(1), which doesn't make sense and causes the ClassCastException.
Also, the warning from the second snippet is wrong at the moment as the case reported as unreachable is actually the one that gets matched.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions