Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

add and update tests for guard variable pollution fix

+100 -4
+5
CHANGELOG.md
··· 158 158 - Fixed a bug where useless comparison warnings for floats compared literal 159 159 strings, claiming for example that `1.0 == 1.` was always false. 160 160 ([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 +
+27
compiler-core/src/javascript/tests/case_clause_guards.rs
··· 576 576 "#, 577 577 ); 578 578 } 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 + }
+2 -2
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__case__guard_variable_only_brought_into_scope_when_needed_1.snap
··· 20 20 if (i === 1) { 21 21 return true; 22 22 } else { 23 - let i$1 = $; 24 - if (i$1 < 2) { 23 + let i = $; 24 + if (i < 2) { 25 25 return true; 26 26 } else { 27 27 return false;
+2 -2
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__case__list_with_guard.snap
··· 24 24 if (first < 10) { 25 25 return first * 2; 26 26 } else { 27 - let first$1 = x.head; 28 - return first$1; 27 + let first = x.head; 28 + return first; 29 29 } 30 30 } 31 31 }
+64
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__case_clause_guards__guard_pattern_does_not_shadow_outer_scope.snap
··· 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 + }