Missed match on Array(1L) in Scala 3.1.1
See original GitHub issueObservation
Consider the following code:
object MatchTest {
val a: Array[Long] = Array(1L)
def test(x: Any) = x match {
case Array(i: Long) => println("Success!")
case _ => println("Failure!") }
def main(args: Array[String]): Unit = test(a) }
When run under Scala 2.13.8 this produces Success
whereas under Scala 3.1.1 it produces Failure
. For the latter it does not matter if the compiler option -source:3.0-migration
is used.
Expectation
Same behaviour under both versions or (at least) a warning that the match will fail. Current situation significantly changes runtime behaviour after migrating projects from Scala 2 to Scala 3.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Breaking match change from Scala 2 to 3 that is too silent
Consider the following code: object MatchTest { val a: Array[Long] = Array(1L) def test(x: Any) = x match { case Array(i: Long) ...
Read more >Pattern matching on arrays doesn't warn for missing cases
The match in this case is exhaustive because unapplying a NonEmptySeq will always produce at least one value, but the compiler can't see...
Read more >Spark 3.1.1 ScalaDoc - org.apache.spark.sql.functions
Merge two given arrays, element-wise, into a single array using a function.
Read more >Querydsl Reference Guide
3.1.1. Complex predicates; 3.1.2. Dynamic expressions; 3.1.3. ... imports, Array of java imports added to generated query classes: com.bar for package ...
Read more >org.apache.spark.sql.execution.ui.SQLAppStatusListener ...
SQLAppStatusListener.scala maven / gradle build tool code. ... Group: com.github.immuta.hadoop ➦ Artifact: spark-sql_2.12 ➦ Version: 3.1.1-hadoop-2.7.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think the problem is with the definition of Array.unapplySeq, if I tweak it like this:
Then the pattern match succeeds. Otherwise, depending on how unapplySeq is instantiated, we’ll end up matching either all arrays or only arrays of objects, because
Array[Any]
erases toObject[]
. Note that the behavior of Scala 2 here seems to be a straight-up bug: if I look at the generated tree it tries to do anisInstanceOf
check onArray[T]
but there is noT
in scope (as pointed out by-Ycheck:pat
), but luckily for Scala 2, it happens to erase this malformed type toObject
so the check succeeds.I believe this would be a binary-compatible change, so there’s a chance we could just apply this diff in the Scala 2 standard library.