scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

False non-exhaustive match report.

Open ftucky opened this issue 2 years ago • 2 comments

Compiler version

3.3.1-RC3

Minimized code

sealed trait A

trait X extends A
trait Y extends A

trait Visitor:
  type Precise[T]
  def dispatch(a:A) : Precise[a.type] =
    (a:a.type) match // <--- match may not be exhaustive
      case x:(X & a.type) => visit(x)
      case y:(Y & a.type) => visit(y)

  def visit(x:X) : Precise[x.type]
  def visit(y:Y) : Precise[y.type]

Output

match may not be exhaustive.
It would fail on pattern case: _: X, _: Y

Expectation

No warning issued. Any _:X is also a _:a.type, and will match the first case.

Context

This is a visitor pattern, where the visit method is a dependent-function. The intention is to write : case x:X => visit(x), in which case,x is widened to X, loosing the dependent type.

case x:(X & a.type) => visit(x) preserves the dependent type, ... but fools the exhaustivity checker.

ftucky avatar Jul 12 '23 14:07 ftucky

@dwijnand do you think that could be an appropriate issue for a future spree?

mbovel avatar Feb 19 '24 14:02 mbovel

Yeah. It's not the best, but it can be an option.

dwijnand avatar Feb 19 '24 15:02 dwijnand