-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Pattern matching with alternatives is compiled to "throw null" with Scala.js #9268
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
@sjrd what could cause the scalajs backend to emit "throw null" here? |
Ah I see :). Would you recommend a straight port of genMatch from the scala 2 plugin? According to https://github.com/scala-js/scala-js/blob/c0b8f67c91a0c94796c8b8df94119c60902199e9/compiler/src/main/scala/org/scalajs/nscplugin/GenJSCode.scala#L3434-L3487 there's some complexity related to handling guards, does that also apply to dotty? |
I do recommend a straight port of |
Great thanks, @devkat: feel free to give this a go :). |
case 'e' | 'E' | 'g' | 'G' | 'f' => 5.5
I didn't realize at first that the working code is a simple type test for a union type. Apparently this is straightforward enough to emit an case _: ('e' | 'E' | 'g' | 'G' | 'f') => 5.5
I'll try to find the time to look into |
Now the JS looks good at first glance, but strangely the match expression is never true and the result is always 5.0: const arg = ((($bC(arg1) === $bC(101)) || (($bC(arg1) === $bC(69)) || (($bC(arg1) === $bC(103)) || (($bC(arg1) === $bC(71)) || ($bC(arg1) === $bC(102)))))) ? 5.5 : 5.0); |
That code is trying to compare boxed chars with an identity comparison. That will always be false. In a switch, the scrutinee and the cases must all be converted to Also I encourage you to look at the IR as the point of reference. Not at the generated JS. |
The IR looks like this:
I'm not sure if the boxed chars comparison is in the scope of this ticket, but I'll take a closer look. |
|
Here's a first draft: This is basically a copy of the Some open issues:
|
Can you already submit a PR with what you have? It's easier to discuss the details via line comments in a PR. |
Done – thanks a lot in advance for reviewing! |
This implementation has been ported from the scalac backend, but is much simpler because it does not have to deal with hacks around scalac's LabelDefs.
Fix #9268: Scala.js: Add support for switch-Matches.
Uh oh!
There was an error while loading. Please reload this page.
Minimized code
https://github.com/scala-js/scala-js/blob/master/test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/util/FormatterTest.scala#L626
Output
Scala.js IR:
JavaScript:
Expectation
This (equivalent?) match expression:
is compiled to the following IR:
and the following JavaScript code:
The text was updated successfully, but these errors were encountered: