Skip to content

Commit bdcdb3c

Browse files
authored
fix(compiler): Fix compilation of closure scope mutable destructuring (#1346)
1 parent 78db882 commit bdcdb3c

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

compiler/src/middle_end/matchcomp.re

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ module MatchTreeCompiler = {
804804
(
805805
~loc=Location.dummy_loc,
806806
~env=Env.empty,
807-
~mut_boxing=false,
807+
~mut_boxing,
808808
tree,
809809
values,
810810
expr,
@@ -843,6 +843,7 @@ module MatchTreeCompiler = {
843843
compile_tree_help(
844844
~loc,
845845
~env,
846+
~mut_boxing,
846847
true_tree,
847848
values,
848849
expr,
@@ -853,6 +854,7 @@ module MatchTreeCompiler = {
853854
compile_tree_help(
854855
~loc,
855856
~env,
857+
~mut_boxing,
856858
false_tree,
857859
values,
858860
expr,
@@ -897,6 +899,7 @@ module MatchTreeCompiler = {
897899
compile_tree_help(
898900
~loc,
899901
~env,
902+
~mut_boxing,
900903
true_tree,
901904
values,
902905
expr,
@@ -907,6 +910,7 @@ module MatchTreeCompiler = {
907910
compile_tree_help(
908911
~loc,
909912
~env,
913+
~mut_boxing,
910914
false_tree,
911915
values,
912916
expr,
@@ -1045,6 +1049,7 @@ module MatchTreeCompiler = {
10451049
compile_tree_help(
10461050
~loc,
10471051
~env,
1052+
~mut_boxing,
10481053
rest,
10491054
new_values,
10501055
expr,
@@ -1056,6 +1061,7 @@ module MatchTreeCompiler = {
10561061
compile_tree_help(
10571062
~loc,
10581063
~env,
1064+
~mut_boxing,
10591065
rest_tree,
10601066
swap_list(idx, values),
10611067
expr,
@@ -1075,6 +1081,7 @@ module MatchTreeCompiler = {
10751081
compile_tree_help(
10761082
~loc,
10771083
~env,
1084+
~mut_boxing,
10781085
base_tree,
10791086
values,
10801087
expr,
@@ -1120,6 +1127,7 @@ module MatchTreeCompiler = {
11201127
compile_tree_help(
11211128
~loc,
11221129
~env,
1130+
~mut_boxing,
11231131
tree,
11241132
values,
11251133
expr,
@@ -1154,7 +1162,7 @@ module MatchTreeCompiler = {
11541162
// Dummy value to be filled in during matching
11551163
let dummy_value = Imm.const(Const_wasmi32(0l));
11561164
if (mut_boxing) {
1157-
BLet(
1165+
BLetMut(
11581166
id,
11591167
Comp.prim1(~allocation_type=Managed, BoxBind, dummy_value),
11601168
Nonglobal,
@@ -1203,7 +1211,16 @@ module MatchTreeCompiler = {
12031211
let bind_setup = collect_bindings(branches);
12041212
let {imm_loc: loc, imm_env: env} = expr;
12051213
let (ans, setup) =
1206-
compile_tree_help(~loc, ~env, tree, [expr], expr, helpI, helpConst);
1214+
compile_tree_help(
1215+
~loc,
1216+
~env,
1217+
~mut_boxing=false,
1218+
tree,
1219+
[expr],
1220+
expr,
1221+
helpI,
1222+
helpConst,
1223+
);
12071224
let jmp_name = Ident.create("match_dest");
12081225
let setup = bind_setup @ setup @ [BLet(jmp_name, ans, Nonglobal)];
12091226
let switch_branches =

compiler/test/suites/let_mut.re

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ describe("let mut", ({test, testSkip}) => {
4242
"record Rec {foo: Number, bar: Bool}; {let mut {foo, bar} = {foo: 5, bar: false}; foo = 6; bar = true; print(foo); print(bar)}",
4343
"6\ntrue\n",
4444
);
45+
assertRun(
46+
"let-mut_destructure5",
47+
{|
48+
let run = () => {
49+
let mut a = 1
50+
let mut b = 2
51+
let mut (x, y) = (a, b)
52+
b = 7
53+
print(y)
54+
}
55+
run()
56+
|},
57+
"2\n",
58+
);
4559
// not-mut let errors
4660
assertCompileError(
4761
"let-mut_err1",

compiler/test/suites/pattern_matching.re

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,34 @@ describe("pattern matching", ({test, testSkip}) => {
357357
|},
358358
"7\n",
359359
);
360+
assertRun(
361+
"destructure_mut_tuple",
362+
{|
363+
let run = () => {
364+
let mut (a, b) = (1, 2)
365+
366+
print(b)
367+
}
368+
369+
run()
370+
|},
371+
"2\n",
372+
);
373+
assertRun(
374+
"destructure_mut_tuple_closure",
375+
{|
376+
let run = () => {
377+
let mut (a, b) = (1, 2)
378+
379+
print(b)
380+
381+
() => b
382+
}
383+
384+
run()
385+
|},
386+
"2\n",
387+
);
360388
assertRun(
361389
"destructure_record",
362390
{|

0 commit comments

Comments
 (0)