Skip to content

Questionable warnings while matching the opaque type IArray #14704

Closed
@devlaam

Description

@devlaam

Observation

Compile and run the code with Scala 3.1.1: Scastie link

object MatchTest :
  val a: Array[String] = Array()
  val b: Array[String] = Array("x")
  val c: Array[String] = Array("a","b")

  def test(x: Array[String]) = x match
    case IArray()          => println("Empty")
    case IArray(i: String) => println("Caught")
    case _                 => println("Failure")

  def main(args: Array[String]): Unit =
    test(a)
    test(b)
    test(c)

Result of compilation:

[warn] -- Unchecked Warning: ...src/main/scala/Main.scala:7:9 
[warn] 7 |    case IArray()          => println("Empty")
[warn]   |         ^
[warn]   |         the type test for IArray[Any] cannot be checked at runtime
[warn] -- [E030] Match case Unreachable Warning: ...src/main/scala/Main.scala:8:9 
[warn] 8 |    case IArray(i: String) => println("Caught")
[warn]   |         ^^^^^^^^^^^^^^^^^
[warn]   |         Unreachable case

Result of running:

Empty
Caught
Failure

Expectation

Based on the runtime result i would say both warnings were not expected. The question remains if the opaque type IArray should match a normal Array at all. Probably it would do no harm, but it is somewhat counter intuitive, since IArray is derived from Array and not the other way around.

Discussion

  • I am uncertain if this qualifies as compiler bug or not.
  • This showcase can be used as a workaround for the bug Missed match on Array(1L) in Scala 3.1.1
  • If you compile with the option -source:future, Scastie link, the Unreachable Warning is replaced by an Unmatchable Type Warning, which indeed is more adequate. The question if an immutable type should be unmatchable should be discussed an other time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stat:needs triageEvery issue needs to have an "area" and "itype" label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions