Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): Fix compilation of closure scope mutable destructuring #1346

Merged
merged 2 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions compiler/src/middle_end/matchcomp.re
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ module MatchTreeCompiler = {
(
~loc=Location.dummy_loc,
~env=Env.empty,
~mut_boxing=false,
~mut_boxing,
tree,
values,
expr,
Expand Down Expand Up @@ -843,6 +843,7 @@ module MatchTreeCompiler = {
compile_tree_help(
~loc,
~env,
~mut_boxing,
true_tree,
values,
expr,
Expand All @@ -853,6 +854,7 @@ module MatchTreeCompiler = {
compile_tree_help(
~loc,
~env,
~mut_boxing,
false_tree,
values,
expr,
Expand Down Expand Up @@ -897,6 +899,7 @@ module MatchTreeCompiler = {
compile_tree_help(
~loc,
~env,
~mut_boxing,
true_tree,
values,
expr,
Expand All @@ -907,6 +910,7 @@ module MatchTreeCompiler = {
compile_tree_help(
~loc,
~env,
~mut_boxing,
false_tree,
values,
expr,
Expand Down Expand Up @@ -1045,6 +1049,7 @@ module MatchTreeCompiler = {
compile_tree_help(
~loc,
~env,
~mut_boxing,
rest,
new_values,
expr,
Expand All @@ -1056,6 +1061,7 @@ module MatchTreeCompiler = {
compile_tree_help(
~loc,
~env,
~mut_boxing,
rest_tree,
swap_list(idx, values),
expr,
Expand All @@ -1075,6 +1081,7 @@ module MatchTreeCompiler = {
compile_tree_help(
~loc,
~env,
~mut_boxing,
base_tree,
values,
expr,
Expand Down Expand Up @@ -1120,6 +1127,7 @@ module MatchTreeCompiler = {
compile_tree_help(
~loc,
~env,
~mut_boxing,
tree,
values,
expr,
Expand Down Expand Up @@ -1154,7 +1162,7 @@ module MatchTreeCompiler = {
// Dummy value to be filled in during matching
let dummy_value = Imm.const(Const_wasmi32(0l));
if (mut_boxing) {
BLet(
BLetMut(
id,
Comp.prim1(~allocation_type=Managed, BoxBind, dummy_value),
Nonglobal,
Expand Down Expand Up @@ -1203,7 +1211,16 @@ module MatchTreeCompiler = {
let bind_setup = collect_bindings(branches);
let {imm_loc: loc, imm_env: env} = expr;
let (ans, setup) =
compile_tree_help(~loc, ~env, tree, [expr], expr, helpI, helpConst);
compile_tree_help(
~loc,
~env,
~mut_boxing=false,
tree,
[expr],
expr,
helpI,
helpConst,
);
let jmp_name = Ident.create("match_dest");
let setup = bind_setup @ setup @ [BLet(jmp_name, ans, Nonglobal)];
let switch_branches =
Expand Down
28 changes: 28 additions & 0 deletions compiler/test/suites/pattern_matching.re
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,34 @@ describe("pattern matching", ({test, testSkip}) => {
|},
"7\n",
);
assertRun(
"destructure_mut_tuple",
{|
let run = () => {
let mut (a, b) = (1, 2)

print(b)
}

run()
|},
"2\n",
);
assertRun(
"destructure_mut_tuple_closure",
{|
let run = () => {
let mut (a, b) = (1, 2)

print(b)

() => b
}

run()
|},
"2\n",
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests have me wondering about this:

let run = () => {
        let mut a = 1
        let mut b = 2
        let mut (x, y) = (a, b)
        b = 7
        print(y)
}
run()

What happens?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You get 2!

Copy link
Member

@phated phated Jun 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add that as a test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

assertRun(
"destructure_record",
{|
Expand Down