question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect union type exhaustivity warning (Nested pattern)

See original GitHub issue

Compiler version

3.0.1-RC1

Minimized code

import scala.reflect.Typeable

case class Err1()
case class Err2()

def handleError[A: Typeable, B: Typeable](x: Either[A | B, Nothing]): Unit =
  x match // false alarm warning: It would fail on pattern case: Left(_)
    case Left(e: A)  => println("A")
    case Left(_: B) => println("B")
    case Right(_)    => println("Nothing")

handleError[Err1, Err2](Left(Err1())) // prints A
handleError[Err1, Err2](Left(Err2())) // prints B

Output

match may not be exhaustive.

It would fail on pattern case: Left(_)

Expectation

As all cases are handled no warnings need to be generated.

Unrelated question, but shouldn’t removing Right be also fine? since it is of Nothing type? If you remove the last, the compiler gives out warning for Right pattern as well. (Looks like scala 2 has the same issue for this)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
liufengyuncommented, Jun 14, 2021

Unrelated question, but shouldn’t removing Right be also fine? since it is of Nothing type? If you remove the last, the compiler gives out warning for Right pattern as well. (Looks like scala 2 has the same issue for this)

Yes, conceptually it’s possible to remove a pattern if it only matches Nothing. Currently, we didn’t optimize for that, as it seems there is no need for it in practice.

Yep would be nice just for the sake of completeness and demo’ing scala

A PR is welcome: you can tweak the following two methods following the example for Unit and Boolean:

https://github.com/lampepfl/dotty/blob/229ec59c3b803c7004c988905a06c07b4f5e67d1/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala#L594-L670

1reaction
liufengyuncommented, Jun 14, 2021

@liufengyun this seems another case related to #12279 ?

@Swoorup Thanks for reporting. This seems to be fixed in master

Read more comments on GitHub >

github_iconTop Results From Across the Web

Incorrect union type exhaustivity warning #12279 - GitHub
No warnings since the union type match pattern is exhaustive. The text was updated successfully, but these errors were encountered: ...
Read more >
ocaml - Nested pattern matching not exhaustive warning
OCaml's pattern matching can check whether a set of patterns is exhaustive or not, based on the type only. and goes on to...
Read more >
Erroneous "non-exhaustive pattern match" using nested GADT ...
In mkOneConFull (which generates pattern-matching information for each constructor), record the type and strictness of each constructor's fields ...
Read more >
OCaml for the Skeptical: Pattern Matching - UChicago Library
Pattern matching comes up in several places in OCaml: as a powerful control ... that a match is non-exhaustive, and will give you...
Read more >
Incorrect exhaustive pattern matching of Enum or Union types
But the type checker should be able to know that the match is exhaustive over all of the Enum variants and not show...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found