Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions changes/inline-try-catch.polish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- When inlining functions from other modules, include `@inline` functions that contain `try … catch`. They used to be called, because the variable a `catch` binds counted as captured from outside.
7 changes: 6 additions & 1 deletion compiler/backend/lam_closure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ let free_variables (export_idents : Set_ident.t) (params : stats Map_ident.t)
iter sink_pos e1;
local_add_list vars;
iter sink_pos e2
| Ltrywith (e1, _, e2) | Ltryfinally (e1, e2) ->
| Ltrywith (e1, exn, e2) ->
iter top e1;
(* The handler binds the exception. *)
local_add exn;
iter sink_pos e2
| Ltryfinally (e1, e2) ->
iter top e1;
iter sink_pos e2
| Lifthenelse (e1, e2, e3) ->
Expand Down
9 changes: 9 additions & 0 deletions tests/tests/src/inline_assign_cross_module_lib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,20 @@ function matchAfterAssign(x) {
}
}

function orDefault(f, $$default) {
try {
return f();
} catch (exn) {
return $$default;
}
}

export {
bumpTwice,
readThenAssign,
copyThenAssign,
negated,
matchAfterAssign,
orDefault,
}
/* No side effect */
8 changes: 8 additions & 0 deletions tests/tests/src/inline_assign_cross_module_lib.res
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ let matchAfterAssign = (x: string) => {
| _ => 2
}
}

// Inlined by inline_assign_cross_module_test.res: the handler binds the
// exception, which each inlined copy renames.
@inline
let orDefault = (f, default) =>
try f() catch {
| _ => default
}
38 changes: 31 additions & 7 deletions tests/tests/src/inline_assign_cross_module_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,37 +76,60 @@ function matchAfterAssignA() {
}
}

function caught() {
let tmp;
try {
tmp = 1;
} catch (exn) {
tmp = 0;
}
let tmp$1;
try {
throw new Error("boom");
} catch (exn$1) {
tmp$1 = 2;
}
return [
tmp,
tmp$1
];
}

Mocha.describe("Inline_assign_cross_module_test", () => {
Mocha.test("a variable the inlined function assigns, twice in a function", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 34, characters 7-14", 14, addBumped(id(3), id(4))));
Mocha.test("a variable the inlined function assigns, with a constant argument", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 38, characters 7-14", [
Mocha.test("a variable the inlined function assigns, twice in a function", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 39, characters 7-14", 14, addBumped(id(3), id(4))));
Mocha.test("a variable the inlined function assigns, with a constant argument", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 43, characters 7-14", [
12,
4
], bumpConstants()));
Mocha.test("a read of an assigned variable, passed to a closure before the assignment", () => {
let r = 5;
let p = r;
r = 10;
Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 42, characters 7-14", 15, p + r | 0);
Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 47, characters 7-14", 15, p + r | 0);
});
Mocha.test("a copy of an assigned variable, made before the assignment", () => {
let r = 1;
let s = r;
r = s + 1 | 0;
Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 46, characters 7-14", 3, s + r | 0);
Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 51, characters 7-14", 3, s + r | 0);
});
Mocha.test("a read of an assigned variable, passed to a bound closure before the assignment", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 50, characters 7-14", 15, readBeforeAssignBound()));
Mocha.test("a read of an assigned variable, passed to a bound closure before the assignment", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 55, characters 7-14", 15, readBeforeAssignBound()));
Mocha.test("a condition on an assigned variable bound to a constant", () => {
let r = true;
r = !r;
Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 54, characters 7-14", 2, r ? 1 : 2);
Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 59, characters 7-14", 2, r ? 1 : 2);
});
Mocha.test("a switch on an assigned variable bound to a constant", () => {
let r = "a";
r = "b";
let match = r;
let tmp = match === "a" ? 1 : 2;
Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 58, characters 7-14", 2, tmp);
Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 63, characters 7-14", 2, tmp);
});
Mocha.test("a function with a try handler, inlined twice in a function", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 67, characters 7-14", [
1,
2
], caught()));
});

export {
Expand All @@ -118,5 +141,6 @@ export {
readBeforeAssignBound,
negatedTrue,
matchAfterAssignA,
caught,
}
/* Not a pure module */
9 changes: 9 additions & 0 deletions tests/tests/src/inline_assign_cross_module_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ let negatedTrue = () => Inline_assign_cross_module_lib.negated(true)

let matchAfterAssignA = () => Inline_assign_cross_module_lib.matchAfterAssign("a")

let caught = () => (
Inline_assign_cross_module_lib.orDefault(() => 1, 0),
Inline_assign_cross_module_lib.orDefault(() => JsError.throwWithMessage("boom"), 2),
)

describe(__MODULE__, () => {
test("a variable the inlined function assigns, twice in a function", () => {
eq(__LOC__, 14, addBumped(id(3), id(4)))
Expand Down Expand Up @@ -57,4 +62,8 @@ describe(__MODULE__, () => {
test("a switch on an assigned variable bound to a constant", () => {
eq(__LOC__, 2, matchAfterAssignA())
})

test("a function with a try handler, inlined twice in a function", () => {
eq(__LOC__, (1, 2), caught())
})
})
Loading