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
Weak Least Upper Bound is not Correctly Calculated in Some Conditional Expressions #9338
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9338?orig=1 |
I would agree it's a bug, and it's still present in 2.13.10. I'm going to mark it as "fixed in Scala 3" not because the bug was addressed directly, even though the behavior is the same: Welcome to Scala 3.3.1-RC1-bin-20230221-847eccc-NIGHTLY-git-847eccc (17.0.6, Java OpenJDK 64-Bit Server VM).
scala> val x: Any = if(true) 1 else 1.0
val x: Any = 1 because in Scala 3 it's behaving as spec'ed, as per https://docs.scala-lang.org/scala3/reference/dropped-features/weak-conformance.html :
and when the expected type is |
Regardless, I'm going to close this ticket as "not planned", since I think that the benefit of fixing it would be exceeded, at this late stage in Scala 2's evolution, by the cost of introducing a Scala 2 vs 3 behavior difference that doesn't currently exist. |
It's not a bug because the spec says each expr must conform to the expected type of the conditional. That is why the following works. Otherwise the type of the conditional is The ticket example is more special because
A similar example:
If the RHS were typed as I think the same principle applies in Scala 3, which still has numeric widening. I don't know how they describe the type of a conditional without recourse to weak LUB; "harmonization" applies only to literals. Edit, union for the inferred case, harmonized for the harmonized literals:
|
It does say that, but But then the spec also says:
I don't see how you've made an argument that this wording somehow doesn't apply here? |
If we're reading the spec differently, maybe the bug is that the spec ought to be clearer. |
The code says the spec should say "The inferred type of the conditional...". So a branch body might be typed with the explicit expected type, or subsequently adapted to the inferred type. |
According to SLS 6.15:
However, it seems that the result of following code (conditional expression is included) is not correct:
because weak least upper bounds of Int (type of 1) and Double (type of 1.0) is Double, 1 should be converted 1.0 regardless of the expected type.
The text was updated successfully, but these errors were encountered: