From 9af8bb988f7e8d26737ec6b910647124764fd187 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Fri, 4 Dec 2026 17:58:08 +0000 Subject: [PATCH] Simplify bodies inlined while compiling to JavaScript Why: A call to another module's function that also passes a parameter of the caller is inlined late, while compiling to JavaScript, after the alias pass has run. So the inlined body kept what that pass removes elsewhere: a `Some(x)` argument was bound and unwrapped again, and a function argument was called instead of inlined. The alias pass now also runs on such a body before it is compiled. JS modified (a literal Some next to a parameter): Example: let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0) Before (js): function knownSome(x, f) { let opt = x; let opt$1 = opt !== undefined ? Primitive_option.some(f(Primitive_option.valFromOption(opt))) : undefined; if (opt$1 !== undefined) { return opt$1; } else { return 0; } } After (js): function knownSome(x, f) { let opt = Primitive_option.some(f(x)); if (opt !== undefined) { return opt; } else { return 0; } } JS modified (a function argument): Example: let mapped = (opt: option) => opt->Option.map(x => x + 1) Before (js): function mapped(opt) { if (opt !== undefined) { return Primitive_option.some((x => x + 1 | 0)(opt)); } } After (js): function mapped(opt) { if (opt !== undefined) { return Primitive_option.some(opt + 1 | 0); } } Review: Why does the late pass get a fresh `ident_tbl`? The table was filled by `collect_info` before `simplify_alias 3`, `simplify_lets`, `sroa` and `simplify_exits` rewrote the program, so at codegen it can give a function body or a `Some` payload naming a variable those passes removed or turned into an assigned one: inlining a let-bound local function from it printed `p + k` with no `k` in scope. The body is now simplified knowing only the parameters it binds; the caller is still compiled with the real `meta`. Review: Which variables count as assigned there? The pass used to take only the ones `lam` assigns, but at codegen `lam` is just the inlined body, so a variable of the caller assigned by a call in it looked immutable and its read moved past that call. The pass now starts from `meta.assignable` as well. In the other direction, the pass may inline a further function that assigns a variable of its own, so the variables the body assigns are collected after the pass, before it is compiled. Review: What stays? `Option.map` still wraps its result with `Primitive_option.some`; that needs a mark on the result of the call. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq Signed-off-by: Christoph Knittel --- changes/late-inline-alias-pass.polish.md | 1 + compiler/backend/lam_compile.ml | 24 +- compiler/backend/lam_pass_remove_alias.ml | 11 +- .../compiler_tests/src/inline_export_test.mjs | 2 +- .../src/inline_late_alias_lib.mjs | 75 ++++++ .../src/inline_late_alias_lib.res | 61 +++++ .../src/inline_late_alias_test.mjs | 222 ++++++++++++++++++ .../src/inline_late_alias_test.res | 159 +++++++++++++ .../src/inline_late_alias_wrap.mjs | 20 ++ .../src/inline_late_alias_wrap.res | 4 + .../src/inline_option_unwrap_test.mjs | 66 ++++-- .../src/inline_option_unwrap_test.res | 10 + 12 files changed, 624 insertions(+), 31 deletions(-) create mode 100644 changes/late-inline-alias-pass.polish.md create mode 100644 tests/compiler_tests/src/inline_late_alias_lib.mjs create mode 100644 tests/compiler_tests/src/inline_late_alias_lib.res create mode 100644 tests/compiler_tests/src/inline_late_alias_test.mjs create mode 100644 tests/compiler_tests/src/inline_late_alias_test.res create mode 100644 tests/compiler_tests/src/inline_late_alias_wrap.mjs create mode 100644 tests/compiler_tests/src/inline_late_alias_wrap.res diff --git a/changes/late-inline-alias-pass.polish.md b/changes/late-inline-alias-pass.polish.md new file mode 100644 index 0000000000..11ae786a8a --- /dev/null +++ b/changes/late-inline-alias-pass.polish.md @@ -0,0 +1 @@ +- Simplify the body of a function from another module that is inlined while compiling to JavaScript, so a `Some(x)` argument is read directly and a function argument applied in the body is inlined. diff --git a/compiler/backend/lam_compile.ml b/compiler/backend/lam_compile.ml index 0f61772afa..5239df5a38 100644 --- a/compiler/backend/lam_compile.ml +++ b/compiler/backend/lam_compile.ml @@ -589,12 +589,28 @@ let compile output_prefix = let _, param_map = Lam_closure.is_closed_with_map Set_ident.empty params body in + (* What [meta.ident_tbl] records about the caller's bindings was + collected before the last passes rewrote them, so it may give + bodies or payloads naming variables that are gone. The inlined body + is simplified with a fresh table, which only gets what is known + about the parameters it binds. *) + let ident_tbl = Hash_ident.create 17 in let inlined = - Lam_beta_reduce.propagate_beta_reduce_with_map lambda_cxt.meta param_map - params body ap_args + Lam_beta_reduce.propagate_beta_reduce_with_map + {lambda_cxt.meta with ident_tbl} + param_map params body ap_args in - (* The inlined body comes from another module, so the variables it - assigns are not in [meta.assignable] yet. *) + (* The alias pass ran before this call was inlined: run it on the + inlined body, so that a known argument, such as a [Some], is + resolved there too *) + let inlined = + Lam_pass_remove_alias.simplify_alias + {lambda_cxt.meta with ident_tbl} + inlined + in + (* The inlined body comes from another module, and the alias pass may + have inlined more into it, so the variables it assigns are not in + [meta.assignable] yet. *) let meta = { lambda_cxt.meta with diff --git a/compiler/backend/lam_pass_remove_alias.ml b/compiler/backend/lam_pass_remove_alias.ml index 888e21aaff..5e771cd606 100644 --- a/compiler/backend/lam_pass_remove_alias.ml +++ b/compiler/backend/lam_pass_remove_alias.ml @@ -56,9 +56,14 @@ let max_inline_depth = 4 let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = (* Inlining keeps the reads of assigned variables in order (see [Lam_beta_reduce]), so it needs the variables some [Lassign] assigns. - Only [Lam_pass_sroa], which runs later, makes [Lassign]s, so [lam] has - none yet; computing them here keeps the inlining sound if that changes. *) - let assignable = ref (Lambda_traverse.assigned_variables lam) in + Before [Lam_pass_sroa], only bodies inlined from other modules have + [Lassign]s. When this pass runs on a body inlined while compiling to + JavaScript, [lam] is only that body: the variables of the function + around it that are assigned come from [meta.assignable]. *) + let assignable = + ref + (Set_ident.union meta.assignable (Lambda_traverse.assigned_variables lam)) + in let current_meta () = {meta with assignable = !assignable} in (* A body inlined from another module was optimized by [Lam_pass_sroa] there, so it may assign variables, and inlining renamed them. They are diff --git a/tests/compiler_tests/src/inline_export_test.mjs b/tests/compiler_tests/src/inline_export_test.mjs index 5b76b2b6a7..b88054bc51 100644 --- a/tests/compiler_tests/src/inline_export_test.mjs +++ b/tests/compiler_tests/src/inline_export_test.mjs @@ -18,7 +18,7 @@ let mappedNested = Primitive_option.some(Primitive_option.valFromOption(nested)) function mapValue(opt) { if (opt !== undefined) { - return Primitive_option.some((x => x + 1 | 0)(opt)); + return Primitive_option.some(opt + 1 | 0); } } diff --git a/tests/compiler_tests/src/inline_late_alias_lib.mjs b/tests/compiler_tests/src/inline_late_alias_lib.mjs new file mode 100644 index 0000000000..0a3865b11a --- /dev/null +++ b/tests/compiler_tests/src/inline_late_alias_lib.mjs @@ -0,0 +1,75 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs"; + +function id(x) { + return x; +} + +let c = id(3); + +function apply(x, f) { + return f(x); +} + +function apply2(p, v, g) { + return g(v, p); +} + +function unwrapG(x, o) { + if (o !== undefined) { + return [ + Primitive_option.valFromOption(o), + x + ]; + } +} + +function weird(p, o, f) { + f(); + if (o !== undefined) { + return o + p | 0; + } else { + return p; + } +} + +function weirdT(p, t, f) { + f(); + return t[0] + p | 0; +} + +function countUp(p, n) { + let r = p; + let tmp = r; + for (let _for = 0; _for <= n; ++_for) { + r = r + 1 | 0; + } + return [ + tmp, + r + ]; +} + +function weirdG(p, o, f) { + f(); + if (o !== undefined) { + return [ + Primitive_option.valFromOption(o), + p + ]; + } +} + +export { + id, + c, + apply, + apply2, + unwrapG, + weird, + weirdT, + countUp, + weirdG, +} +/* c Not a pure module */ diff --git a/tests/compiler_tests/src/inline_late_alias_lib.res b/tests/compiler_tests/src/inline_late_alias_lib.res new file mode 100644 index 0000000000..1849428092 --- /dev/null +++ b/tests/compiler_tests/src/inline_late_alias_lib.res @@ -0,0 +1,61 @@ +// Inlined by inline_late_alias_test.res. Each call there passes a parameter of +// the caller, so it is inlined while compiling to JavaScript. +@inline(never) let id = x => x + +let c = id(3) + +@inline +let apply = (x, f) => f(x) + +@inline +let apply2 = (p: int, v, g) => g(v, p) + +@inline +let unwrapG = (x: int, o: option<'a>) => + switch o { + | Some(v) => Some((v, x)) + | None => None + } + +// [f] may assign the variable the option or the tuple was made from. +@inline +let weird = (p: int, o: option, f) => { + f() + switch o { + | Some(v) => v + p + | None => p + } +} + +@inline +let weirdT = (p: int, t: (int, int), f) => { + f() + let (a, _) = t + a + p +} + +// Reads [r] before the loop that assigns it. Inlined into +// inline_late_alias_wrap.res's [countUpWrapped] only by the alias pass that +// runs while compiling to JavaScript. +@inline +let countUp = (p: int, n: int) => { + let r = ref(p) + ( + r.contents, + { + for _ in 0 to n { + r := r.contents + 1 + } + r.contents + }, + ) +} + +@inline +let weirdG = (p: int, o: option<'a>, f) => { + f() + switch o { + | Some(v) => Some((v, p)) + | None => None + } +} diff --git a/tests/compiler_tests/src/inline_late_alias_test.mjs b/tests/compiler_tests/src/inline_late_alias_test.mjs new file mode 100644 index 0000000000..808fe2735e --- /dev/null +++ b/tests/compiler_tests/src/inline_late_alias_test.mjs @@ -0,0 +1,222 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Mocha from "mocha"; +import * as Test_utils from "./test_utils.mjs"; +import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs"; +import * as Inline_late_alias_lib from "./inline_late_alias_lib.mjs"; +import * as Inline_late_alias_wrap from "./inline_late_alias_wrap.mjs"; + +function letBoundGlobal(p) { + let h = x => x + Inline_late_alias_lib.c | 0; + return h(p) + h(p) | 0; +} + +function tupleField(p, q) { + let h = x => x + q.length | 0; + return h(p) + h(p) | 0; +} + +function assignedRef(p) { + let r = 0; + let h = x => x + r | 0; + r = 10; + return h(p) + h(p) | 0; +} + +function assignedRecord(p) { + let r_a = 1; + let r_b = 2; + let h = x => x + r_b | 0; + r_b = 10; + r_a = r_a + r_b | 0; + return (h(p) + h(p) | 0) + r_a | 0; +} + +function someGlobal(p) { + let x = Inline_late_alias_lib.c; + return [ + x !== undefined ? [ + x, + p + ] : undefined, + x + ]; +} + +function someField(p, q) { + let x = q[0]; + return [ + x !== undefined ? [ + x, + p + ] : undefined, + x + ]; +} + +function someTupleField(p, q) { + let x = q; + return [ + x !== undefined ? [ + x, + p + ] : undefined, + x + ]; +} + +function readBeforeCall(p, n) { + let r = 1; + let f = () => { + for (let i = 0; i <= n; ++i) { + r = r + i | 0; + } + }; + let x = r; + f(); + let res = x + p | 0; + return [ + res, + r + ]; +} + +function someBeforeCall(p, n) { + let r = 1; + let f = () => { + for (let i = 0; i <= n; ++i) { + r = r + i | 0; + } + }; + let o = r; + f(); + let res = o + p | 0; + return [ + res, + r + ]; +} + +function tupleBeforeCall(p, n) { + let r = 1; + let f = () => { + for (let i = 0; i <= n; ++i) { + r = r + i | 0; + } + }; + let t_0 = r; + f(); + let res = t_0 + p | 0; + return [ + res, + r + ]; +} + +function genericSomeBeforeCall(p, n, q, q2) { + let r = q; + let f = () => { + for (let _for = 0; _for <= n; ++_for) { + r = q2; + } + }; + let o = Primitive_option.some(r); + f(); + let res = [ + Primitive_option.valFromOption(o), + p + ]; + return [ + res, + r + ]; +} + +function nestedAssigned(p, n) { + let r = p; + let tmp = r; + for (let _for = 0; _for <= n; ++_for) { + r = r + 1 | 0; + } + return [ + tmp, + r + ]; +} + +Mocha.describe("Inline_late_alias_test", () => { + Mocha.test("a function reading a let-bound global", () => Test_utils.eq("File \"inline_late_alias_test.res\", line 113, characters 7-14", 8, letBoundGlobal(1))); + Mocha.test("a function reading a field of a known tuple", () => Test_utils.eq("File \"inline_late_alias_test.res\", line 117, characters 7-14", 8, tupleField(1, [ + 1, + 2, + 3 + ]))); + Mocha.test("a function reading a ref assigned before the calls", () => Test_utils.eq("File \"inline_late_alias_test.res\", line 121, characters 7-14", 22, assignedRef(1))); + Mocha.test("a function reading a mutable field assigned before the calls", () => Test_utils.eq("File \"inline_late_alias_test.res\", line 125, characters 7-14", 33, assignedRecord(1))); + Mocha.test("a Some of a let-bound global", () => Test_utils.eq("File \"inline_late_alias_test.res\", line 129, characters 7-14", [ + [ + 3, + 1 + ], + 3 + ], someGlobal(1))); + Mocha.test("a Some of a field of a parameter", () => Test_utils.eq("File \"inline_late_alias_test.res\", line 133, characters 7-14", [ + [ + 5, + 1 + ], + 5 + ], someField(1, [ + 5, + 6 + ]))); + Mocha.test("a Some of a field of a known tuple", () => Test_utils.eq("File \"inline_late_alias_test.res\", line 137, characters 7-14", [ + [ + "a", + 1 + ], + "a" + ], someTupleField(1, "a"))); + Mocha.test("an argument read before a call that assigns it", () => Test_utils.eq("File \"inline_late_alias_test.res\", line 141, characters 7-14", [ + 2, + 7 + ], readBeforeCall(1, 3))); + Mocha.test("a Some made before a call that assigns its variable", () => Test_utils.eq("File \"inline_late_alias_test.res\", line 145, characters 7-14", [ + 2, + 7 + ], someBeforeCall(1, 3))); + Mocha.test("a tuple made before a call that assigns its variable", () => Test_utils.eq("File \"inline_late_alias_test.res\", line 149, characters 7-14", [ + 2, + 7 + ], tupleBeforeCall(1, 3))); + Mocha.test("a generic Some made before a call that assigns its variable", () => Test_utils.eq("File \"inline_late_alias_test.res\", line 153, characters 7-14", [ + [ + "a", + 1 + ], + "b" + ], genericSomeBeforeCall(1, 3, "a", "b"))); + Mocha.test("a variable assigned in a body inlined by the late alias pass", () => Test_utils.eq("File \"inline_late_alias_test.res\", line 157, characters 7-14", [ + 10, + 14 + ], nestedAssigned(10, 3))); +}); + +let L; + +export { + L, + letBoundGlobal, + tupleField, + assignedRef, + assignedRecord, + someGlobal, + someField, + someTupleField, + readBeforeCall, + someBeforeCall, + tupleBeforeCall, + genericSomeBeforeCall, + nestedAssigned, +} +/* Not a pure module */ diff --git a/tests/compiler_tests/src/inline_late_alias_test.res b/tests/compiler_tests/src/inline_late_alias_test.res new file mode 100644 index 0000000000..bf4a3f1251 --- /dev/null +++ b/tests/compiler_tests/src/inline_late_alias_test.res @@ -0,0 +1,159 @@ +// A call inlined while compiling to JavaScript is simplified after the other +// passes rewrote the function around it: what was known about that function's +// variables before then must not be used, and its assigned variables must be +// read where they were read. +open Mocha +open Test_utils + +module L = Inline_late_alias_lib + +let letBoundGlobal = p => { + let k = L.c + let h = x => x + k + L.apply(p, h) + L.apply(p, h) +} + +let tupleField = (p, q: array) => { + let t = (q, 1) + let (a, _) = t + let h = x => x + Array.length(a) + L.apply(p, h) + L.apply(p, h) +} + +let assignedRef = p => { + let r = ref(0) + let h = x => x + r.contents + r := 10 + L.apply(p, h) + L.apply(p, h) +} + +type pt = {mutable a: int, mutable b: int} + +let assignedRecord = p => { + let r = {a: 1, b: 2} + let h = x => x + r.b + r.b = 10 + r.a = r.a + r.b + L.apply(p, h) + L.apply(p, h) + r.a +} + +let someGlobal = p => { + let y = L.c + let x = Some(y) + (L.unwrapG(p, x), x) +} + +let someField = (p, q: (int, int)) => { + let (a, _) = q + let x = Some(a) + (L.unwrapG(p, x), x) +} + +let someTupleField = (p, q: string) => { + let t = (q, 1) + let (y, _) = t + let x = Some(y) + (L.unwrapG(p, x), x) +} + +let readBeforeCall = (p, n) => { + let r = ref(1) + let f = () => { + for i in 0 to n { + r := r.contents + i + } + } + let res = L.apply2(p, r.contents, (x, pp) => { + f() + x + pp + }) + (res, r.contents) +} + +let someBeforeCall = (p, n) => { + let r = ref(1) + let f = () => { + for i in 0 to n { + r := r.contents + i + } + } + let res = L.weird(p, Some(r.contents), f) + (res, r.contents) +} + +let tupleBeforeCall = (p, n) => { + let r = ref(1) + let f = () => { + for i in 0 to n { + r := r.contents + i + } + } + let res = L.weirdT(p, (r.contents, 2), f) + (res, r.contents) +} + +let genericSomeBeforeCall = (p, n, q: 'a, q2: 'a) => { + let r = ref(q) + let f = () => { + for _ in 0 to n { + r := q2 + } + } + let res = L.weirdG(p, Some(r.contents), f) + (res, r.contents) +} + +// The late alias pass inlines [countUp] into the wrapper's body, which brings +// in an assigned variable. Not inlined itself, so the test runs this code. +@inline(never) +let nestedAssigned = (p, n) => Inline_late_alias_wrap.countUpWrapped(p, n) + +describe(__MODULE__, () => { + test("a function reading a let-bound global", () => { + eq(__LOC__, 8, letBoundGlobal(1)) + }) + + test("a function reading a field of a known tuple", () => { + eq(__LOC__, 8, tupleField(1, [1, 2, 3])) + }) + + test("a function reading a ref assigned before the calls", () => { + eq(__LOC__, 22, assignedRef(1)) + }) + + test("a function reading a mutable field assigned before the calls", () => { + eq(__LOC__, 33, assignedRecord(1)) + }) + + test("a Some of a let-bound global", () => { + eq(__LOC__, (Some((3, 1)), Some(3)), someGlobal(1)) + }) + + test("a Some of a field of a parameter", () => { + eq(__LOC__, (Some((5, 1)), Some(5)), someField(1, (5, 6))) + }) + + test("a Some of a field of a known tuple", () => { + eq(__LOC__, (Some(("a", 1)), Some("a")), someTupleField(1, "a")) + }) + + test("an argument read before a call that assigns it", () => { + eq(__LOC__, (2, 7), readBeforeCall(1, 3)) + }) + + test("a Some made before a call that assigns its variable", () => { + eq(__LOC__, (2, 7), someBeforeCall(1, 3)) + }) + + test("a tuple made before a call that assigns its variable", () => { + eq(__LOC__, (2, 7), tupleBeforeCall(1, 3)) + }) + + test("a generic Some made before a call that assigns its variable", () => { + eq(__LOC__, (Some(("a", 1)), "b"), genericSomeBeforeCall(1, 3, "a", "b")) + }) + + test("a variable assigned in a body inlined by the late alias pass", () => { + eq(__LOC__, (10, 14), nestedAssigned(10, 3)) + }) +}) diff --git a/tests/compiler_tests/src/inline_late_alias_wrap.mjs b/tests/compiler_tests/src/inline_late_alias_wrap.mjs new file mode 100644 index 0000000000..ceb3f53de4 --- /dev/null +++ b/tests/compiler_tests/src/inline_late_alias_wrap.mjs @@ -0,0 +1,20 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Inline_late_alias_lib from "./inline_late_alias_lib.mjs"; + +function countUpWrapped(p, n) { + let r = p; + let tmp = r; + for (let _for = 0; _for <= n; ++_for) { + r = r + 1 | 0; + } + return [ + tmp, + r + ]; +} + +export { + countUpWrapped, +} +/* Inline_late_alias_lib Not a pure module */ diff --git a/tests/compiler_tests/src/inline_late_alias_wrap.res b/tests/compiler_tests/src/inline_late_alias_wrap.res new file mode 100644 index 0000000000..71a440d3ad --- /dev/null +++ b/tests/compiler_tests/src/inline_late_alias_wrap.res @@ -0,0 +1,4 @@ +// Calls a function of inline_late_alias_lib.res with its own parameters, so +// inlining it waits until this wrapper is itself inlined. +@inline +let countUpWrapped = (p: int, n: int) => Inline_late_alias_lib.countUp(p, n) diff --git a/tests/compiler_tests/src/inline_option_unwrap_test.mjs b/tests/compiler_tests/src/inline_option_unwrap_test.mjs index 6e8d6848cd..c6e445124e 100644 --- a/tests/compiler_tests/src/inline_option_unwrap_test.mjs +++ b/tests/compiler_tests/src/inline_option_unwrap_test.mjs @@ -30,7 +30,7 @@ function stdlibIntOr(opt) { function mapped(opt) { if (opt !== undefined) { - return Primitive_option.some((x => x + 1 | 0)(opt)); + return Primitive_option.some(opt + 1 | 0); } } @@ -138,43 +138,61 @@ function castOnce(opt) { ]; } +function knownSome(x, f) { + let opt = Primitive_option.some(f(x)); + if (opt !== undefined) { + return opt; + } else { + return 0; + } +} + +function knownSomeNested(x, f) { + return Primitive_option.some(f(x)); +} + Mocha.describe("Inline_option_unwrap_test", () => { + Mocha.test("literal Some with a parameter argument", () => { + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 64, characters 7-14", knownSome(2, x => x + 1 | 0), 3); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 65, characters 7-14", Primitive_option.some(true), true); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 66, characters 7-14", Primitive_option.some(false), false); + }); Mocha.test("payload that cannot be undefined", () => { - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 59, characters 7-14", 2, 2); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 60, characters 7-14", 0, 0); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 61, characters 7-14", 2, 2); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 62, characters 7-14", 0, 0); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 63, characters 7-14", mapped(2), 3); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 64, characters 7-14", mapped(undefined), undefined); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 65, characters 7-14", 2, 2); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 66, characters 7-14", 2, 2); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 67, characters 7-14", 0, 0); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 68, characters 7-14", 2, 2); Test_utils.eq("File \"inline_option_unwrap_test.res\", line 69, characters 7-14", 2, 2); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 70, characters 7-14", 1, 1); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 71, characters 7-14", "ab".length, 2); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 70, characters 7-14", 0, 0); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 71, characters 7-14", 2, 2); Test_utils.eq("File \"inline_option_unwrap_test.res\", line 72, characters 7-14", 0, 0); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 73, characters 7-14", nullableInput("x"), [ + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 73, characters 7-14", mapped(2), 3); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 74, characters 7-14", mapped(undefined), undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 75, characters 7-14", 2, 2); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 76, characters 7-14", 2, 2); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 77, characters 7-14", 0, 0); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 78, characters 7-14", 2, 2); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 79, characters 7-14", 2, 2); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 80, characters 7-14", 1, 1); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 81, characters 7-14", "ab".length, 2); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 82, characters 7-14", 0, 0); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 83, characters 7-14", nullableInput("x"), [ "xa", "xb" ]); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 74, characters 7-14", nullableInput(null), [ + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 84, characters 7-14", nullableInput(null), [ undefined, undefined ]); }); Mocha.test("payload that can be undefined", () => { - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 77, characters 7-14", undefined, undefined); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 78, characters 7-14", undefined, undefined); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 79, characters 7-14", undefined, undefined); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 80, characters 7-14", 1, 1); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 81, characters 7-14", 0, 0); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 82, characters 7-14", undefined, undefined); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 83, characters 7-14", undefined, undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 87, characters 7-14", undefined, undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 88, characters 7-14", undefined, undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 89, characters 7-14", undefined, undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 90, characters 7-14", 1, 1); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 91, characters 7-14", 0, 0); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 92, characters 7-14", undefined, undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 93, characters 7-14", undefined, undefined); }); Mocha.test("cast at one call", () => { let match = castOnce(Primitive_option.some(undefined)); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 87, characters 7-14", match[1], undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 97, characters 7-14", match[1], undefined); }); }); @@ -196,5 +214,7 @@ export { nullableOr, genericOr, castOnce, + knownSome, + knownSomeNested, } /* Not a pure module */ diff --git a/tests/compiler_tests/src/inline_option_unwrap_test.res b/tests/compiler_tests/src/inline_option_unwrap_test.res index 3251ba6821..8ae2af01d5 100644 --- a/tests/compiler_tests/src/inline_option_unwrap_test.res +++ b/tests/compiler_tests/src/inline_option_unwrap_test.res @@ -54,7 +54,17 @@ let castOnce = (opt: option>) => { (unwrapped, inner) } +// A call that also passes a parameter is inlined while compiling to +// JavaScript. A literal Some argument is still read directly. +let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0) +let knownSomeNested = (x: option, f) => Some(x)->Option.map(f) + describe(__MODULE__, () => { + test("literal Some with a parameter argument", () => { + eq(__LOC__, knownSome(2, x => x + 1), 3) + eq(__LOC__, knownSomeNested(None, o => o == None), Some(true)) + eq(__LOC__, knownSomeNested(Some(1), o => o == None), Some(false)) + }) test("payload that cannot be undefined", () => { eq(__LOC__, intOr(Some(2)), 2) eq(__LOC__, intOr(None), 0)