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
No compile time avoidance of mismatched outers #12879
Comments
Scala 2 is too conservative and prevents code that would work fine at runtime from compiling: class C {
class D
val d: D = new D
}
object Test extends App {
def foo(c1: C, c2: C): Unit = {
c1.d match {
case c2.d => // OK in Scala 3, type mismatch in Scala 2
}
}
val c = new C
foo(c, c)
} On the other hand, the pattern match: c1.d match {
case c2.d =>
} should emit an exhaustivity warning since there's no guarantee that the case matches the selector. @liufengyun how does the exhaustivity checker deals with prefixes currently? |
The check reason at the level of types, so prefixes are handled as part of the type system. In child instantiation, we do prefix inference for child classes (in If we add -- [E029] Pattern Match Exhaustivity Warning: examples/hello.scala:8:7 ---------
8 | c1.d match {
| ^^^^
| match may not be exhaustive.
|
| It would fail on pattern case: _: c1.D |
Ah thank you, I forgot about the need for sealed, we also get a warning with |
Surprised TIL D is not effectively sealed. Very surprised there is no warning at all. Willing to entertain which warning is appropriate. |
This is totally wrong. You have to warn at compile time, basta. There are lots of type checks that wouldn't happen to fail at runtime, but that doesn't mean you don't issue the type check at compile time. I shouldn't match
where this is why REPL needs
|
I mean, the whole point of pattern matching is that there might be information you can find at runtime that is unknown at compile-time, so the compiler should only prevent you from writing a pattern match if it's certain that it will in fact never match (implemented by https://github.com/lampepfl/dotty/blob/515cb9f01c609f59e2dd305107596066b0e1f580/compiler/src/dotty/tools/dotc/typer/Typer.scala#L3868-L3872). I do agree that we should have some kind of warning here by default, probably by enabling exhaustivity checking for unsealed selectors by default, but that requires some more discussion: scala/scala#9299 (comment) |
OK, I see I confused matching on any, which arbitrarily narrows to a subset of values, and an ADT, which narrows to those subtypes. I'm not normally confused on that score, but I was just looking at how uncheckable type args work, namely, when can you accept |
Compiler version
3 dawg
Minimized code
Output
Expectation
I kind of prefer the compile-time error to runtime.
The text was updated successfully, but these errors were encountered: