Skip to content
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

Fix: Labels are no longer compiled in the case of variable declarations #2973

Merged
merged 4 commits into from
Nov 2, 2022

Conversation

MikaelMayer
Copy link
Member

@MikaelMayer MikaelMayer commented Nov 2, 2022

This PR fixes #2608 and fixes #2576
I added the corresponding test.

Here is the story. Whenever there is a label L1 to a statement S, the JavaScript compiler wraps the statement with curly braces {} and put the label in front of it, like this:

L1 {
  [[S]] // Translate S to JavaScript
}

The problem was that, if there are labelled variable declarations, they are compiled to let and not available after }.
Looking carefully at it, we don't have goto in Dafny so labels are used only to break out of loops. But these are no loops, only variable declarations. Hence, I claim it make sense to just remove the labels when compiling variable declarations.
The test now passes.

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@MikaelMayer MikaelMayer self-assigned this Nov 2, 2022
@@ -4331,7 +4331,9 @@ private class GetterSetterLvalueImpl : ILvalue {
// <prelude> // filled via copyInstrWriters -- copies out-parameters used in letexpr to local variables
// ss // translation of ss has side effect of filling the top copyInstrWriters
var w = writer;
if (ss.Labels != null) {
if (ss.Labels != null && !(ss is VarDeclPattern or VarDeclStmt)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely sounds like a good change, but how confident are we that no other statement cases would similarly be broken by being wrapped in a block?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only problem of wrapping with block quotes (at least in javascript), is that it creates a "local scope". Other than that, you can insert {} around any statement without changing its meaning.
Hence, in our case, the only place where this matter is that we are introducing new variables for subsequent statements. All other statements are only modifying existing vars that were already in scope, so they have no reason to be broken by being wrapped into a labelled block.

docs/dev/news/2608.fix Outdated Show resolved Hide resolved
@MikaelMayer MikaelMayer changed the title Fix: Labels should not be compiled in the case of variable declarations Fix: Labels are no longer compiled in the case of variable declarations Nov 2, 2022
Co-authored-by: Robin Salkeld <salkeldr@amazon.com>
@MikaelMayer MikaelMayer enabled auto-merge (squash) November 2, 2022 20:49
@MikaelMayer MikaelMayer merged commit 03799c9 into master Nov 2, 2022
@MikaelMayer MikaelMayer deleted the fix-2608-label-javascript branch November 2, 2022 22:16
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.

JavaScript compiler and labels issue Miscompilation (scope issue) of labels
2 participants