-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Unsoundness in GADT pattern matching (falsely assumed injectivity) #13080
Comments
Like in #11545, probably another pitfall of mixing type inference (for which it's fine to assume injectivity when there is nothing better to do) with GADT reasoning (for which it's unsound). Cc @abgruszecki |
You mentioned the soundness problem on the commented line, but I'm confused before that - how does this typecheck? val f: Fun[String * Int, String * Int] = Par(Id[String](), Id[Int]())
val g: Fun[String * Int, Long * Char] = f Is it because |
The wrong assumption is that: f: Fun[A1 * A2, B1 * B2]
f: Par[?A, ?B, ?C, ?D] <:< Fun[?A * ?B, ?C * ?D]
// implies (true, Fun is a trait => injective)
(?A * ?B) =:= (A1 * A2)
(?C * ?D) =:= (B1 * B2)
// implies (wrong, * is abstract => injectivity unknown)
?A =:= A1
?B =:= A2
?C =:= B1
?D =:= B2 |
Yup, both |
In Scala >= 3.0.2 we at least get a warning (although a confusing one): -- [E029] Pattern Match Exhaustivity Warning: try/i13080.scala:27:4 ------------
27 | f match {
| ^
|match may not be exhaustive.
|
|It would fail on pattern case: Functions[?].Id(), Functions[?].Par(_, _) |
Compiler version
3.0.1-RC2
Minimized code
Output
Expectation
Soundness problem is on the commented line in the source. Strictly speaking, it should not compile. However, I expect most type constructors to be injective and it is useful to be able to use that fact in pattern matching. So I would like this to be fixed (by rejecting the program at compile time) only after there is a way to tell the compiler that an abstract type constructor is injective.
The text was updated successfully, but these errors were encountered: