-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Returning a non-tuple into a val pattern match compiles #14472
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
looks like typer is inserting an [[syntax trees at end of typer]]
val $1$: (Int, Int) =
(Tuple2.apply[Int, Int](1, 2) match
{
case _ =>
1
case null =>
Tuple2.apply[Int, Int](1, 2)
}
):((1 : Int) | (Int, Int)) @unchecked match
{
case Tuple2.unapply[Int, Int](n @ _, m @ _):(Int, Int) =>
Tuple2.apply[Int, Int](n, m)
}
val n: Int = $1$._1
val m: Int = $1$._2 |
Note that it's reproducible without a guard, without scala> val (n, m) = (0, 2) match {
| case (0, _) => 1
| case _ => (1, 2)
| }
scala.MatchError: 1 (of class java.lang.Integer) and it doesn't matter which prong has the scala> val (n, m) = (0, 2) match {
| case (1, _) => (1, 2)
| case _ => 1
| }
scala.MatchError: 1 (of class java.lang.Integer) |
The |
This is not a new issue actually https://www.lihaoyi.com/post/WartsoftheScalaProgrammingLanguage.html#conflating-total-destructuring-with-partial-pattern-matching |
I need to check, but it might be this is all implemented, it's just under the "-strict" flag: #6389 |
It is under the -source future flag:
|
It's under |
I thought |
It is more than a lint (you are required to insert the |
Compiler version
scala 3.1.1
Minimized code
This code compiles, and fails at runtime with a MatchError:
Output
Fails with
scala.MatchError: 1 (of class java.lang.Integer)
Expectation
The code should not compile; the return
1
cannot be destructured into aTuple2
For comparison, the following code does not compile:
The text was updated successfully, but these errors were encountered: