diff --git a/tests/compiler_tests/src/inline_twin_lib.mjs b/tests/compiler_tests/src/inline_twin_lib.mjs new file mode 100644 index 0000000000..268b3008bc --- /dev/null +++ b/tests/compiler_tests/src/inline_twin_lib.mjs @@ -0,0 +1,121 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs"; + +function afterGI(p, o, f) { + f(); + if (o !== undefined) { + return [ + Primitive_option.valFromOption(o), + p + ]; + } +} + +function afterGN(p, o, f) { + f(); + if (o !== undefined) { + return [ + Primitive_option.valFromOption(o), + p + ]; + } +} + +function someI(x) { + return Primitive_option.some(x); +} + +function someN(x) { + return Primitive_option.some(x); +} + +function mapI(o, f) { + if (o !== undefined) { + return Primitive_option.some(f(Primitive_option.valFromOption(o))); + } +} + +function mapN(o, f) { + if (o !== undefined) { + return Primitive_option.some(f(Primitive_option.valFromOption(o))); + } +} + +function getOrI(o, d) { + if (o !== undefined) { + return Primitive_option.valFromOption(o); + } else { + return d; + } +} + +function getOrN(o, d) { + if (o !== undefined) { + return Primitive_option.valFromOption(o); + } else { + return d; + } +} + +function flatMapI(o, f) { + if (o !== undefined) { + return f(Primitive_option.valFromOption(o)); + } +} + +function flatMapN(o, f) { + if (o !== undefined) { + return f(Primitive_option.valFromOption(o)); + } +} + +function bothI(a, b, f) { + return [ + f(a), + f(b) + ]; +} + +function bothN(a, b, f) { + return [ + f(a), + f(b) + ]; +} + +function subI(a, b) { + return b - a | 0; +} + +function subN(a, b) { + return b - a | 0; +} + +function sndI(_a, b) { + return b; +} + +function sndN(_a, b) { + return b; +} + +export { + afterGI, + afterGN, + someI, + someN, + mapI, + mapN, + getOrI, + getOrN, + flatMapI, + flatMapN, + bothI, + bothN, + subI, + subN, + sndI, + sndN, +} +/* No side effect */ diff --git a/tests/compiler_tests/src/inline_twin_lib.res b/tests/compiler_tests/src/inline_twin_lib.res new file mode 100644 index 0000000000..110817a42a --- /dev/null +++ b/tests/compiler_tests/src/inline_twin_lib.res @@ -0,0 +1,79 @@ +// Helpers for inline_twin_test.res. Each one is defined twice with the same +// body: `xI` is @inline, so other modules inline it, and `xN` is +// @inline(never), so they call it. Keep the two bodies identical, apart from +// the I or N suffix of the helpers they call. No helper may update a local +// ref: other modules would then call it instead of inlining it (warning 113). + +// [f] runs before the other argument is read, and may assign the variable +// that argument was made from. +@inline +let afterGI = (p: int, o: option<'a>, f) => { + f() + switch o { + | Some(v) => Some((v, p)) + | None => None + } +} + +@inline(never) +let afterGN = (p: int, o: option<'a>, f) => { + f() + switch o { + | Some(v) => Some((v, p)) + | None => None + } +} + +@inline let someI = x => Some(x) +@inline(never) let someN = x => Some(x) + +@inline +let mapI = (o, f) => + switch o { + | Some(v) => Some(f(v)) + | None => None + } + +@inline(never) +let mapN = (o, f) => + switch o { + | Some(v) => Some(f(v)) + | None => None + } + +@inline +let getOrI = (o, d) => + switch o { + | Some(v) => v + | None => d + } + +@inline(never) +let getOrN = (o, d) => + switch o { + | Some(v) => v + | None => d + } + +@inline +let flatMapI = (o, f) => + switch o { + | Some(v) => f(v) + | None => None + } + +@inline(never) +let flatMapN = (o, f) => + switch o { + | Some(v) => f(v) + | None => None + } + +@inline let bothI = (a, b, f) => (f(a), f(b)) +@inline(never) let bothN = (a, b, f) => (f(a), f(b)) + +// Reads its parameters in the other order, or not at all. +@inline let subI = (a: int, b: int) => b - a +@inline(never) let subN = (a: int, b: int) => b - a +@inline let sndI = (_a, b) => b +@inline(never) let sndN = (_a, b) => b diff --git a/tests/compiler_tests/src/inline_twin_test.mjs b/tests/compiler_tests/src/inline_twin_test.mjs new file mode 100644 index 0000000000..0f04ec16ba --- /dev/null +++ b/tests/compiler_tests/src/inline_twin_test.mjs @@ -0,0 +1,230 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Mocha from "mocha"; +import * as Test_utils from "./test_utils.mjs"; +import * as Inline_twin_lib from "./inline_twin_lib.mjs"; +import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs"; + +function genSomeAfter(p, q, q2) { + let r = q; + let o = Primitive_option.some(r); + r = q2; + return [ + [ + Primitive_option.valFromOption(o), + p + ], + r + ]; +} + +function someNested(x) { + return [ + Primitive_option.some(x), + true + ]; +} + +function someNullable(x) { + return [ + Primitive_option.some(x), + true + ]; +} + +function someUnboxed(x) { + return [ + Primitive_option.some(x), + true + ]; +} + +function someGen(x) { + return [ + Primitive_option.some(x), + true + ]; +} + +function mapToOpt(x, f) { + return Primitive_option.some(f(x)); +} + +function someSome(x) { + let o = Primitive_option.some(Primitive_option.some(x)); + return Primitive_option.valFromOption(o); +} + +function flatMapNested(x, f) { + let o = f(x); + if (o !== undefined) { + return Primitive_option.valFromOption(o); + } else { + return -1; + } +} + +function effects(x, f, g) { + let log = []; + let eff = (tag, v) => { + log.push(tag); + return v; + }; + let a = eff("a", x); + let b = eff("b", x + 1 | 0); + let a_0 = f(a); + let a_1 = f(b); + let a$1 = [ + a_0, + a_1 + ]; + let a$2 = eff("c", x); + let b$1 = eff("d", (x << 1)); + let b$2 = b$1 - a$2 | 0; + let c = eff("e", 3) - x | 0; + eff("f", x); + let o = eff("g", x); + let o$1 = Primitive_option.some(f(Primitive_option.valFromOption(o))); + return [ + a$1, + b$2, + c, + g, + o$1, + log + ]; +} + +let I = { + genSomeAfter: genSomeAfter, + someNested: someNested, + someNullable: someNullable, + someUnboxed: someUnboxed, + someGen: someGen, + mapToOpt: mapToOpt, + someSome: someSome, + flatMapNested: flatMapNested, + effects: effects +}; + +function genSomeAfter$1(p, q, q2) { + let r = q; + return [ + Inline_twin_lib.afterGN(p, Primitive_option.some(r), () => { + r = q2; + }), + r + ]; +} + +function someNested$1(x) { + return [ + Inline_twin_lib.someN(x), + Inline_twin_lib.someN(x) !== undefined + ]; +} + +function someNullable$1(x) { + return [ + Inline_twin_lib.someN(x), + Inline_twin_lib.someN(x) !== undefined + ]; +} + +function someUnboxed$1(x) { + return [ + Inline_twin_lib.someN(x), + Inline_twin_lib.someN(x) !== undefined + ]; +} + +function someGen$1(x) { + return [ + Inline_twin_lib.someN(x), + Inline_twin_lib.someN(x) !== undefined + ]; +} + +function mapToOpt$1(x, f) { + return Inline_twin_lib.mapN(Primitive_option.some(x), f); +} + +function someSome$1(x) { + return Inline_twin_lib.getOrN(Inline_twin_lib.mapN(Inline_twin_lib.someN(Inline_twin_lib.someN(x)), v => v), undefined); +} + +function flatMapNested$1(x, f) { + return Inline_twin_lib.getOrN(Inline_twin_lib.flatMapN(Primitive_option.some(x), f), -1); +} + +function effects$1(x, f, g) { + let log = []; + let eff = (tag, v) => { + log.push(tag); + return v; + }; + let a = Inline_twin_lib.bothN(eff("a", x), eff("b", x + 1 | 0), f); + let b = Inline_twin_lib.subN(eff("c", x), eff("d", (x << 1))); + let c = Inline_twin_lib.subN(x, eff("e", 3)); + let d = Inline_twin_lib.sndN(eff("f", x), g); + let o = Inline_twin_lib.mapN(eff("g", x), f); + return [ + a, + b, + c, + d, + o, + log + ]; +} + +let N = { + genSomeAfter: genSomeAfter$1, + someNested: someNested$1, + someNullable: someNullable$1, + someUnboxed: someUnboxed$1, + someGen: someGen$1, + mapToOpt: mapToOpt$1, + someSome: someSome$1, + flatMapNested: flatMapNested$1, + effects: effects$1 +}; + +function twin(name, run) { + Mocha.test(name, () => Test_utils.eq(name, run(I), run(N))); +} + +Mocha.describe("Inline_twin_test", () => { + twin("genSomeAfter string", C => C.genSomeAfter(1, "a", "b")); + twin("genSomeAfter option", C => C.genSomeAfter(1, undefined, 2)); + twin("someNested None", C => C.someNested(undefined)); + twin("someNested Some", C => C.someNested(2)); + twin("someNullable null", C => C.someNullable(null)); + twin("someNullable undefined", C => C.someNullable(undefined)); + twin("someNullable value", C => C.someNullable(1)); + twin("someUnboxed U", C => C.someUnboxed(undefined)); + twin("someUnboxed I", C => C.someUnboxed(2)); + twin("someGen int", C => C.someGen(1)); + twin("someGen None", C => C.someGen(undefined)); + twin("someGen unit", C => C.someGen()); + twin("someGen Some(None)", C => C.someGen(Primitive_option.some(undefined))); + twin("mapToOpt int", C => C.mapToOpt(1, v => v)); + twin("mapToOpt None", C => C.mapToOpt(undefined, v => Primitive_option.some(v))); + twin("mapToOpt unit", C => C.mapToOpt(undefined, v => Primitive_option.some(v))); + twin("someSome None", C => C.someSome(undefined)); + twin("someSome int", C => C.someSome(3)); + twin("flatMapNested None", C => C.flatMapNested(undefined, v => v)); + twin("flatMapNested Some(None)", C => C.flatMapNested(Primitive_option.some(undefined), v => v)); + twin("flatMapNested Some", C => C.flatMapNested(2, v => v)); + twin("effects", C => C.effects(1, v => (v << 1), "g")); +}); + +let Lib; + +export { + Lib, + I, + N, + twin, +} +/* Not a pure module */ diff --git a/tests/compiler_tests/src/inline_twin_test.res b/tests/compiler_tests/src/inline_twin_test.res new file mode 100644 index 0000000000..f4390adbb6 --- /dev/null +++ b/tests/compiler_tests/src/inline_twin_test.res @@ -0,0 +1,150 @@ +// Differential test for cross-module inlining. Inline_twin_lib defines each +// helper twice with the same body: `xI` is @inline and `xN` @inline(never). +// Modules I and N below are the same code, except that I calls the `xI` +// helpers, which the alias pass inlines, and N calls the `xN` ones. Each test +// expects both to return the same, so a difference is a miscompile of the +// inlined copy. Edit a case in I, then copy it to N with the suffixes changed. +// Cases are @inline(never), so that the tests run them as compiled, and take +// their inputs as parameters, so that the optimizer can't fold them away. +open Mocha +open Test_utils + +module Lib = Inline_twin_lib + +@unboxed type u = | @as(undefined) U | I(int) + +module I = { + // 1. A ref of the case, read in an argument before a callback that the + // helper calls first assigns it. + + @inline(never) + let genSomeAfter = (p, q: 'a, q2: 'a) => { + let r = ref(q) + (Lib.afterGI(p, Some(r.contents), () => r := q2), r.contents) + } + + // 2. Options, at payload types that can be undefined and types that can't. + + @inline(never) + let someNested = (x: option) => (Lib.someI(x), Option.isSome(Lib.someI(x))) + + @inline(never) + let someNullable = (x: Nullable.t) => (Lib.someI(x), Option.isSome(Lib.someI(x))) + + @inline(never) + let someUnboxed = (x: u) => (Lib.someI(x), Option.isSome(Lib.someI(x))) + + @inline(never) + let someGen = (x: 'a) => (Lib.someI(x), Option.isSome(Lib.someI(x))) + + @inline(never) + let mapToOpt = (x: 'a, f: 'a => option<'a>) => Lib.mapI(Some(x), f) + + @inline(never) + let someSome = (x: 'a) => Lib.someI(Lib.someI(x))->Lib.mapI(v => v)->Lib.getOrI(None) + + @inline(never) + let flatMapNested = (x: option>, f) => Some(x)->Lib.flatMapI(f)->Lib.getOrI(Some(-1)) + + // 3. Arguments with side effects, evaluated once and left to right. + + @inline(never) + let effects = (x: int, f, g) => { + let log = [] + let eff = (tag, v) => { + log->Array.push(tag) + v + } + let a = Lib.bothI(eff("a", x), eff("b", x + 1), f) + let b = Lib.subI(eff("c", x), eff("d", 2 * x)) + let c = Lib.subI(x, eff("e", 3)) + let d = Lib.sndI(eff("f", x), g) + let o = Lib.mapI(Some(eff("g", x)), f) + (a, b, c, d, o, log) + } +} + +// The same as I, calling the @inline(never) twins. +module N = { + // 1. A ref of the case, read in an argument before a callback that the + // helper calls first assigns it. + + @inline(never) + let genSomeAfter = (p, q: 'a, q2: 'a) => { + let r = ref(q) + (Lib.afterGN(p, Some(r.contents), () => r := q2), r.contents) + } + + // 2. Options, at payload types that can be undefined and types that can't. + + @inline(never) + let someNested = (x: option) => (Lib.someN(x), Option.isSome(Lib.someN(x))) + + @inline(never) + let someNullable = (x: Nullable.t) => (Lib.someN(x), Option.isSome(Lib.someN(x))) + + @inline(never) + let someUnboxed = (x: u) => (Lib.someN(x), Option.isSome(Lib.someN(x))) + + @inline(never) + let someGen = (x: 'a) => (Lib.someN(x), Option.isSome(Lib.someN(x))) + + @inline(never) + let mapToOpt = (x: 'a, f: 'a => option<'a>) => Lib.mapN(Some(x), f) + + @inline(never) + let someSome = (x: 'a) => Lib.someN(Lib.someN(x))->Lib.mapN(v => v)->Lib.getOrN(None) + + @inline(never) + let flatMapNested = (x: option>, f) => Some(x)->Lib.flatMapN(f)->Lib.getOrN(Some(-1)) + + // 3. Arguments with side effects, evaluated once and left to right. + + @inline(never) + let effects = (x: int, f, g) => { + let log = [] + let eff = (tag, v) => { + log->Array.push(tag) + v + } + let a = Lib.bothN(eff("a", x), eff("b", x + 1), f) + let b = Lib.subN(eff("c", x), eff("d", 2 * x)) + let c = Lib.subN(x, eff("e", 3)) + let d = Lib.sndN(eff("f", x), g) + let o = Lib.mapN(Some(eff("g", x)), f) + (a, b, c, d, o, log) + } +} + +module type Cases = module type of I + +// Runs a case of I and the same case of N. +let twin = (name, run: module(Cases) => 'a) => + test(name, () => eq(name, run(module(I)), run(module(N)))) + +describe(__MODULE__, () => { + twin("genSomeAfter string", (module(C)) => C.genSomeAfter(1, "a", "b")) + twin("genSomeAfter option", (module(C)) => C.genSomeAfter(1, None, Some(2))) + + twin("someNested None", (module(C)) => C.someNested(None)) + twin("someNested Some", (module(C)) => C.someNested(Some(2))) + twin("someNullable null", (module(C)) => C.someNullable(Nullable.null)) + twin("someNullable undefined", (module(C)) => C.someNullable(Nullable.undefined)) + twin("someNullable value", (module(C)) => C.someNullable(Nullable.make(1))) + twin("someUnboxed U", (module(C)) => C.someUnboxed(U)) + twin("someUnboxed I", (module(C)) => C.someUnboxed(I(2))) + twin("someGen int", (module(C)) => C.someGen(1)) + twin("someGen None", (module(C)) => C.someGen(None)) + twin("someGen unit", (module(C)) => C.someGen()) + twin("someGen Some(None)", (module(C)) => C.someGen(Some(None))) + twin("mapToOpt int", (module(C)) => C.mapToOpt(1, v => Some(v))) + twin("mapToOpt None", (module(C)) => C.mapToOpt(None, v => Some(v))) + twin("mapToOpt unit", (module(C)) => C.mapToOpt((), v => Some(v))) + twin("someSome None", (module(C)) => C.someSome(None)) + twin("someSome int", (module(C)) => C.someSome(3)) + twin("flatMapNested None", (module(C)) => C.flatMapNested(None, v => v)) + twin("flatMapNested Some(None)", (module(C)) => C.flatMapNested(Some(None), v => v)) + twin("flatMapNested Some", (module(C)) => C.flatMapNested(Some(Some(2)), v => v)) + + twin("effects", (module(C)) => C.effects(1, v => v * 2, "g")) +})