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

Configure Feed

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

Fix bug when `let assert` is alone in a block

+76 -21
+4
CHANGELOG.md
··· 292 292 when it could use the cache from previous compilations. 293 293 ([Louis Pilfold](https://github.com/lpil)) 294 294 295 + - Fixed a bug where `let assert` would not assert that the given value matched 296 + the pattern if it was the only expression inside a block. 297 + ([Surya Rose](https://github.com/GearsDatapacks)) 298 + 295 299 ## v1.9.1 - 2025-03-10 296 300 297 301 ### Formatter
+26 -21
compiler-core/src/javascript/expression.rs
··· 692 692 fn block(&mut self, statements: &'a Vec1<TypedStatement>) -> Output<'a> { 693 693 if statements.len() == 1 { 694 694 match statements.first() { 695 - Statement::Expression(expression) => self.child_expression(expression), 695 + Statement::Expression(expression) => return self.child_expression(expression), 696 696 697 - Statement::Assignment(assignment) => { 698 - self.child_expression(assignment.value.as_ref()) 699 - } 697 + Statement::Assignment(assignment) => match &assignment.kind { 698 + AssignmentKind::Let | AssignmentKind::Generated => { 699 + return self.child_expression(assignment.value.as_ref()); 700 + } 701 + // We can't just return the right-hand side of a `let assert` 702 + // assignment; we still need to check that the pattern matches. 703 + AssignmentKind::Assert { .. } => {} 704 + }, 700 705 701 - Statement::Use(use_) => self.child_expression(&use_.call), 706 + Statement::Use(use_) => return self.child_expression(&use_.call), 702 707 } 703 - } else { 704 - match &self.scope_position { 705 - Position::Tail | Position::Assign(_) => self.block_document(statements), 706 - Position::NotTail(Ordering::Strict) => self 707 - .immediately_invoked_function_expression(statements, |this, statements| { 708 - this.statements(statements) 709 - }), 710 - Position::NotTail(Ordering::Loose) => self.wrap_block(|this| { 711 - // Save previous scope 712 - let current_scope_vars = this.current_scope_vars.clone(); 708 + } 709 + 710 + match &self.scope_position { 711 + Position::Tail | Position::Assign(_) => self.block_document(statements), 712 + Position::NotTail(Ordering::Strict) => self 713 + .immediately_invoked_function_expression(statements, |this, statements| { 714 + this.statements(statements) 715 + }), 716 + Position::NotTail(Ordering::Loose) => self.wrap_block(|this| { 717 + // Save previous scope 718 + let current_scope_vars = this.current_scope_vars.clone(); 713 719 714 - let document = this.block_document(statements)?; 720 + let document = this.block_document(statements)?; 715 721 716 - // Restore previous state 717 - this.current_scope_vars = current_scope_vars; 722 + // Restore previous state 723 + this.current_scope_vars = current_scope_vars; 718 724 719 - Ok(document) 720 - }), 721 - } 725 + Ok(document) 726 + }), 722 727 } 723 728 } 724 729
+14
compiler-core/src/javascript/tests/blocks.rs
··· 277 277 " 278 278 ) 279 279 } 280 + 281 + // https://github.com/gleam-lang/gleam/issues/4393 282 + #[test] 283 + fn let_assert_only_statement_in_block() { 284 + assert_js!( 285 + " 286 + pub fn main() { 287 + { 288 + let assert Ok(1) = Error(Nil) 289 + } 290 + } 291 + " 292 + ) 293 + }
+32
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__blocks__let_assert_only_statement_in_block.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/blocks.rs 3 + expression: "\npub fn main() {\n {\n let assert Ok(1) = Error(Nil)\n }\n}\n" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + pub fn main() { 8 + { 9 + let assert Ok(1) = Error(Nil) 10 + } 11 + } 12 + 13 + 14 + ----- COMPILED JAVASCRIPT 15 + import { Error, makeError } from "../gleam.mjs"; 16 + 17 + export function main() { 18 + { 19 + let $ = new Error(undefined); 20 + if (!$.isOk() || $[0] !== 1) { 21 + throw makeError( 22 + "let_assert", 23 + "my/mod", 24 + 4, 25 + "main", 26 + "Pattern match failed, no pattern matched the value.", 27 + { value: $ } 28 + ) 29 + } 30 + return $; 31 + } 32 + }