Incorrect union type exhaustivity warning (Nested pattern)
See original GitHub issueCompiler 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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
A PR is welcome: you can tweak the following two methods following the example for
Unit
andBoolean
:https://github.com/lampepfl/dotty/blob/229ec59c3b803c7004c988905a06c07b4f5e67d1/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala#L594-L670
@Swoorup Thanks for reporting. This seems to be fixed in master