Exhaustivity warning on opaque type

Hello everyone!

I’m getting an exhaustivity warning when matching on Message | Int, where Message is an opaque type.

Writing the TypeTest boilerplate was already cumbersome, but even with it defined, the compiler reports the match is not exhaustive for _: Package.Message.

[Scala 3.7.4]

Reproduction:

object Package:
  opaque type Message = String
  object Message:
    def apply(msg: String): Message = msg

  given scala.reflect.TypeTest[Any, Message] with
    def unapply(msg: Any): Option[msg.type & String] = msg match
      case s: (String & msg.type) => Some(s)
      case _                      => None

@main def main(): Unit =
  import Package.*

  def get: Message | Int = Message("test")

  get match
    case i: Int     => println(i)
    case s: Message => println(s)

Warning:

get match
^^^
match may not be exhaustive.

It would fail on pattern case: _: Package.Message
1 Like

Did you forget to do import Package.given maybe?
Recall that givens and other things have to be imported separately.

Edit: for me, the exhaustivity warning sometimes shows up on Scastie, sometimes does not… :person_shrugging:

The given is already in implicit scope (in the prefix of Message).

Probably the TypeTest isn’t handled. I noticed it’s not used for catch cases like ClassTag is.

It works with ClassTag.

given ClassTag[Message] = classTag[String]
1 Like

Does it mean that it’s a bug?