From d50face7714fb7734f8d32dcc92c16df6adf4d90 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Thu, 19 Nov 2026 19:17:58 +0000 Subject: [PATCH] Evaluate labelled arguments passed out of order in source order Why: An application lists its arguments in declared order. A labelled argument passed before arguments declared ahead of it was therefore evaluated after them, against the left-to-right order ReScript keeps elsewhere (#9340, #9154). An argument is stable when it reads the same wherever it is evaluated: a constant, variable, function, constant constructor or module path. When the arguments that are not stable are out of source order and one of them can have side effects, the callee (unless it is an identifier) and those arguments are bound to temporaries in source order. A stable argument passed early, as in `fromArray(~id=module(IntCmp), range(0, 30))`, changes nothing. JS modified (out-of-order labelled arguments): Example: let f = (x: int, ~extra: int, y: int) => [x, extra, y] let r = f(~extra=b(), a(), c()) Before (js): let r = [ a(), b(), c() ]; After (js): let arg = b(); let arg$1 = a(); let arg$2 = c(); let r = [ arg$1, arg, arg$2 ]; Review: Why bind every argument that is not stable, not only the moved ones? An argument left in place runs after all bound ones, so it would have to come after them in the source as well. Binding all of them is simpler and only happens for out-of-order calls with effects. Fixes #9340 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gHt1hL7PvCPo3r2viL Signed-off-by: Christoph Knittel --- changes/labelled-argument-order.fix.md | 1 + compiler/typing/translcore.ml | 110 ++++++++++++ .../src/labelled_argument_order_test.mjs | 157 ++++++++++++++++++ .../src/labelled_argument_order_test.res | 79 +++++++++ 4 files changed, 347 insertions(+) create mode 100644 changes/labelled-argument-order.fix.md create mode 100644 tests/tests/src/labelled_argument_order_test.mjs create mode 100644 tests/tests/src/labelled_argument_order_test.res diff --git a/changes/labelled-argument-order.fix.md b/changes/labelled-argument-order.fix.md new file mode 100644 index 0000000000..2d8c66c43a --- /dev/null +++ b/changes/labelled-argument-order.fix.md @@ -0,0 +1 @@ +- Labelled arguments passed before arguments declared ahead of them are evaluated in source order. diff --git a/compiler/typing/translcore.ml b/compiler/typing/translcore.ml index 79e321ff3a..866fe311a1 100644 --- a/compiler/typing/translcore.ml +++ b/compiler/typing/translcore.ml @@ -1187,6 +1187,54 @@ let check_callee_attributes (funct : expression) = (fun () -> List.iter (Translattribute.check_attribute funct) funct.exp_attributes) +(* Whether an argument reads the same wherever it is evaluated, so it can stay + in place when the arguments around it move. *) +let rec is_module_path (m : Typedtree.module_expr) = + match m.mod_desc with + | Tmod_ident _ -> true + | Tmod_constraint (m, _, _, _) -> is_module_path m + | _ -> false + +let is_stable_arg (e : Typedtree.expression) = + match e.exp_desc with + | Texp_constant _ + | Texp_ident (_, _, {val_kind = Val_reg}) + | Texp_function _ + | Texp_construct (_, _, []) + | Texp_variant (_, None) -> + true + | Texp_pack m -> is_module_path m + | _ -> false + +(* A labelled argument can be passed before arguments declared ahead of it, as + in [f(~b=g(), x)], but an application lists its arguments in declared + order. When that changes the order of the arguments that are not stable, + and one of them can have side effects, those arguments have to be + evaluated in source order first (#9340). A stable argument reads the same + wherever it is evaluated, and without effects the order makes no + difference. *) +let args_out_of_source_order + (args : (Asttypes.arg_label * Typedtree.expression option) list) = + let rec sorted = function + | a :: (b :: _ as rest) -> a <= b && sorted rest + | _ -> true + in + let positions = + List.filter_map + (function + | _, Some (e : Typedtree.expression) + when (not e.exp_loc.loc_ghost) && not (is_stable_arg e) -> + Some e.exp_loc.loc_start.pos_cnum + | _ -> None) + args + in + (not (sorted positions)) + && List.exists + (function + | _, Some e -> not (Typecore.is_nonexpansive e) + | _, None -> false) + args + let rec transl_exp e = Builtin_attributes.warning_scope ~ppwarning:false e.exp_attributes (fun () -> List.iter (Translattribute.check_attribute e) e.exp_attributes; @@ -1231,6 +1279,9 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.t = e.exp_loc | Texp_template {segments; values} -> prim ~primitive:(Ptemplate segments) ~args:(transl_list values) e.exp_loc + | Texp_apply {funct; args; partial; transformed_jsx = false} + when args_out_of_source_order args -> + transl_apply_in_source_order e funct args ~partial | Texp_apply ({ funct = @@ -1571,6 +1622,65 @@ and transl_case {c_lhs; c_guard; c_rhs} = (c_lhs, transl_guard c_guard c_rhs) and transl_cases cases = List.map transl_case cases +(* Binds the callee, unless it is an identifier, and then every argument that + is not stable to a temporary, in source order, and translates the + application with the temporaries in their place. *) +and transl_apply_in_source_order (e : Typedtree.expression) funct args ~partial + = + let defs = ref [] in + let bind (arg : Typedtree.expression) : Typedtree.expression = + let id = Ident.create "arg" in + defs := (id, transl_exp arg) :: !defs; + { + arg with + exp_desc = + Texp_ident + ( Path.Pident id, + mknoloc (Longident.Lident (Ident.name id)), + { + val_type = arg.exp_type; + val_kind = Val_reg; + val_loc = arg.exp_loc; + val_attributes = []; + } ); + exp_extra = []; + exp_attributes = []; + } + in + let funct = + match funct.exp_desc with + | Texp_ident _ -> funct + | _ -> bind funct + in + let to_bind = + List.filter_map + (function + | _, Some (arg : Typedtree.expression) when not (is_stable_arg arg) -> + Some arg + | _ -> None) + args + |> List.stable_sort (fun (a : Typedtree.expression) b -> + compare a.exp_loc.loc_start.pos_cnum b.exp_loc.loc_start.pos_cnum) + in + let bound = List.map (fun arg -> (arg, bind arg)) to_bind in + let args = + List.map + (fun (l, arg) -> + match arg with + | Some arg when not (is_stable_arg arg) -> + (l, Some (List.assq arg bound)) + | _ -> (l, arg)) + args + in + let body = + transl_exp0 + { + e with + exp_desc = Texp_apply {funct; args; partial; transformed_jsx = false}; + } + in + List.fold_left (fun body (id, lam) -> let_ Strict id lam body) body !defs + and transl_apply ?(uncurried_partial_application = None) lam sargs loc = let lapply ap_func ap_args = apply ap_func ap_args loc in (* A partial application creates a function. Its callee and arguments are diff --git a/tests/tests/src/labelled_argument_order_test.mjs b/tests/tests/src/labelled_argument_order_test.mjs new file mode 100644 index 0000000000..2fd89f588e --- /dev/null +++ b/tests/tests/src/labelled_argument_order_test.mjs @@ -0,0 +1,157 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Mocha from "mocha"; +import * as Test_utils from "./test_utils.mjs"; +import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.mjs"; + +let target = ({send: (...xs) => xs}); + +function f(x, extra, y) { + return [ + x, + extra, + y + ]; +} + +function withOpt(x, extra, y) { + return [ + x, + Stdlib_Option.getOr(extra, 0), + y + ]; +} + +Mocha.describe("Labelled_argument_order_test", () => { + let log = []; + let mark = (name, v) => { + log.push(name); + return v; + }; + let take = () => { + let names = log.join(""); + log.splice(0, log.length); + return names; + }; + Mocha.test("a function", () => { + let arg = mark("b", 2); + let arg$1 = mark("a", 1); + let arg$2 = mark("c", 3); + let r = [ + arg$1, + arg, + arg$2 + ]; + Test_utils.eq("File \"labelled_argument_order_test.res\", line 30, characters 7-14", "bac", take()); + Test_utils.eq("File \"labelled_argument_order_test.res\", line 31, characters 7-14", [ + 1, + 2, + 3 + ], r); + }); + Mocha.test("a @send external", () => { + let arg = mark("b", 2); + let arg$1 = mark("a", 1); + let arg$2 = mark("c", 3); + let r = target.send(arg$1, arg, arg$2); + Test_utils.eq("File \"labelled_argument_order_test.res\", line 36, characters 7-14", "bac", take()); + Test_utils.eq("File \"labelled_argument_order_test.res\", line 37, characters 7-14", [ + 1, + 2, + 3 + ], r); + }); + Mocha.test("an @obj external", () => { + let arg = mark("y", 2); + let arg$1 = mark("x", 1); + let o = { + x: arg$1, + y: arg + }; + Test_utils.eq("File \"labelled_argument_order_test.res\", line 42, characters 7-14", "yx", take()); + Test_utils.eq("File \"labelled_argument_order_test.res\", line 43, characters 7-14", [ + 1, + 2 + ], [ + o.x, + o.y + ]); + }); + Mocha.test("a callee that is a call", () => { + let arg = mark("f", f); + let arg$1 = mark("b", 2); + let arg$2 = mark("a", 1); + let arg$3 = mark("c", 3); + let r = arg(arg$2, arg$1, arg$3); + Test_utils.eq("File \"labelled_argument_order_test.res\", line 49, characters 7-14", "fbac", take()); + Test_utils.eq("File \"labelled_argument_order_test.res\", line 50, characters 7-14", [ + 1, + 2, + 3 + ], r); + }); + Mocha.test("a read after an effect that changes it", () => { + let n = 0; + let bump = () => { + n = n + 1 | 0; + return 10; + }; + let arg = bump(); + let arg$1 = n; + Test_utils.eq("File \"labelled_argument_order_test.res\", line 59, characters 7-14", [ + 1, + 10, + 5 + ], [ + arg$1, + arg, + 5 + ]); + }); + Mocha.test("an optional argument", () => { + let arg = mark("b", 2); + let arg$1 = mark("a", 1); + let arg$2 = mark("c", 3); + let r = withOpt(arg$1, arg, arg$2); + Test_utils.eq("File \"labelled_argument_order_test.res\", line 64, characters 7-14", "bac", take()); + Test_utils.eq("File \"labelled_argument_order_test.res\", line 65, characters 7-14", [ + 1, + 2, + 3 + ], r); + }); + Mocha.test("a partial application", () => { + let arg = mark("b", 2); + let arg$1 = mark("a", 1); + Test_utils.eq("File \"labelled_argument_order_test.res\", line 70, characters 7-14", "ba", take()); + Test_utils.eq("File \"labelled_argument_order_test.res\", line 71, characters 7-14", [ + 1, + 2, + 3 + ], [ + arg$1, + arg, + 3 + ]); + }); + Mocha.test("arguments already in order", () => { + let r = [ + mark("a", 1), + mark("b", 2), + mark("c", 3) + ]; + Test_utils.eq("File \"labelled_argument_order_test.res\", line 76, characters 7-14", "abc", take()); + Test_utils.eq("File \"labelled_argument_order_test.res\", line 77, characters 7-14", [ + 1, + 2, + 3 + ], r); + }); +}); + +export { + target, + f, + withOpt, +} +/* target Not a pure module */ diff --git a/tests/tests/src/labelled_argument_order_test.res b/tests/tests/src/labelled_argument_order_test.res new file mode 100644 index 0000000000..a9f09533f8 --- /dev/null +++ b/tests/tests/src/labelled_argument_order_test.res @@ -0,0 +1,79 @@ +open Mocha +open Test_utils + +// Labelled arguments passed before arguments declared ahead of them are still +// evaluated in source order. + +type t +@send external send: (t, int, ~extra: int, int) => array = "send" +@obj external makeObj: (~x: int, ~y: int) => {"x": int, "y": int} = "" + +let target: t = %raw(`{send: (...xs) => xs}`) + +let f = (x: int, ~extra: int, y: int) => [x, extra, y] +let withOpt = (x: int, ~extra: option=?, y: int) => [x, extra->Option.getOr(0), y] + +describe(__MODULE__, () => { + let log = [] + let mark = (name, v) => { + log->Array.push(name) + v + } + let take = () => { + let names = log->Array.join("") + log->Array.splice(~start=0, ~remove=log->Array.length, ~insert=[]) + names + } + + test("a function", () => { + let r = f(~extra=mark("b", 2), mark("a", 1), mark("c", 3)) + eq(__LOC__, "bac", take()) + eq(__LOC__, [1, 2, 3], r) + }) + + test("a @send external", () => { + let r = send(target, ~extra=mark("b", 2), mark("a", 1), mark("c", 3)) + eq(__LOC__, "bac", take()) + eq(__LOC__, [1, 2, 3], r) + }) + + test("an @obj external", () => { + let o = makeObj(~y=mark("y", 2), ~x=mark("x", 1)) + eq(__LOC__, "yx", take()) + eq(__LOC__, (1, 2), (o["x"], o["y"])) + }) + + test("a callee that is a call", () => { + let getF = () => mark("f", f) + let r = getF()(~extra=mark("b", 2), mark("a", 1), mark("c", 3)) + eq(__LOC__, "fbac", take()) + eq(__LOC__, [1, 2, 3], r) + }) + + test("a read after an effect that changes it", () => { + let n = ref(0) + let bump = () => { + n := n.contents + 1 + 10 + } + eq(__LOC__, [1, 10, 5], f(~extra=bump(), n.contents, 5)) + }) + + test("an optional argument", () => { + let r = withOpt(~extra=?mark("b", Some(2)), mark("a", 1), mark("c", 3)) + eq(__LOC__, "bac", take()) + eq(__LOC__, [1, 2, 3], r) + }) + + test("a partial application", () => { + let g = f(~extra=mark("b", 2), mark("a", 1), ...) + eq(__LOC__, "ba", take()) + eq(__LOC__, [1, 2, 3], g(3)) + }) + + test("arguments already in order", () => { + let r = f(mark("a", 1), ~extra=mark("b", 2), mark("c", 3)) + eq(__LOC__, "abc", take()) + eq(__LOC__, [1, 2, 3], r) + }) +})