Skip to content

Commit 53ed4c7

Browse files
EliasDerHailpil
authored andcommitted
add and update tests for guard variable pollution fix
1 parent ec33281 commit 53ed4c7

5 files changed

+100
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,8 @@
158158
- Fixed a bug where useless comparison warnings for floats compared literal
159159
strings, claiming for example that `1.0 == 1.` was always false.
160160
([fruno](https://github.com/fruno-bulax/))
161+
162+
- Fixed a bug where pattern variables in case clause guards would incorrectly
163+
shadow outer scope variables in other branches when compiling to JavaScript.
164+
([Elias Haider](https://github.com/EliasDerHai))
165+

compiler-core/src/javascript/tests/case_clause_guards.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,30 @@ pub fn main() {
576576
"#,
577577
);
578578
}
579+
580+
// https://github.com/gleam-lang/gleam/issues/5094
581+
#[test]
582+
fn guard_pattern_does_not_shadow_outer_scope() {
583+
assert_js!(
584+
r#"
585+
pub type Option(a) {
586+
Some(a)
587+
None
588+
}
589+
590+
pub type Container {
591+
Container(x: Option(Int))
592+
}
593+
594+
pub fn main() {
595+
let x: Option(Int) = Some(42)
596+
case Some(1) {
597+
Some(x) if x < 0 -> Container(None)
598+
_ -> {
599+
Container(x:)
600+
}
601+
}
602+
}
603+
"#,
604+
);
605+
}

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__case__guard_variable_only_brought_into_scope_when_needed_1.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export function main() {
2020
if (i === 1) {
2121
return true;
2222
} else {
23-
let i$1 = $;
24-
if (i$1 < 2) {
23+
let i = $;
24+
if (i < 2) {
2525
return true;
2626
} else {
2727
return false;

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__case__list_with_guard.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export function go(x) {
2424
if (first < 10) {
2525
return first * 2;
2626
} else {
27-
let first$1 = x.head;
28-
return first$1;
27+
let first = x.head;
28+
return first;
2929
}
3030
}
3131
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
source: compiler-core/src/javascript/tests/case_clause_guards.rs
3+
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"
4+
---
5+
----- SOURCE CODE
6+
7+
pub type Option(a) {
8+
Some(a)
9+
None
10+
}
11+
12+
pub type Container {
13+
Container(x: Option(Int))
14+
}
15+
16+
pub fn main() {
17+
let x: Option(Int) = Some(42)
18+
case Some(1) {
19+
Some(x) if x < 0 -> Container(None)
20+
_ -> {
21+
Container(x:)
22+
}
23+
}
24+
}
25+
26+
27+
----- COMPILED JAVASCRIPT
28+
import { CustomType as $CustomType } from "../gleam.mjs";
29+
30+
export class Some extends $CustomType {
31+
constructor($0) {
32+
super();
33+
this[0] = $0;
34+
}
35+
}
36+
export const Option$Some = ($0) => new Some($0);
37+
export const Option$isSome = (value) => value instanceof Some;
38+
export const Option$Some$0 = (value) => value[0];
39+
40+
export class None extends $CustomType {}
41+
export const Option$None = () => new None();
42+
export const Option$isNone = (value) => value instanceof None;
43+
44+
export class Container extends $CustomType {
45+
constructor(x) {
46+
super();
47+
this.x = x;
48+
}
49+
}
50+
export const Container$Container = (x) => new Container(x);
51+
export const Container$isContainer = (value) => value instanceof Container;
52+
export const Container$Container$x = (value) => value.x;
53+
export const Container$Container$0 = (value) => value.x;
54+
55+
export function main() {
56+
let x = new Some(42);
57+
let $ = new Some(1);
58+
let x$1 = $[0];
59+
if (x$1 < 0) {
60+
return new Container(new None());
61+
} else {
62+
return new Container(x);
63+
}
64+
}

0 commit comments

Comments
 (0)