Skip to content

Commit 739ab05

Browse files
Fix wrongly scoped pipe variable
1 parent 8d1217d commit 739ab05

6 files changed

+59
-11
lines changed

compiler-core/src/erlang.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,8 @@ fn pipeline<'a>(
22112211
echo(name, location, env)
22122212
};
22132213

2214+
let vars = env.current_scope_vars.clone();
2215+
22142216
let mut prev_local_var_name = None;
22152217
for a in all_assignments {
22162218
match a.value.as_ref() {
@@ -2243,6 +2245,8 @@ fn pipeline<'a>(
22432245
_ => documents.push(expr(finally, env)),
22442246
}
22452247

2248+
env.current_scope_vars = vars;
2249+
22462250
documents.to_doc()
22472251
}
22482252

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn pipe_in_record_update() {
7979
fn id(x) {
8080
x
8181
}
82-
82+
8383
pub fn main(x) {
8484
X(..x, a: 1 |> id)
8585
}"
@@ -93,7 +93,7 @@ fn pipe_in_eq() {
9393
"fn id(x) {
9494
x
9595
}
96-
96+
9797
pub fn main() {
9898
1 == 1 |> id
9999
}"
@@ -135,3 +135,17 @@ pub fn two(a, b) {
135135
"#
136136
);
137137
}
138+
139+
#[test]
140+
fn multiple_pipes() {
141+
assert_erl!(
142+
"
143+
pub fn main() {
144+
1 |> x |> x
145+
2 |> x |> x
146+
}
147+
148+
fn x(x) { x }
149+
"
150+
);
151+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
source: compiler-core/src/erlang/tests/pipes.rs
3+
expression: "\npub fn main() {\n 1 |> x |> x\n 2 |> x |> x\n}\n\nfn x(x) { x }\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
1 |> x |> x
9+
2 |> x |> x
10+
}
11+
12+
fn x(x) { x }
13+
14+
15+
----- COMPILED ERLANG
16+
-module(my@mod).
17+
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
18+
-define(FILEPATH, "project/test/my/mod.gleam").
19+
-export([main/0]).
20+
21+
-file("project/test/my/mod.gleam", 7).
22+
-spec x(J) -> J.
23+
x(X) ->
24+
X.
25+
26+
-file("project/test/my/mod.gleam", 2).
27+
-spec main() -> integer().
28+
main() ->
29+
_pipe = 1,
30+
_pipe@1 = x(_pipe),
31+
x(_pipe@1),
32+
_pipe@2 = 2,
33+
_pipe@3 = x(_pipe@2),
34+
x(_pipe@3).

compiler-core/src/erlang/tests/snapshots/gleam_core__erlang__tests__pipes__pipe_in_call.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ main() ->
3737
_pipe@1 = 1,
3838
two(_pipe@1, 2)
3939
end,
40-
_pipe@1
40+
_pipe
4141
).

compiler-core/src/erlang/tests/snapshots/gleam_core__erlang__tests__pipes__pipe_in_eq.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
---
22
source: compiler-core/src/erlang/tests/pipes.rs
3-
assertion_line: 92
4-
expression: "fn id(x) {\n x\n}\n \npub fn main() {\n 1 == 1 |> id\n}"
5-
snapshot_kind: text
3+
expression: "fn id(x) {\n x\n}\n\npub fn main() {\n 1 == 1 |> id\n}"
64
---
75
----- SOURCE CODE
86
fn id(x) {
97
x
108
}
11-
9+
1210
pub fn main() {
1311
1 == 1 |> id
1412
}

compiler-core/src/erlang/tests/snapshots/gleam_core__erlang__tests__pipes__pipe_in_record_update.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22
source: compiler-core/src/erlang/tests/pipes.rs
3-
assertion_line: 74
4-
expression: "pub type X {\n X(a: Int, b: Int)\n}\n\nfn id(x) {\n x\n}\n \npub fn main(x) {\n X(..x, a: 1 |> id)\n}"
5-
snapshot_kind: text
3+
expression: "pub type X {\n X(a: Int, b: Int)\n}\n\nfn id(x) {\n x\n}\n\npub fn main(x) {\n X(..x, a: 1 |> id)\n}"
64
---
75
----- SOURCE CODE
86
pub type X {
@@ -12,7 +10,7 @@ pub type X {
1210
fn id(x) {
1311
x
1412
}
15-
13+
1614
pub fn main(x) {
1715
X(..x, a: 1 |> id)
1816
}

0 commit comments

Comments
 (0)