Skip to content

Commit f88294f

Browse files
cknittclaude
andauthored
Evaluate arguments left to right when a later one compiles to statements (#9159)
Why: The statements of an argument were emitted before the whole call or literal, so they ran before the arguments to their left, which could read values the statements had already changed (#9154). JS modified (argument order): Example: let count = ref(0) let pair = (count.contents, { count := count.contents + 1 let id = count.contents `item-${Int.toString(id)}` }) Before (js): pair is [1, "item-1"] count.contents = count.contents + 1 | 0; let id = count.contents; let pair_0 = count.contents; let pair_1 = `item-${id.toString()}`; After (js): pair is [0, "item-1"] let tmp = count.contents; count.contents = count.contents + 1 | 0; let id = count.contents; let pair_1 = `item-${id.toString()}`; JS modified (called fields): Example: let r = o.f({ o.f = x => x + 100 let y = Int.fromString("1")->Option.getOrThrow y * 2 }) Before (js): the new function was called o.f = x => x + 100 | 0; let y = Stdlib_Option.getOrThrow(Stdlib_Int.fromString("1"), undefined); let r = o.f((y << 1)); After (js): the function is read before the arguments let tmp = o.f; o.f = x => x + 100 | 0; let y = Stdlib_Option.getOrThrow(Stdlib_Int.fromString("1"), undefined); let r = tmp((y << 1)); Review: Which arguments get a temporary? Only those to the left of an argument with statements, and only when delaying them could change their value. Constants, functions, immutable variables and literals of such values stay in place. Reads of fields, JS globals and assignable variables are held: the statements, or a closure they call, may change them. A literal keeps its shape and only its elements are held, so translations that read their arguments, such as dict rows and @variadic arrays, still see it. Review: How are assignable variables known? They are the targets of `Lassign` in the final Lambda (`Lambda_traverse.assigned_variables`), collected once per module into `Lam_stats.assignable`, and for a body inlined from another module while generating JS. Review: What about method calls? The callee is held like any argument, except a JS method read (`Pjs_object_get`): there the object is held and the property read stays in the call, so the object is still `this`. Review: Which other constructs change? JS object writes (`obj["k"] = v`) go through the same path, so the target is evaluated before the value's statements. The start of a `for` loop was moved after the statements of its end whenever it had no side effects, which included field reads; it now uses the same check. Review: Does the output change elsewhere? Only where the order was wrong: in the test corpus, one call in bs_array_test.mjs, whose first argument `Belt_Array.reverse(x)` now runs before the copy in the second. A field read such as `init.x` in `{...init, y: {block}}` still gets a temporary, since fields are never delayed. Fixes: #9154 Claude-Session: https://claude.ai/code/session_01Kbf3gJs1wimqEvBr89ZYbb Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 995be5c commit f88294f

10 files changed

Lines changed: 790 additions & 64 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Evaluate arguments and literal elements left to right when a later one is a block that compiles to statements, which could produce wrong values.

‎compiler/core/lam_compile.ml‎

Lines changed: 173 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,135 @@ let functions_before_values (group : (Ident.t * Lambda.t) list) =
238238
| _, _ -> 0)
239239
group
240240

241+
(* Whether evaluating [e] later, after other code, gives the same value and
242+
the same effects as evaluating it now: constants, functions, immutable
243+
local variables and pure operations on them. [assignable] are the local
244+
variables some [Lassign] assigns; the other code, or a closure it calls,
245+
may assign them. JS globals ([@val]) and reads of the heap (fields, array
246+
elements, lengths) may change as well. *)
247+
let rec delay_safe ~assignable (e : J.expression) =
248+
match e.expression_desc with
249+
| Undefined _ | Null | Bool _ | Number _ | Str _ | Template_literal _
250+
| Json_literal _ | Fun _
251+
| Var (Qualified _) ->
252+
true
253+
| Var (Id id) ->
254+
(not (Ext_ident.is_js id)) && not (Set_ident.mem id assignable)
255+
| Bin (Eq, _, _) -> false
256+
| Bin (_, a, b) ->
257+
Js_analyzer.no_side_effect_expression e
258+
&& delay_safe ~assignable a && delay_safe ~assignable b
259+
| String_append (a, b) -> delay_safe ~assignable a && delay_safe ~assignable b
260+
| Js_not a
261+
| Js_bnot a
262+
| Typeof a
263+
| Is_null_or_undefined a
264+
| Optional_block (a, _) ->
265+
delay_safe ~assignable a
266+
| Cond (a, b, c) ->
267+
delay_safe ~assignable a && delay_safe ~assignable b
268+
&& delay_safe ~assignable c
269+
| Interpolated_template {values} ->
270+
Ext_list.for_all values (delay_safe ~assignable)
271+
| Array xs | Caml_block (xs, _, _) ->
272+
Ext_list.for_all xs (delay_safe ~assignable)
273+
| Object entries ->
274+
Ext_list.for_all entries (function
275+
| J.Object_property (_, v) -> delay_safe ~assignable v
276+
| Record_spread _ | Object_spread _ -> false)
277+
| _ -> false
278+
279+
(* Statements that evaluate [e] now, and the expression that stands for its
280+
value afterwards. A sequence's leading expressions become statements of
281+
their own, and the last one gets a temporary unless it is [delay_safe].
282+
Building an array, block or object literal is pure, so a literal stays in
283+
place and its elements are evaluated now instead. That also keeps the
284+
literal visible to translations that look into their arguments, such as
285+
the rows of a dict literal or a [@variadic] array. *)
286+
let rec evaluate_now ~assignable (e : J.expression) : J.block * J.expression =
287+
match e.expression_desc with
288+
| _ when delay_safe ~assignable e -> ([], e)
289+
| Seq (a, b) ->
290+
let rest, b = evaluate_now ~assignable b in
291+
if Js_analyzer.no_side_effect_expression a then (rest, b)
292+
else
293+
({J.statement_desc = Exp a; comment = None; source_loc = None} :: rest, b)
294+
| Array xs ->
295+
let held, xs = evaluate_all_now ~assignable xs in
296+
(held, {e with expression_desc = Array xs})
297+
| Caml_block (xs, mutable_flag, tag) ->
298+
let held, xs = evaluate_all_now ~assignable xs in
299+
(held, {e with expression_desc = Caml_block (xs, mutable_flag, tag)})
300+
| Object entries
301+
when Ext_list.for_all entries (function
302+
| J.Object_property _ -> true
303+
| Record_spread _ | Object_spread _ -> false) ->
304+
let held, values =
305+
evaluate_all_now ~assignable
306+
(Ext_list.filter_map entries (function
307+
| J.Object_property (_, v) -> Some v
308+
| Record_spread _ | Object_spread _ -> None))
309+
in
310+
let entries =
311+
List.map2
312+
(fun entry v ->
313+
match entry with
314+
| J.Object_property (k, _) -> J.Object_property (k, v)
315+
| Record_spread _ | Object_spread _ -> assert false)
316+
entries values
317+
in
318+
(held, {e with expression_desc = Object entries})
319+
| _ ->
320+
let tmp = Ext_ident.create_tmp () in
321+
([S.define_variable ~kind:Strict tmp e], E.var tmp)
322+
323+
(* [evaluate_now] on each of [xs], left to right. The elements left in place
324+
are [delay_safe], so no other element's statements can change them. *)
325+
and evaluate_all_now ~assignable (xs : J.expression list) :
326+
J.block * J.expression list =
327+
let blocks, xs = Ext_list.split_map xs (evaluate_now ~assignable) in
328+
(List.concat blocks, xs)
329+
330+
(* Assembles compiled arguments for a call or a literal that evaluates them
331+
left to right. Each argument comes with the statements that have to run
332+
before its value is read. Putting all statements first and then the
333+
values would move the statements of an argument in front of the values of
334+
the arguments to its left, so when an argument has statements, every
335+
argument to its left is evaluated before them with [evaluate_now].
336+
337+
With [~method_callee:true], the first argument is a JS method read
338+
[obj.m] being called: the object is held and the property read stays in
339+
the call, so the call still passes the object as [this]. *)
340+
let sequence_arguments ?(method_callee = false) ~assignable
341+
(args : (J.block * J.expression) list) : J.block * J.expression list =
342+
let later_statements =
343+
match args with
344+
| [] -> false
345+
| _ :: rest -> Ext_list.exists_fst rest (fun block -> block <> [])
346+
in
347+
if not later_statements then (List.concat_map fst args, Ext_list.map args snd)
348+
else
349+
let last = List.length args - 1 in
350+
let _, code, values =
351+
Ext_list.fold_right args (last, [], [])
352+
(fun (block, value) (i, code, values) ->
353+
let held, value =
354+
if code = [] then ([], value)
355+
else
356+
match value.expression_desc with
357+
| Static_index (obj, name, pos) when method_callee && i = 0 ->
358+
let held, obj = evaluate_now ~assignable obj in
359+
( held,
360+
{value with expression_desc = Static_index (obj, name, pos)}
361+
)
362+
| _ -> evaluate_now ~assignable value
363+
in
364+
( i - 1,
365+
Ext_list.append block (Ext_list.append held code),
366+
value :: values ))
367+
in
368+
(code, values)
369+
241370
let compile output_prefix =
242371
(* When compiling a read from another module, a nested source path like
243372
Other.A.B.make reaches this point as nested module-field reads:
@@ -320,21 +449,23 @@ let compile output_prefix =
320449
let _, param_map =
321450
Lam_closure.is_closed_with_map Set_ident.empty params body
322451
in
323-
compile_lambda lambda_cxt
324-
(Lam_beta_reduce.propagate_beta_reduce_with_map lambda_cxt.meta
325-
param_map params body ap_args)
326-
| _ ->
327-
let args_code, args =
328-
let dummy = ([], []) in
329-
if ap_args = [] then dummy
330-
else
331-
let arg_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in
332-
Ext_list.fold_right ap_args dummy (fun arg_lambda (args_code, args) ->
333-
match compile_lambda arg_cxt arg_lambda with
334-
| {block; value = Some b} ->
335-
(Ext_list.append block args_code, b :: args)
336-
| _ -> assert false)
452+
let inlined =
453+
Lam_beta_reduce.propagate_beta_reduce_with_map lambda_cxt.meta param_map
454+
params body ap_args
337455
in
456+
(* The inlined body comes from another module, so the variables it
457+
assigns are not in [meta.assignable] yet. *)
458+
let meta =
459+
{
460+
lambda_cxt.meta with
461+
assignable =
462+
Set_ident.union lambda_cxt.meta.assignable
463+
(Lambda_traverse.assigned_variables inlined);
464+
}
465+
in
466+
compile_lambda {lambda_cxt with meta} inlined
467+
| _ ->
468+
let args_code, args = compile_arguments lambda_cxt ap_args in
338469

339470
let fn = E.ml_var_dot module_id ident_info.name in
340471
let expression =
@@ -1256,7 +1387,7 @@ let compile output_prefix =
12561387
match (b1, b2) with
12571388
| _, [] -> Ext_list.append_one b1 (make_for (Some e1))
12581389
| _, _
1259-
when Js_analyzer.no_side_effect_expression e1
1390+
when delay_safe ~assignable:lambda_cxt.meta.assignable e1
12601391
(*
12611392
e1 > b2 > e2
12621393
re-order
@@ -1563,15 +1694,15 @@ let compile output_prefix =
15631694
2. no need create names
15641695
*)
15651696
let ap_func = appinfo.ap_func in
1566-
let new_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in
15671697
let args_code, fn_code, args =
15681698
match
1569-
Ext_list.fold_right (ap_func :: appinfo.ap_args) ([], [])
1570-
(fun x (args_code, fn_code) ->
1571-
match compile_lambda new_cxt x with
1572-
| {block; value = Some b} ->
1573-
(Ext_list.append block args_code, b :: fn_code)
1574-
| {value = None} -> assert false)
1699+
compile_arguments
1700+
~method_callee:
1701+
(match ap_func with
1702+
| Lprim {primitive = Pjs_object_get _} -> true
1703+
| _ -> false)
1704+
lambda_cxt
1705+
(ap_func :: appinfo.ap_args)
15751706
with
15761707
| args_code, fn_code :: args -> (args_code, fn_code, args)
15771708
| _, [] -> assert false
@@ -1631,19 +1762,22 @@ let compile output_prefix =
16311762
(E.call
16321763
~info:(call_info_of_apply lambda_cxt.meta appinfo)
16331764
fn_code args)))
1765+
(* Compiles the arguments of a call or a literal, which are evaluated left to
1766+
right; see [sequence_arguments]. *)
1767+
and compile_arguments ?method_callee (lambda_cxt : Lam_compile_context.t)
1768+
(args : Lambda.t list) : J.block * J.expression list =
1769+
if args = [] then ([], [])
1770+
else
1771+
let arg_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in
1772+
sequence_arguments ?method_callee ~assignable:lambda_cxt.meta.assignable
1773+
(Ext_list.map args (fun arg ->
1774+
match compile_lambda arg_cxt arg with
1775+
| {block; value = Some value} -> (block, value)
1776+
| {value = None} -> assert false))
16341777
and compile_prim (prim_info : Lambda.prim_info)
16351778
(lambda_cxt : Lam_compile_context.t) =
16361779
let compile_primitive_default primitive args loc =
1637-
let args_block, args_expr =
1638-
if args = [] then ([], [])
1639-
else
1640-
let new_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in
1641-
Ext_list.split_map args (fun x ->
1642-
match compile_lambda new_cxt x with
1643-
| {block; value = Some b} -> (block, b)
1644-
| {value = None} -> assert false)
1645-
in
1646-
let args_code : J.block = List.concat args_block in
1780+
let args_code, args_expr = compile_arguments lambda_cxt args in
16471781
let exp =
16481782
(* TODO: all can be done in [Lam_compile_primitive.translate] *)
16491783
Lam_compile_primitive.translate output_prefix loc lambda_cxt primitive
@@ -1725,28 +1859,12 @@ let compile output_prefix =
17251859
ret)
17261860
| {primitive = Pjs_object_set property; args = [obj; setter_val]} -> (
17271861
(* property write: obj["height"] = v *)
1728-
let need_value_no_return_cxt =
1729-
{lambda_cxt with continuation = NeedValue Not_tail}
1730-
in
1731-
let obj_output = compile_lambda need_value_no_return_cxt obj in
1732-
let arg_output = compile_lambda need_value_no_return_cxt setter_val in
1733-
let cont obj_block arg_block obj_code =
1862+
match compile_arguments lambda_cxt [obj; setter_val] with
1863+
| args_code, [obj; value] ->
17341864
Js_output.output_of_block_and_expression lambda_cxt.continuation
1735-
(match obj_code with
1736-
| None -> Ext_list.append obj_block arg_block
1737-
| Some obj_code -> Ext_list.append obj_block (obj_code :: arg_block))
1738-
in
1739-
match (obj_output, arg_output) with
1740-
| {value = None}, _ | _, {value = None} -> assert false
1741-
| ( {block = obj_block; value = Some obj},
1742-
{block = arg_block; value = Some value} ) -> (
1743-
match Js_ast_util.named_expression obj with
1744-
| None ->
1745-
cont obj_block arg_block None
1746-
(E.seq (E.assign (E.dot obj property) value) E.unit)
1747-
| Some (obj_code, obj) ->
1748-
cont obj_block arg_block (Some obj_code)
1749-
(E.seq (E.assign (E.dot (E.var obj) property) value) E.unit)))
1865+
args_code
1866+
(E.seq (E.assign (E.dot obj property) value) E.unit)
1867+
| _ -> assert false)
17501868
| {primitive = Pjs_object_get _ | Pjs_object_set _} -> assert false
17511869
| {primitive = Pjs_fn_method; args = args_lambda} -> (
17521870
match args_lambda with
@@ -1771,20 +1889,12 @@ let compile output_prefix =
17711889
body))))
17721890
| _ -> assert false)
17731891
| {primitive = Pjs_object_create labels; args} ->
1774-
let args_block, args_expr =
1775-
if args = [] then ([], [])
1776-
else
1777-
let new_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in
1778-
Ext_list.split_map args (fun x ->
1779-
match compile_lambda new_cxt x with
1780-
| {block; value = Some b} -> (block, b)
1781-
| {value = None} -> assert false)
1782-
in
1892+
let args_code, args_expr = compile_arguments lambda_cxt args in
17831893
let block, exp =
17841894
Lam_compile_external_obj.assemble_obj_args labels args_expr
17851895
in
17861896
Js_output.output_of_block_and_expression lambda_cxt.continuation
1787-
(Ext_list.concat_append args_block block)
1897+
(Ext_list.append args_code block)
17881898
exp
17891899
| {primitive; args; loc} -> compile_primitive_default primitive args loc
17901900
and collect_dup_overrides (copy_id : Ident.t) (lam : Lambda.t)

‎compiler/core/lam_compile_main.ml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ let is_awaited_unit (lam : Lambda.t) =
215215
true
216216
| _ -> false
217217

218+
(* See [Lam_stats.assignable]. *)
219+
let assigned_variables (group : Lam_group.t) =
220+
match group with
221+
| Single (_, _, lam) | Nop lam -> Lambda_traverse.assigned_variables lam
222+
| Recursive bindings ->
223+
Ext_list.fold_left bindings Set_ident.empty (fun acc (_, lam) ->
224+
Set_ident.union acc (Lambda_traverse.assigned_variables lam))
225+
218226
(* See [Lam_stats.awaited_units]. *)
219227
let awaited_units (groups : Lam_group.t list) =
220228
let units = ref Set_ident.empty in
@@ -360,6 +368,9 @@ let compile ?on_optimized_lambda (output_prefix : string)
360368
{
361369
meta with
362370
awaited_units = awaited_units groups;
371+
assignable =
372+
Ext_list.fold_left groups Set_ident.empty (fun acc group ->
373+
Set_ident.union acc (assigned_variables group));
363374
exports = meta.exports @ flat_exports;
364375
export_idents =
365376
Ext_list.fold_left flat_exports meta.export_idents (fun acc id ->

‎compiler/core/lam_stats.ml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ type t = {
4747
for sure it's not a function, there is no need to count them
4848
*)
4949
awaited_units: Set_ident.t;
50+
assignable: Set_ident.t;
51+
(** The variables some [Lassign] assigns. [Lam_compile] never delays
52+
reading one of them past other code. *)
5053
}
5154

5255
let pp = Format.fprintf
@@ -68,4 +71,5 @@ let make ~export_idents ~export_ident_sets : t =
6871
exports = export_idents;
6972
export_idents = export_ident_sets;
7073
awaited_units = Set_ident.empty;
74+
assignable = Set_ident.empty;
7175
}

‎compiler/core/lam_stats.mli‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ type t = {
4040
(** Locals bound to an awaited compilation unit,
4141
[let d = await import(Unit)]: reads of nested [make] values through
4242
them use the flat exports too. *)
43+
assignable: Set_ident.t;
44+
(** The variables some [Lassign] assigns. [Lam_compile] never delays
45+
reading one of them past other code. *)
4346
}
4447

4548
val print : Format.formatter -> t -> unit

‎compiler/ml/lambda_traverse.ml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,17 @@ let free_variables l =
243243
| _ -> [])
244244
l
245245

246+
let assigned_variables l =
247+
let assigned = ref Set_ident.empty in
248+
let rec walk l =
249+
(match l with
250+
| Lassign (id, _) -> assigned := Set_ident.add id !assigned
251+
| _ -> ());
252+
iter walk l
253+
in
254+
walk l;
255+
!assigned
256+
246257
(* Substitution rebuilds through [shallow_map_sharing], so the result is
247258
normalized and an untouched subterm is returned physically unchanged. *)
248259
let subst_lambda s lam =

‎compiler/ml/lambda_traverse.mli‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ val iter : (Lambda.t -> unit) -> Lambda.t -> unit
2828

2929
val free_variables : Lambda.t -> Set_ident.t
3030

31+
val assigned_variables : Lambda.t -> Set_ident.t
32+
(** The variables some [Lassign] in the term assigns. *)
33+
3134
val subst_lambda : Lambda.t Ident.tbl -> Lambda.t -> Lambda.t
3235
(** Substitute for the free variables in the domain of the substitution.
3336
Assumes the substitution's image is out of reach of the term's bound

‎tests/belt_tests/src/bs_array_test.mjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,9 @@ Mocha.describe("Bs_array_test", () => {
847847
}), c === 6));
848848
});
849849
let id = (loc, x) => {
850+
let tmp = Belt_Array.reverse(x);
850851
let u = x.slice(0);
851-
Test_utils.eq("File \"bs_array_test.res\", line 298, characters 6-13", Belt_Array.reverse(x), (Belt_Array.reverseInPlace(u), u));
852+
Test_utils.eq("File \"bs_array_test.res\", line 298, characters 6-13", tmp, (Belt_Array.reverseInPlace(u), u));
852853
};
853854
Mocha.test("bs_array_test_id_functions", () => {
854855
id("File \"bs_array_test.res\", line 308, characters 7-14", []);

0 commit comments

Comments
 (0)