-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Pattern matching on sealed abstract classes causes spurious warning #3145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can be reproduced with sealed trait Foo
class Bar extends Foo
sealed trait Baz extends Foo
class Test {
def f(x: Foo) = x match {
case bar: Bar => 1
case baz: Baz => 2
}
}
|
This seems to be the intended behaviour, because |
What about this example: sealed trait Foo
class Bar extends Foo
sealed trait Baz extends Foo
class Test {
def f = {
val x = new Baz {}
x match {
case bar: Bar => 1
case baz: Baz => 2
}
}
} scalac emits no warning here. |
The last example above is due to the problem of anonymous class extending sealed class/trait. @smarter suggested to simply disallow anonymous class extending sealed class/trait long time ago, see below: Currently, the exhaustivity check doesn't have the information to know whether a sealed trait has anonymous children or not. |
Sealed classes and traits are effectively inhabited in practice. By treating them as inhabited by default, we avoid spurious warnings that ignore anonymous classes. When we have a mechanism to track anonymous children or disallow anonymous children for sealed classes, we could revert this change to make the check stronger.
In some situations, matching on a sealed abstract class using a type annotation will cause the compiler to warn about unreachable code
This warning does not happen if the case for the sealed abstract class comes first.
The text was updated successfully, but these errors were encountered: