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

Configure Feed

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

fix a bug with JS codegen of pattern matching expression using string literals

and segments with a statically unknown size

author
Giacomo Cavalieri
committer
Louis Pilfold
date (Sep 19, 2025, 10:21 PM +0100) commit efe51ee8 parent 62239380 change-id yuuwmwxu
+56 -4
+4
CHANGELOG.md
··· 748 748 - Fixed a bug where running `gleam update` would not properly update git 749 749 dependencies unless `gleam clean` was run first. 750 750 ([Surya Rose](https://github.com/GearsDatapacks)) 751 + 752 + - Fixed a bug where the compiler would produce wrong JavaScript code for binary 753 + pattern matching expressions using literal strings and byte segments. 754 + ([Giacomo Cavalieri](https://github.com/giacomocavalieri))
+8 -4
compiler-core/src/javascript/decision.rs
··· 1443 1443 from_byte += 1; 1444 1444 } 1445 1445 } else { 1446 + let mut start = start.clone(); 1447 + 1446 1448 // If the string doesn't start at a byte aligned offset then we'll 1447 1449 // have to take slices out of it to check that each byte matches. 1448 1450 for byte in bytes { 1449 - let end = self.offset_to_doc(&start.add_constant(8), false); 1450 - let from = self.offset_to_doc(start, false); 1451 - let byte_access = 1452 - self.bit_array_slice_to_int(&bit_array, from, end, endianness, *signed); 1451 + let start_doc = self.offset_to_doc(&start, false); 1452 + let end = start.add_constant(8); 1453 + let end_doc = self.offset_to_doc(&end, false); 1454 + let byte_access = self 1455 + .bit_array_slice_to_int(&bit_array, start_doc, end_doc, endianness, *signed); 1453 1456 checks.push(docvec![byte_access, equality, byte]); 1457 + start = end; 1454 1458 } 1455 1459 } 1456 1460
+14
compiler-core/src/javascript/tests/bit_arrays.rs
··· 2631 2631 " 2632 2632 ); 2633 2633 } 2634 + 2635 + #[test] 2636 + fn pattern_match_unknown_size_and_literal_string() { 2637 + assert_js!( 2638 + r#" 2639 + pub fn go(x, n) { 2640 + case x { 2641 + <<_:size(n), "\r\n">> -> 1 2642 + _ -> 2 2643 + } 2644 + } 2645 + "# 2646 + ); 2647 + }
+30
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__bit_arrays__pattern_match_unknown_size_and_literal_string.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/bit_arrays.rs 3 + expression: "\npub fn go(x, n) {\n case x {\n <<_:size(n), \"\\r\\n\">> -> 1\n _ -> 2\n }\n}\n" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + pub fn go(x, n) { 8 + case x { 9 + <<_:size(n), "\r\n">> -> 1 10 + _ -> 2 11 + } 12 + } 13 + 14 + 15 + ----- COMPILED JAVASCRIPT 16 + import { bitArraySliceToInt } from "../gleam.mjs"; 17 + 18 + export function go(x, n) { 19 + if ( 20 + n >= 0 && 21 + x.bitSize >= n && 22 + x.bitSize === 16 + n && 23 + bitArraySliceToInt(x, n, 8 + n, true, false) === 13 && 24 + bitArraySliceToInt(x, 8 + n, 16 + n, true, false) === 10 25 + ) { 26 + return 1; 27 + } else { 28 + return 2; 29 + } 30 + }