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

Configure Feed

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

Inline functions through blocks

+192 -2
+1 -1
compiler-core/src/erlang/snapshots/gleam_core__erlang__tests__integration_test3.snap
··· 18 18 -file("project/test/my/mod.gleam", 2). 19 19 -spec y() -> point(). 20 20 y() -> 21 - (fun(Field@0, Field@1) -> {point, Field@0, Field@1} end)(4, 6). 21 + {point, 4, 6}.
+26
compiler-core/src/erlang/tests/inlining.rs
··· 241 241 " 242 242 ); 243 243 } 244 + 245 + #[test] 246 + fn inlining_works_through_blocks() { 247 + assert_erl!( 248 + " 249 + pub fn main() { 250 + { fn(x) { Ok(x + 1) } }(41) 251 + } 252 + " 253 + ); 254 + } 255 + 256 + #[test] 257 + fn blocks_get_preserved_when_needed() { 258 + assert_erl!( 259 + " 260 + pub fn main() { 261 + { 4 |> make_adder }(6) 262 + } 263 + 264 + fn make_adder(a) { 265 + fn(b) { a + b } 266 + } 267 + " 268 + ); 269 + }
+33
compiler-core/src/erlang/tests/snapshots/gleam_core__erlang__tests__inlining__blocks_get_preserved_when_needed.snap
··· 1 + --- 2 + source: compiler-core/src/erlang/tests/inlining.rs 3 + expression: "\npub fn main() {\n { 4 |> make_adder }(6)\n}\n\nfn make_adder(a) {\n fn(b) { a + b }\n}\n" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + pub fn main() { 8 + { 4 |> make_adder }(6) 9 + } 10 + 11 + fn make_adder(a) { 12 + fn(b) { a + b } 13 + } 14 + 15 + 16 + ----- COMPILED ERLANG 17 + -module(my@mod). 18 + -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). 19 + -define(FILEPATH, "project/test/my/mod.gleam"). 20 + -export([main/0]). 21 + 22 + -file("project/test/my/mod.gleam", 6). 23 + -spec make_adder(integer()) -> fun((integer()) -> integer()). 24 + make_adder(A) -> 25 + fun(B) -> A + B end. 26 + 27 + -file("project/test/my/mod.gleam", 2). 28 + -spec main() -> integer(). 29 + main() -> 30 + begin 31 + _pipe = 4, 32 + make_adder(_pipe) 33 + end(6).
+24
compiler-core/src/erlang/tests/snapshots/gleam_core__erlang__tests__inlining__inlining_works_through_blocks.snap
··· 1 + --- 2 + source: compiler-core/src/erlang/tests/inlining.rs 3 + expression: "\npub fn main() {\n { fn(x) { Ok(x + 1) } }(41)\n}\n" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + pub fn main() { 8 + { fn(x) { Ok(x + 1) } }(41) 9 + } 10 + 11 + 12 + ----- COMPILED ERLANG 13 + -module(my@mod). 14 + -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). 15 + -define(FILEPATH, "project/test/my/mod.gleam"). 16 + -export([main/0]). 17 + 18 + -file("project/test/my/mod.gleam", 2). 19 + -spec main() -> {ok, integer()} | {error, any()}. 20 + main() -> 21 + begin 22 + X = 41, 23 + {ok, X + 1} 24 + end.
+32
compiler-core/src/inline.rs
··· 577 577 // argument, allowing further inlining. 578 578 let function = self.expression(*function); 579 579 580 + // If the left-hand side is in a block for some reason, for example 581 + // `{ fn(x) { x + 1 } }(10)`, we still want to be able to inline it. 582 + let function = expand_block(function); 583 + 580 584 let function = match function { 581 585 TypedExpr::Var { 582 586 ref constructor, ··· 903 907 clauses, 904 908 compiled_case, 905 909 } 910 + } 911 + } 912 + 913 + /// Removes any blocks which are acting as brackets (they hold a single expression) 914 + fn expand_block(expression: TypedExpr) -> TypedExpr { 915 + match expression { 916 + TypedExpr::Block { 917 + location, 918 + statements, 919 + } if statements.len() == 1 => { 920 + let first = statements 921 + .into_iter() 922 + .next() 923 + .expect("Vec1 always has a first element"); 924 + 925 + match first { 926 + // If this is several blocks inside each other, we want to 927 + // expand them all. 928 + Statement::Expression(inner) => expand_block(inner), 929 + Statement::Assignment(_) | Statement::Use(_) | Statement::Assert(_) => { 930 + TypedExpr::Block { 931 + location, 932 + statements: Vec1::new(first), 933 + } 934 + } 935 + } 936 + } 937 + _ => expression, 906 938 } 907 939 } 908 940
+26
compiler-core/src/javascript/tests/inlining.rs
··· 241 241 " 242 242 ); 243 243 } 244 + 245 + #[test] 246 + fn inlining_works_through_blocks() { 247 + assert_js!( 248 + " 249 + pub fn main() { 250 + { fn(x) { Ok(x + 1) } }(41) 251 + } 252 + " 253 + ); 254 + } 255 + 256 + #[test] 257 + fn blocks_get_preserved_when_needed() { 258 + assert_js!( 259 + " 260 + pub fn main() { 261 + { 4 |> make_adder }(6) 262 + } 263 + 264 + fn make_adder(a) { 265 + fn(b) { a + b } 266 + } 267 + " 268 + ); 269 + }
+4 -1
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__functions__function_literals_get_properly_wrapped_2.snap
··· 10 10 11 11 ----- COMPILED JAVASCRIPT 12 12 export function main() { 13 - return ((n) => { return n + 1; })(10); 13 + { 14 + let n = 10; 15 + return n + 1; 16 + } 14 17 }
+26
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__inlining__blocks_get_preserved_when_needed.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/inlining.rs 3 + expression: "\npub fn main() {\n { 4 |> make_adder }(6)\n}\n\nfn make_adder(a) {\n fn(b) { a + b }\n}\n" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + pub fn main() { 8 + { 4 |> make_adder }(6) 9 + } 10 + 11 + fn make_adder(a) { 12 + fn(b) { a + b } 13 + } 14 + 15 + 16 + ----- COMPILED JAVASCRIPT 17 + function make_adder(a) { 18 + return (b) => { return a + b; }; 19 + } 20 + 21 + export function main() { 22 + let _block; 23 + let _pipe = 4; 24 + _block = make_adder(_pipe); 25 + return _block(6); 26 + }
+20
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__inlining__inlining_works_through_blocks.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/inlining.rs 3 + expression: "\npub fn main() {\n { fn(x) { Ok(x + 1) } }(41)\n}\n" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + pub fn main() { 8 + { fn(x) { Ok(x + 1) } }(41) 9 + } 10 + 11 + 12 + ----- COMPILED JAVASCRIPT 13 + import { Ok } from "../gleam.mjs"; 14 + 15 + export function main() { 16 + { 17 + let x = 41; 18 + return new Ok(x + 1); 19 + } 20 + }