-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Matching strings should emit switches (as in Scala 2) #11923
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
I'm going to take a stab at this, but I'm opening an issue anyways in case I run out of steam (since this really should be addressed). |
@sjrd should we keep those matched as |
We should do as Scala 2 does, i.e., keep those as |
I started with ef06795 but never got to refining it |
The pattern matcher will now emit `Match` with `String` scrutinee as well as the existing `Int` scrutinee. The JVM backend handles this case by emitting bytecode that switches on the String's `hashCode` (this matches what Java does). The SJS already handles `String` matches. The approach is similar to scala/scala#8451 (see scala/bug#11740 too), except that instead of doing a transformation on the AST, we just emit the right bytecode straight away. This is desirable since it means that Scala.js (and any other backend) can choose their own optimised strategy for compiling a match on strings. Fixes scala#11923
The pattern matcher will now emit `Match` with `String` scrutinee as well as the existing `Int` scrutinee. The JVM backend handles this case by emitting bytecode that switches on the String's `hashCode` (this matches what Java does). The SJS already handles `String` matches. The approach is similar to scala/scala#8451 (see scala/bug#11740 too), except that instead of doing a transformation on the AST, we just emit the right bytecode straight away. This is desirable since it means that Scala.js (and any other backend) can choose their own optimised strategy for compiling a match on strings. Fixes scala#11923
This was initially reported in scala/bug#11740, then fixed in scala/scala#8451, but never ported to Dotty (perhaps because the changes collide a little without the new pattern matching).
I would expect the following to compile into a
lookupswitch
based on thehashCode
of the cases, as it does in Scala 2 (and as Java, Kotlin, etc. also do).To further confuse the story, this comment from the JS codgen appears to suggest that at some point the Scala.js backend would've received and emitted a
switch
over strings (although I can't reproduce that behavior, even on Scala 2 and with@switch
supposedly claiming a switch will be emitted).The text was updated successfully, but these errors were encountered: