-
-
Notifications
You must be signed in to change notification settings - Fork 910
Emit correctly-scoped JavaScript code for directly-matching case subjects #5271
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
base: main
Are you sure you want to change the base?
Conversation
lpil
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lovely, thank you! Would you mind also adding a JavaScript codegen test please 🙏
|
Okay, I think I did it! I used the same test I added to |
|
Hello! I tried to rebase this on main but couldn't work out how to do it as your branch is also called |
|
Hi! I rebased onto |
| pub fn main() { | ||
| gleeunit.main() | ||
| } | ||
| } No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add the newline back?
|
Looks good to me just some minor nitpick!
If I recall correctly recently we switched to using |
This PR fixes #5265. I added the code snippet from that issue as a test to
language_test.gleam(with the dependence on the standard library removed for convenience). Now the JavaScript and Erlang compilation targets have the same behavior and pass this test. (Before this PR, the new test would fail under JavaScript only.)It appears as if the root issue was that the body of the directly-matching branch was inserted directly into the scope of the
caseexpression. I observed that commenting out lines 346–354 ofdecision.rsalmost fixed the issue, but the resulting branch was emitted as part of a danglingelse. (The generated code looked like<correct code> else { <correct code> } <correct code>with noif.) The main difference I saw between how theelsebranch is handled and how the lines above are handled is that the latter does not contain a call toself.inside_new_scope(). I figured that might be the missing piece, so I gave it a shot and it seems to work!This is my first time looking at the internals of the Gleam compiler. Someone more experienced may want to check to make sure that no other code from the other paths in the
switchfunction need to be ported over to thechoices.is_empty()path.Technically, this change affects the behavior of Gleam programs when compiled to JavaScript, and so it might therefore be considered backwards-incompatible.