-
-
Notifications
You must be signed in to change notification settings - Fork 887
Fix guard variable scoping #5097
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
Merged
+109
−9
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...re__javascript__tests__case_clause_guards__guard_pattern_does_not_shadow_outer_scope.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| --- | ||
| source: compiler-core/src/javascript/tests/case_clause_guards.rs | ||
| expression: "\npub type Option(a) {\n Some(a)\n None\n}\n\npub type Container {\n Container(x: Option(Int))\n}\n\npub fn main() {\n let x: Option(Int) = Some(42)\n case Some(1) {\n Some(x) if x < 0 -> Container(None)\n _ -> {\n Container(x:)\n }\n }\n}\n" | ||
| --- | ||
| ----- SOURCE CODE | ||
|
|
||
| pub type Option(a) { | ||
| Some(a) | ||
| None | ||
| } | ||
|
|
||
| pub type Container { | ||
| Container(x: Option(Int)) | ||
| } | ||
|
|
||
| pub fn main() { | ||
| let x: Option(Int) = Some(42) | ||
| case Some(1) { | ||
| Some(x) if x < 0 -> Container(None) | ||
| _ -> { | ||
| Container(x:) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| ----- COMPILED JAVASCRIPT | ||
| import { CustomType as $CustomType } from "../gleam.mjs"; | ||
|
|
||
| export class Some extends $CustomType { | ||
| constructor($0) { | ||
| super(); | ||
| this[0] = $0; | ||
| } | ||
| } | ||
| export const Option$Some = ($0) => new Some($0); | ||
| export const Option$isSome = (value) => value instanceof Some; | ||
| export const Option$Some$0 = (value) => value[0]; | ||
|
|
||
| export class None extends $CustomType {} | ||
| export const Option$None = () => new None(); | ||
| export const Option$isNone = (value) => value instanceof None; | ||
|
|
||
| export class Container extends $CustomType { | ||
| constructor(x) { | ||
| super(); | ||
| this.x = x; | ||
| } | ||
| } | ||
| export const Container$Container = (x) => new Container(x); | ||
| export const Container$isContainer = (value) => value instanceof Container; | ||
| export const Container$Container$x = (value) => value.x; | ||
| export const Container$Container$0 = (value) => value.x; | ||
|
|
||
| export function main() { | ||
| let x = new Some(42); | ||
| let $ = new Some(1); | ||
| let x$1 = $[0]; | ||
| if (x$1 < 0) { | ||
| return new Container(new None()); | ||
| } else { | ||
| return new Container(x); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Isn't this an error in JavaScript? There is already a
firstdefined in this functionUh oh!
There was an error while loading. Please reload this page.
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.
No it's not an error in js since
letandconstare block-scoped.Shadowing doesn't pollute the outer scope - in contrary to
varwhich is bad practice exactly for that reason.I ran a quick test on how my change behaves for "double-guards" - eg. :
This previously compiled to:
with my change compiles to:
Again we see that
x$1is repeatedly defined (in different scopes). - Fine by js but maybe odd for humans to read. It could very well be though that what we declare asxin gleam are different types. Back-tracing declarations from upper scopes for the purpose of potential reuse doesn't make much sense and is probably quite error-prone though.Each guard produces a declaration as far as I can see. Since expressions within if-conditions aren't possible we're forced to declare in upper scope. if we shadow vars or 'soft-shadow' with counter increment doesn't matter much or?
Do you suggest putting more effort to avoid variable shadowing in js?
Do you see a way how the change could cause a bug?
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.
Oh yes! Thank you for your thorough explanation. Very silly of me there