-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Missing unreachable warning/error in pattern matching case with Union Type #13277
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
That unreachable warning is actually emitted during Erasure, not pattern matching (not sure why). The reason that this doesn't emit any warnings from the pattern matcher is because, by design, transitively non-sealed types aren't check (and neither A nor B are sealed). So should that reachability warning also be suppressed? If you enable the
Should that also give a reachability warning? Should it only give a reachability warning? I'm not sure - I kind of feel that at least one warning is enough. |
@dwijnand even with enabled trait A
trait B
def foo(x: String): Unit =
x match {
case aOrB: A => // [error]: this case is unreachable since type String is not a subclass of trait A
case _: String =>
}
def foo2(x: String): Unit =
x match {
case aOrB: (A | B) => // no warning/error
case _: String =>
}
|
Yeah, I guess we can look to making |
Why can't the compiler do this check before the erasure? I was looking forward to scala compiler stopping me from abusing union types 😅 |
I think I saw this before (because Scala 2 does have this logic): Scala 3 isn't taking into consideration that String (the scrutinee type) is final and therefore it could never have a subclass that mixes in trait A. |
The cause here is that Erasure has a case OrType(tp1, tp2) =>
evalOnce(expr) { e =>
transformTypeTest(e, tp1, flagUnrelated = false)
.or(transformTypeTest(e, tp2, flagUnrelated = false))
} Sometimes only one part is unrelated, so it doesn't want to warn on those. Part of the problem is the code reuse: both transforming and warning. I couldn't find a straightforward way to delay the warning so I'm parking this. |
3.0.2-RC1
Minimized code
Output
It compiles without any error or warning.
Expectation
Compilation should fails as it does for the following:
The text was updated successfully, but these errors were encountered: