scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Exhausted by warnings #12871

Closed som-snytt closed 8 months ago

som-snytt commented 1 year ago

Reproduction steps

Scala version: 2.13.12

final case class Tombstone(distance: Int)

final class V[K](val fields: Vector[Any]) {
  def f(slot: Int): (Int, K) =
    fields(slot) match {
      case Tombstone(distance) => f(slot + distance)
      case k: K @unchecked     => (slot, k)
      case null                => (slot, null.asInstanceOf[K])
    }
}

Problem

scalac -Xlint exhausted.scala
exhausted.scala:6: warning: match may not be exhaustive.
It would fail on the following input: (x: Any forSome x not in Tombstone)
    fields(slot) match {
          ^
1 warning

This is likely a duplicate of some other ticket where they expected either no warning or a plausibly accurate one.

In this case, the Vector is really Vector[Tombstone | K | Null]. I guess the three cases are Tombstone, not-Null (since K is unchecked), and null. So I expect no warning.

The IRL roundtrip was through @nowarn("cat=other-match-analysis&msg=match may not be exhaustive") and then changing the null case to case nullish @ _ => and then remembering to delete the annotation.

sjrd commented 1 year ago

I agree with the compiler. The match only handles Tombstone, K and null. It doesn't handle all the other values.

@unchecked is not a cast that says: "actually match an Any then cast to K". It shouldn't be used as one. What it does is to tell the compiler: "match a K, but don't worry if you're not able to emit all the necessary tests, just do the ones you can".

Using @unchecked instead of casts is bound to end up in this kind of issues.

som-snytt commented 1 year ago

The other part of the two-part ticket is that the message should be a reasonable approximation of actionable. I'd rather it just said, "There's something fishy about your match, but darned if I know what."

I don't mind whatever you call mechanical sympathy for pattern matching.

(But I didn't try any debug options if there is additional output.)

Also, thanks for the helpful clarification. I thought I was thinking about unchecked correctly, because you are literally quoting me when I patted the compiler on the shoulder and said, "Don't worry about it, just do your best." But maybe I had an ulterior motive.