Skip to content

Questionable warnings while matching the opaque type IArray #14704

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

Closed
devlaam opened this issue Mar 17, 2022 · 2 comments
Closed

Questionable warnings while matching the opaque type IArray #14704

devlaam opened this issue Mar 17, 2022 · 2 comments
Labels
stat:needs triage Every issue needs to have an "area" and "itype" label

Comments

@devlaam
Copy link

devlaam commented Mar 17, 2022

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.
@devlaam devlaam added the stat:needs triage Every issue needs to have an "area" and "itype" label label Mar 17, 2022
@odersky
Copy link
Contributor

odersky commented Mar 19, 2022

I think this is as expected.

@odersky odersky closed this as completed Mar 19, 2022
@nicolasstucki
Copy link
Contributor

Should it be an unchecked warning instead of unreachable?

Because it is unreachable as long as it can be type tested. Maybe we should emit both warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:needs triage Every issue needs to have an "area" and "itype" label
Projects
None yet
Development

No branches or pull requests

3 participants