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
2 changes: 2 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Unreleased
- melange.ppx: improve the inference of `@mel.as` in polymorphic variant
arguments to `external`s, allow to mix ints and strings in the different
variant branches ([#1418](https://github.com/melange-re/melange/pull/1418))
- Fix code generation bug when assigning functions to variables
([#1429](https://github.com/melange-re/melange/pull/1429))

5.1.0-53 2025-03-23
---------------
Expand Down
21 changes: 12 additions & 9 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ let is_var (b : J.expression) a =

type fn_exp_state =
| Is_return (* for sure no name *)
| Name_top of Ident.t
| Name_non_top of Ident.t
| Name_top of { name : Ident.t; property : Lam_group.let_kind }
| Name_non_top of { name : Ident.t; property : Lam_group.let_kind }
| No_name of { single_arg : bool }
(* true means for sure, false -- not sure *)

Expand Down Expand Up @@ -401,8 +401,8 @@ and pp_function ~return_unit ~is_method cxt ~fn_state (l : Ident.t list)
let len = List.length l in
(* length *)
match fn_state with
| Name_top i | Name_non_top i ->
let cxt = pp_const_assign cxt i in
| Name_top { name = i; property } | Name_non_top { name = i; property } ->
let cxt = pp_assign ~property cxt i in
let cxt = optimize len ~p:(arity = NA && len <= 8) cxt v in
semi cxt;
cxt
Expand All @@ -414,7 +414,8 @@ and pp_function ~return_unit ~is_method cxt ~fn_state (l : Ident.t list)
(* identifiers will be printed following*)
match fn_state with
| Is_return | No_name _ -> Js_fun_env.get_unbounded env
| Name_top id | Name_non_top id ->
| Name_top { name = id; property = _ }
| Name_non_top { name = id; property = _ } ->
Ident.Set.add (Js_fun_env.get_unbounded env) id
in
(* the context will be continued after this function *)
Expand Down Expand Up @@ -462,13 +463,13 @@ and pp_function ~return_unit ~is_method cxt ~fn_state (l : Ident.t list)
string cxt L.function_;
space cxt;
param_body ())
| Name_non_top x ->
ignore (pp_const_assign inner_cxt x : cxt);
| Name_non_top { name = x; property } ->
ignore (pp_assign ~property inner_cxt x : cxt);
string cxt L.function_;
space cxt;
param_body ();
semi cxt
| Name_top x ->
| Name_top { name = x; property = _ } ->
string cxt L.function_;
space cxt;
ignore (ident inner_cxt x : cxt);
Expand Down Expand Up @@ -961,7 +962,9 @@ and variable_declaration top cxt (variable : J.variable_declaration) : cxt =
match e.expression_desc with
| Fun { method_ = is_method; params; body = b; env; return_unit } ->
pp_function ~return_unit ~is_method cxt
~fn_state:(if top then Name_top name else Name_non_top name)
~fn_state:
(if top then Name_top { name; property }
else Name_non_top { name; property })
params b env
| _ ->
let cxt = pp_assign ~property cxt name in
Expand Down
6 changes: 3 additions & 3 deletions jscomp/test/dist/jscomp/test/bench.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions jscomp/test/dist/jscomp/test/earger_curry_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions jscomp/test/dist/jscomp/test/ocaml_parsetree_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jscomp/test/dist/jscomp/test/ocaml_re_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions jscomp/test/dist/jscomp/test/ocaml_typedtree_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jscomp/test/dist/jscomp/test/qcc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jscomp/test/dist/jscomp/test/sexpm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions test/blackbox-tests/function-assignment-const.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

$ . ./setup.sh
$ cat >dune-project <<EOF
> (lang dune 3.8)
> (using melange 0.1)
> EOF

$ cat > main.ml <<EOF
> let f x =
> let rec aux x k =
> match x with
> | 0 -> k 0
> | x -> aux (x - 1) (fun x -> x)
> in
> aux x (fun x -> x)
> let _: int = f 3
> EOF

$ melc main.ml | tee main.js
// Generated by Melange
'use strict';

const Curry = require("melange.js/curry.js");

function f(x) {
let _x = x;
let _k = function (x) {
return x;
};
while (true) {
const k = _k;
const x$1 = _x;
if (x$1 === 0) {
return Curry._1(k, 0);
}
_k = (function (x) {
return x;
});
_x = x$1 - 1 | 0;
continue;
};
}

f(3);

module.exports = {
f,
}
/* Not a pure module */

Loading