Skip to content
Draft
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/tailcall-inline-nested-declaration.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix a `ReferenceError` when a local function that is inlined into another one in turn passes a function to a third one it inlines: the parameter's declaration was dropped.
37 changes: 9 additions & 28 deletions compiler/backend/js_pass_tailcall_inline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ let inline_call (assignable : Set_ident.t) (immutable_list : bool list) params
*)
let super = Js_record_map.super

let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats =
let subst (assignable : Set_ident.t) stats =
{
super with
statement =
Expand All @@ -203,23 +203,6 @@ let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats =
block =
(fun self bs ->
match bs with
| ({
statement_desc =
Variable
({value = Some ({expression_desc = Fun _; _} as v)} as vd);
comment = _;
} as st)
:: rest -> (
let is_export = Set_ident.mem vd.ident export_set in
if is_export then self.statement self st :: self.block self rest
else
match Hash_ident.find_opt stats vd.ident with
(* TODO: could be improved as [mem] *)
| None ->
if Js_analyzer.no_side_effect_expression v then
S.exp v :: self.block self rest
else self.block self rest
| Some _ -> self.statement self st :: self.block self rest)
| [
({
statement_desc =
Expand Down Expand Up @@ -262,14 +245,9 @@ let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats =
self.block self body
(* see #278 before changes*)
in
(* The function is marked dead above, so its declaration, which
comes earlier in the block, is removed: see [x :: xs] below *)
inline_call assignable no_tailcall params args processed_blocks
(* Ext_list.fold_right2
params args processed_blocks
(fun param arg acc ->
S.define_variable ~kind:Variable param arg :: acc) *)
(* Mark a function as dead means it will never be scanned,
here we inline the function
*)
| None | Some _ -> [self.statement self st])
| [
{
Expand Down Expand Up @@ -302,12 +280,15 @@ let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats =
(* see #278 before changes*)
in
inline_call assignable no_tailcall params args processed_blocks
| x :: xs -> self.statement self x :: self.block self xs
| x :: xs ->
(* The rest first: inlining a call in it marks the declaration of
the function dead, which removes it here *)
let xs = self.block self xs in
self.statement self x :: xs
| [] -> []);
}

let tailcall_inline ~assignable (program : J.program) =
let stats = Js_pass_get_used.get_stats program in
let export_set = program.export_set in
let obj = subst export_set assignable stats in
let obj = subst assignable stats in
obj.program obj program
43 changes: 43 additions & 0 deletions tests/compiler_tests/src/tailcall_inline_nested_test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Mocha from "mocha";
import * as Test_utils from "./test_utils.mjs";

let steps = {
contents: 0
};

function main(z) {
let a = n => {
steps.contents = steps.contents + n | 0;
steps.contents = (steps.contents + n | 0) + 1 | 0;
steps.contents = (steps.contents + n | 0) + 2 | 0;
if (n > 10) {
steps.contents = steps.contents + n | 0;
steps.contents = (steps.contents + n | 0) + 1 | 0;
steps.contents = (steps.contents + n | 0) + 2 | 0;
if (n > 100) {
return a(n - 100 | 0);
} else {
let k = x => x + n | 0;
steps.contents = steps.contents + n | 0;
steps.contents = (steps.contents + n | 0) + 1 | 0;
steps.contents = (steps.contents + n | 0) + 2 | 0;
return k(n);
}
} else {
return 0;
}
};
return a(z) + a(z + 1 | 0) | 0;
}

Mocha.describe("Tailcall_inline_nested_test", () => {
Mocha.test("a function inlined into one that is inlined in turn", () => Test_utils.eq("File \"tailcall_inline_nested_test.res\", line 41, characters 71-78", main(20), 82));
});

export {
steps,
main,
}
/* Not a pure module */
42 changes: 42 additions & 0 deletions tests/compiler_tests/src/tailcall_inline_nested_test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// `b` is inlined into `a` in JavaScript, and `c` into `b`. Inlining `c` binds
// its parameter `k` to a function, so the body of `b` that `a` gets declares
// `k`. That declaration must stay a declaration: it was once turned into an
// expression statement, and calling `k` failed with a ReferenceError.
open Mocha
open Test_utils

let steps = ref(0)

let main = z => {
let c = (k, y) => {
steps := steps.contents + y
steps := steps.contents + y + 1
steps := steps.contents + y + 2
k(y)
}
let rec a = n => {
steps := steps.contents + n
steps := steps.contents + n + 1
steps := steps.contents + n + 2
if n > 10 {
b(n)
} else {
0
}
}
and b = n => {
steps := steps.contents + n
steps := steps.contents + n + 1
steps := steps.contents + n + 2
if n > 100 {
a(n - 100)
} else {
c(x => x + n, n)
}
}
a(z) + a(z + 1)
}

describe(__MODULE__, () => {
test("a function inlined into one that is inlined in turn", () => eq(__LOC__, main(20), 82))
})
Loading