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 with block lifting in pipelines

+117 -5
+9 -4
compiler-core/src/javascript/expression.rs
··· 654 654 655 655 // Otherwise we assign the intermediate pipe value to a variable. 656 656 _ => { 657 - documents.push(self.not_in_tail_position(Some(Ordering::Strict), |this| { 658 - this.simple_variable_assignment(&assignment.name, &assignment.value) 659 - })?); 657 + let assignment_document = self 658 + .not_in_tail_position(Some(Ordering::Strict), |this| { 659 + this.simple_variable_assignment(&assignment.name, &assignment.value) 660 + })?; 661 + documents.push(self.add_statement_level(assignment_document)); 660 662 latest_local_var = Some(self.local_var(&assignment.name)); 661 663 } 662 664 } ··· 673 675 let var = latest_local_var.expect("echo with no previous step in a pipe"); 674 676 documents.push(self.echo(var.to_doc(), location)?); 675 677 } 676 - _ => documents.push(self.expression(finally)?), 678 + _ => { 679 + let finally = self.expression(finally)?; 680 + documents.push(self.add_statement_level(finally)) 681 + } 677 682 } 678 683 679 684 Ok(documents.to_doc().force_break())
+39
compiler-core/src/javascript/tests/functions.rs
··· 502 502 " 503 503 ); 504 504 } 505 + 506 + // https://github.com/gleam-lang/gleam/issues/4472 507 + #[test] 508 + fn pipe_into_block() { 509 + assert_js!( 510 + " 511 + fn side_effects(x) { x } 512 + 513 + pub fn main() { 514 + 1 515 + |> side_effects 516 + |> { 517 + side_effects(2) 518 + side_effects 519 + } 520 + } 521 + " 522 + ); 523 + } 524 + 525 + // https://github.com/gleam-lang/gleam/issues/4472 526 + #[test] 527 + fn pipe_with_block_in_the_middle() { 528 + assert_js!( 529 + " 530 + fn side_effects(x) { x } 531 + 532 + pub fn main() { 533 + 1 534 + |> side_effects 535 + |> { 536 + side_effects(2) 537 + side_effects 538 + } 539 + |> side_effects 540 + } 541 + " 542 + ); 543 + }
+33
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__functions__pipe_into_block.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/functions.rs 3 + expression: "\nfn side_effects(x) { x }\n\npub fn main() {\n 1\n |> side_effects\n |> {\n side_effects(2)\n side_effects\n }\n}\n" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + fn side_effects(x) { x } 8 + 9 + pub fn main() { 10 + 1 11 + |> side_effects 12 + |> { 13 + side_effects(2) 14 + side_effects 15 + } 16 + } 17 + 18 + 19 + ----- COMPILED JAVASCRIPT 20 + function side_effects(x) { 21 + return x; 22 + } 23 + 24 + export function main() { 25 + let _pipe = 1; 26 + let _pipe$1 = side_effects(_pipe); 27 + let _block; 28 + { 29 + side_effects(2); 30 + _block = side_effects; 31 + } 32 + return _block(_pipe$1); 33 + }
+35
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__functions__pipe_with_block_in_the_middle.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/functions.rs 3 + expression: "\nfn side_effects(x) { x }\n\npub fn main() {\n 1\n |> side_effects\n |> {\n side_effects(2)\n side_effects\n }\n |> side_effects\n}\n" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + fn side_effects(x) { x } 8 + 9 + pub fn main() { 10 + 1 11 + |> side_effects 12 + |> { 13 + side_effects(2) 14 + side_effects 15 + } 16 + |> side_effects 17 + } 18 + 19 + 20 + ----- COMPILED JAVASCRIPT 21 + function side_effects(x) { 22 + return x; 23 + } 24 + 25 + export function main() { 26 + let _pipe = 1; 27 + let _pipe$1 = side_effects(_pipe); 28 + let _block; 29 + { 30 + side_effects(2); 31 + _block = side_effects; 32 + } 33 + let _pipe$2 = _block(_pipe$1); 34 + return side_effects(_pipe$2); 35 + }
+1 -1
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__panic__pipe.snap
··· 13 13 import { makeError } from "../gleam.mjs"; 14 14 15 15 function go(f) { 16 + let _pipe = f; 16 17 let _block; 17 18 throw makeError( 18 19 "panic", ··· 22 23 "`panic` expression evaluated.", 23 24 {} 24 25 ) 25 - let _pipe = f; 26 26 return _block(_pipe); 27 27 }