No warning is triggered for non-exhaustive pattern matching on case class field
See original GitHub issueCompiler version
3.0.0-RC3
from print scalaVersion
on sbt
console.
Minimized code
sealed trait Status
object Status {
case object Active extends Status
case object Inactive extends Status
}
final case class Foo(status: Status)
def bar(user: Foo): Unit = user match {
case Foo(Status.Active) =>
println("active")
// no compile-time warning for missing Status.Inactive case
}
scalacOptions
has
List(
"-unchecked",
"-deprecation",
"-feature",
"-Xfatal-warnings"
)
NOTE:
-
Replacing the
Status
above with theenum
like below doesn’t make any difference.enum Status { case Active case Inactive }
-
final case class
(i.e.final case class Foo(status: Status)
) makes no difference.
Output
It compiles without any warning or error even with -Xfatal-warnings
, and it results in the following runtime error.
[error] (run-main-1) scala.MatchError: Foo(Inactive) (of class kevinlee.Example1$Foo)
[error] scala.MatchError: Foo(Inactive) (of class kevinlee.Example1$Foo)
...
Expectation
The compile-time warning like
match may not be exhaustive.
It would fail on the following input: Foo(Inactive)
def bar(user: Foo): Unit = user match {
(or the compile-time error with "-Xfatal-warnings"
compiler option)
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Scala pattern match is not exhaustive on nested case classes
A workaround appears to be to chose different names for the field of RequestError and ProcessingError . Here's a slightly minimized version: sealed...
Read more >Scala pattern matching keep saying "match is not ...
Finally, underscores are a Bad Thing(tm) in Scala variable or class names. All UPPERCASE names are not idiomatic either. So: sealed trait Message...
Read more >4. Pattern Matching - Programming Scala, 2nd Edition [Book]
So, it warns that the match isn't exhaustive. Then we see what happens when we try to match on a value for which...
Read more >Pattern match not-exhaustive, no warning - Question
At runtime I get an error, because the local function, loop, is called with (Nil,4,None), and there is no case for (Nil,None). However,...
Read more >Lower Your Guards
A compiler should warn if a function defined by pattern matching does not ... its pattern-match coverage warnings, there are still several cases...
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
final
is also not required to see the warning in Scala 2. All that is required is that it walk like a duck.@liufengyun Here is the same code in Scala 2.13.5. As you can see, the non-exhaustive warning is triggered. ~~https://scastie.scala-lang.org/AXKSl4K5S26cPjTE8b1kdw~~ https://scastie.scala-lang.org/KoU8DeZvTuKMlTfHFmHYeg (updated with non-final case class)