Skip to content

The code generated from "let open Functor(Module) in ..." is buggy. #661

@kxc-wraikny

Description

@kxc-wraikny

Summary

OCaml source code:

let test3 () =
  let open Samplelib.MonadOps(Promise) in
  return 2
  >>= fun x -> return (x + 40)
  >>= fun x -> return (to_be (expect x) 42)

The generated JavaScript:

function test3(param) {
  var open = Samplelib.MonadOps($$Promise$1);
  var $great$great$eq = open[1];
  var $$return = open[0];
  return Curry._2($great$great$eq, Curry._1($$return, 2), (function (x) {
                return Curry._2($great$great$eq, Curry._1($$return, x + 40 | 0), (function (x) {
                              return Curry._1($$return, (expect(x).toBe(42), undefined));
                            }));
              }));
}

Test result:

FAIL test/samplelib.test.js
  ✓ it loads (2 ms)
  ✓ test1 (1 ms)
  ✓ test2
  ✕ test3 (1 ms)

  ● test3

    TypeError: Cannot read properties of undefined (reading 'length')

      43 |   var $great$great$eq = open[1];
      44 |   var $$return = open[0];
    > 45 |   return Curry._2($great$great$eq, Curry._1($$return, 2), (function (x) {
         |                                          ^
      46 |                 return Curry._2($great$great$eq, Curry._1($$return, x + 40 | 0), (function (x) {
      47 |                               return Curry._1($$return, (expect(x).toBe(42), undefined));
      48 |                             }));

      at Object._1 (_output/node_modules/melange.runtime/curry.js:31:17)
      at Object._1 [as test3] (_output/samplelib_test.js:45:42)
      at Object.test3 (test/samplelib.test.js:17:29)

This is a reproduction repository:
https://github.com/kxc-wraikny/Melange_bug

Details

The functor is defined as

module MonadOps(M : sig
  type _ t
  val return : 'x -> 'x t
  val bind : 'x t -> ('x -> 'y t) -> 'y t
end) = struct
  let return = M.return
  let (>>=) = M.bind
end

https://github.com/kxc-wraikny/Melange_bug/blob/7f3aa164d5b0553424fe54638878717249855eb9/samplelib.ml#L1-L8

and this is the test code and the problem occurs at test3

module Test = struct
  let test1 () =
    let open Promise in
    return 2
    |> flip bind (fun x -> return (x + 40))
    |> flip bind @@ fun x -> return (to_be (expect x) 42)
    
  let test2 () =
    let module M = Samplelib.MonadOps(Promise) in
    let open M in
    return 2
    >>= fun x -> return (x + 40)
    >>= fun x -> return (to_be (expect x) 42)

  let test3 () =
    let open Samplelib.MonadOps(Promise) in
    return 2
    >>= fun x -> return (x + 40)
    >>= fun x -> return (to_be (expect x) 42)
end

https://github.com/kxc-wraikny/Melange_bug/blob/7f3aa164d5b0553424fe54638878717249855eb9/samplelib_test.ml#L19-L38

The generated code:

function test1(param) {
  var m = Promise.resolve(2);
  var m$1 = m.then(function (x) {
        return Promise.resolve(x + 40 | 0);
      });
  return m$1.then(function (x) {
              return Promise.resolve((expect(x).toBe(42), undefined));
            });
}

function test2(param) {
  var M = Samplelib.MonadOps($$Promise$1);
  return Curry._2(M.$great$great$eq, Curry._1(M.$$return, 2), (function (x) {
                return Curry._2(M.$great$great$eq, Curry._1(M.$$return, x + 40 | 0), (function (x) {
                              return Curry._1(M.$$return, (expect(x).toBe(42), undefined));
                            }));
              }));
}

function test3(param) {
  var open = Samplelib.MonadOps($$Promise$1);
  var $great$great$eq = open[1];
  var $$return = open[0];
  return Curry._2($great$great$eq, Curry._1($$return, 2), (function (x) {
                return Curry._2($great$great$eq, Curry._1($$return, x + 40 | 0), (function (x) {
                              return Curry._1($$return, (expect(x).toBe(42), undefined));
                            }));
              }));
}

https://github.com/kxc-wraikny/Melange_bug/blob/7f3aa164d5b0553424fe54638878717249855eb9/_output/samplelib_test.js#L22-L50

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions