Skip to content
Draft
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
1 change: 1 addition & 0 deletions changes/labelled-argument-order.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Labelled arguments passed before arguments declared ahead of them are evaluated in source order.
110 changes: 110 additions & 0 deletions compiler/typing/translcore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Comment thread
cknitt marked this conversation as resolved.
Resolved
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
Expand Down
157 changes: 157 additions & 0 deletions tests/tests/src/labelled_argument_order_test.mjs
Original file line number Diff line number Diff line change
@@ -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 */
79 changes: 79 additions & 0 deletions tests/tests/src/labelled_argument_order_test.res
Original file line number Diff line number Diff line change
@@ -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<int> = "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<int>=?, 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)
})
})
Loading