Skip to content

Conversation

@justinlubin
Copy link

@justinlubin justinlubin commented Jan 7, 2026

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 case expression. I observed that commenting out lines 346–354 of decision.rs almost fixed the issue, but the resulting branch was emitted as part of a dangling else. (The generated code looked like <correct code> else { <correct code> } <correct code> with no if.) The main difference I saw between how the else branch is handled and how the lines above are handled is that the latter does not contain a call to self.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 switch function need to be ported over to the choices.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.

  • The changes in this PR have been discussed beforehand in an issue
  • The issue for this PR has been linked
  • Tests have been added for new behaviour
  • The changelog has been updated for any user-facing changes

Copy link
Member

@lpil lpil left a 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 🙏

@justinlubin
Copy link
Author

Okay, I think I did it! I used the same test I added to language_test.gleam as a snapshot test in compiler-core/src/javascript/tests/case.rs.

@lpil
Copy link
Member

lpil commented Feb 7, 2026

Hello! I tried to rebase this on main but couldn't work out how to do it as your branch is also called main 😅 Could you rebase on main please

@justinlubin
Copy link
Author

Hi! I rebased onto gleam-lang:main. I see that the language tests are now split from one file into many (nice!), so I created a new file for the language test I introduce. I also had to update gleam.toml for the language tests. It had a section called dev_dependencies (with underscore) but I think it needs to be dev-dependencies (with dash); at least, that's what I had to do on my machine to get Gleam to install gleeunit.

pub fn main() {
gleeunit.main()
}
} No newline at end of file
Copy link
Member

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?

@giacomocavalieri
Copy link
Member

Looks good to me just some minor nitpick!

It had a section called dev_dependencies (with underscore) but I think it needs to be dev-dependencies (with dash); at least, that's what I had to do on my machine to get Gleam to install gleeunit.

If I recall correctly recently we switched to using dev_dependencies and dev-dependencies is considered deprecated, so it would be best to use that. I think the problem might be because the test is being run with the locally installed version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Case expression inlining leaks shadowed names to enclosing scope in JavaScript

3 participants