Skip to content

Commit 58adea1

Browse files
Add more tests
1 parent e853bd3 commit 58adea1

8 files changed

+229
-0
lines changed

compiler-core/src/erlang/tests/inlining.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,40 @@ fn do_side_effects() {
204204
"#
205205
);
206206
}
207+
208+
#[test]
209+
fn inline_anonymous_function_call() {
210+
assert_erl!(
211+
"
212+
pub fn main() {
213+
fn(a, b) { #(a, b) }(42, False)
214+
}
215+
"
216+
);
217+
}
218+
219+
#[test]
220+
fn inline_anonymous_function_in_pipe() {
221+
assert_erl!(
222+
"
223+
pub fn main() {
224+
1 |> fn(x) { x + 1 } |> fn(y) { y * y }
225+
}
226+
"
227+
);
228+
}
229+
230+
#[test]
231+
fn inline_function_capture_in_pipe() {
232+
// The function capture is desugared to an anonymous function, so it should
233+
// be turned into a direct call to `add`
234+
assert_erl!(
235+
"
236+
pub fn main() {
237+
1 |> add(4, _)
238+
}
239+
240+
fn add(a, b) { a + b }
241+
"
242+
);
243+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
source: compiler-core/src/erlang/tests/inlining.rs
3+
expression: "\npub fn main() {\n fn(a, b) { #(a, b) }(42, False)\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
fn(a, b) { #(a, b) }(42, False)
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() -> {integer(), boolean()}.
20+
main() ->
21+
begin
22+
A = 42,
23+
B = false,
24+
{A, B}
25+
end.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
source: compiler-core/src/erlang/tests/inlining.rs
3+
expression: "\npub fn main() {\n 1 |> fn(x) { x + 1 } |> fn(y) { y * y }\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
1 |> fn(x) { x + 1 } |> fn(y) { y * y }
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() -> integer().
20+
main() ->
21+
_pipe = 1,
22+
_pipe@1 = begin
23+
X = _pipe,
24+
X + 1
25+
end,
26+
begin
27+
Y = _pipe@1,
28+
Y * Y
29+
end.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
source: compiler-core/src/erlang/tests/inlining.rs
3+
expression: "\npub fn main() {\n 1 |> add(4, _)\n}\n\nfn add(a, b) { a + b }\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
1 |> add(4, _)
9+
}
10+
11+
fn add(a, b) { a + b }
12+
13+
14+
----- COMPILED ERLANG
15+
-module(my@mod).
16+
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
17+
-define(FILEPATH, "project/test/my/mod.gleam").
18+
-export([main/0]).
19+
20+
-file("project/test/my/mod.gleam", 6).
21+
-spec add(integer(), integer()) -> integer().
22+
add(A, B) ->
23+
A + B.
24+
25+
-file("project/test/my/mod.gleam", 2).
26+
-spec main() -> integer().
27+
main() ->
28+
_pipe = 1,
29+
begin
30+
_capture = _pipe,
31+
add(4, _capture)
32+
end.

compiler-core/src/javascript/tests/inlining.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,40 @@ fn do_side_effects() {
204204
"#
205205
);
206206
}
207+
208+
#[test]
209+
fn inline_anonymous_function_call() {
210+
assert_js!(
211+
"
212+
pub fn main() {
213+
fn(a, b) { #(a, b) }(42, False)
214+
}
215+
"
216+
);
217+
}
218+
219+
#[test]
220+
fn inline_anonymous_function_in_pipe() {
221+
assert_js!(
222+
"
223+
pub fn main() {
224+
1 |> fn(x) { x + 1 } |> fn(y) { y * y }
225+
}
226+
"
227+
);
228+
}
229+
230+
#[test]
231+
fn inline_function_capture_in_pipe() {
232+
// The function capture is desugared to an anonymous function, so it should
233+
// be turned into a direct call to `add`
234+
assert_js!(
235+
"
236+
pub fn main() {
237+
1 |> add(4, _)
238+
}
239+
240+
fn add(a, b) { a + b }
241+
"
242+
);
243+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
source: compiler-core/src/javascript/tests/inlining.rs
3+
expression: "\npub fn main() {\n fn(a, b) { #(a, b) }(42, False)\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
fn(a, b) { #(a, b) }(42, False)
9+
}
10+
11+
12+
----- COMPILED JAVASCRIPT
13+
export function main() {
14+
{
15+
let a = 42;
16+
let b = false;
17+
return [a, b];
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
source: compiler-core/src/javascript/tests/inlining.rs
3+
expression: "\npub fn main() {\n 1 |> fn(x) { x + 1 } |> fn(y) { y * y }\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
1 |> fn(x) { x + 1 } |> fn(y) { y * y }
9+
}
10+
11+
12+
----- COMPILED JAVASCRIPT
13+
export function main() {
14+
let _pipe = 1;
15+
let _block;
16+
{
17+
let x = _pipe;
18+
_block = x + 1;
19+
}
20+
let _pipe$1 = _block;
21+
{
22+
let y = _pipe$1;
23+
return y * y;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
source: compiler-core/src/javascript/tests/inlining.rs
3+
expression: "\npub fn main() {\n 1 |> add(4, _)\n}\n\nfn add(a, b) { a + b }\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
1 |> add(4, _)
9+
}
10+
11+
fn add(a, b) { a + b }
12+
13+
14+
----- COMPILED JAVASCRIPT
15+
function add(a, b) {
16+
return a + b;
17+
}
18+
19+
export function main() {
20+
let _pipe = 1;
21+
{
22+
let _capture = _pipe;
23+
return add(4, _capture);
24+
}
25+
}

0 commit comments

Comments
 (0)