Skip to content

Evaluate labelled arguments passed out of order in source order - #9344

Draft
cknitt wants to merge 1 commit into
masterfrom
labelled-arg-order
Draft

cknitt wants to merge 1 commit into
masterfrom
labelled-arg-order

Conversation

@cknitt

@cknitt cknitt commented Nov 19, 2026

Copy link
Copy Markdown
Member

Fixes #9340.

An application lists its arguments in declared order. So a labelled argument passed before arguments declared ahead of it ran after them:

let f = (x: int, ~extra: int, y: int) => [x, extra, y]
f(~extra=b(), a(), c())

compiled to [a(), b(), c()], so a ran before b. This happened for any function and any external, including @obj (mk(~y=b(), ~x=a())). ReScript otherwise evaluates arguments left to right (#9154).

Now translcore checks the source positions of the arguments that are not stable. A stable argument reads the same wherever it is evaluated: a constant, variable, function, constant constructor or module path. If the other arguments are out of order and one of them can have side effects, translcore binds them to temporaries in source order. It also binds the callee first, unless the callee is an identifier. The call then gets the temporaries:

let arg = b();
let arg$1 = a();
let arg$2 = c();
let r = [arg$1, arg, arg$2];

These calls compile as before:

  • calls whose arguments are already in source order
  • calls where only stable arguments move, such as Belt.Set.fromArray(~id=module(IntCmp), range(0, 30))
  • calls whose arguments have no side effects, such as mk(~y=o.b, ~x=o.a)

Tests: labelled_argument_order_test.res checks a function, a @send and an @obj external, a callee that is a call, a read after an effect that changes it, an optional argument, a partial application, and arguments already in order.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gHt1hL7PvCPo3r2viL

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Labelled arguments passed before arguments declared ahead of them are evaluated in source order.

Semantic Diff

1 commit · Full page

Evaluate labelled arguments passed out of order in source order

d50face · cknitt + Claude · Nov 19, 2026, 19:17 UTC

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)

let f = (x: int, ~extra: int, y: int) => [x, extra, y]
let r = f(~extra=b(), a(), c())

js

+let arg = b();
+let arg$1 = a();
+let arg$2 = c();
 let r = [
-  a(),
-  b(),
-  c()
+  arg$1,
+  arg,
+  arg$2
 ];

Fixes #9340

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.

@cknitt

cknitt commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 245c6abe9e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread compiler/typing/translcore.ml Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T19:28:08.560956Z 245c6ab Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 636,509,109 → 636,673,770 (+0.03%).

Generated projects: 629,030,522 → 629,048,113 (±0.00%).

Benchmark Base Head Change
stress/NestedClosures.res 10,273,646 10,297,901 +0.24%

Instructions

In total: 9,221,430,982 → 9,226,256,449 (+0.05%).

Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Math.resi 21,296,414 21,370,447 +0.35%
packages/@rescript/runtime/Stdlib_Temporal_PlainDate.res 24,086,792 24,160,617 +0.31%
packages/@rescript/runtime/Stdlib_List.resi 24,523,846 24,597,793 +0.30%
packages/@rescript/runtime/Stdlib_Temporal_PlainDateTime.res 25,526,606 25,600,279 +0.29%
parse/RedBlackTreeNoComments.res 14,487,560 14,525,067 +0.26%
packages/@rescript/runtime/Stdlib_String.resi 29,450,176 29,522,852 +0.25%
packages/@rescript/runtime/Stdlib_Temporal_ZonedDateTime.res 31,194,313 31,269,308 +0.24%
parse/RedBlackTree.res 14,919,553 14,955,188 +0.24%
packages/@rescript/belt/src/Belt_Array.resi 33,711,320 33,785,298 +0.22%
packages/@rescript/runtime/Stdlib_Date.resi 34,293,714 34,364,747 +0.21%
7 more changes
Benchmark Base Head Change
stress/NestedClosures.res 529,923,398 531,003,903 +0.20%
packages/@rescript/belt/src/Belt_List.resi 38,983,143 39,055,105 +0.18%
packages/@rescript/runtime/Stdlib_Array.resi 40,104,283 40,177,849 +0.18%
packages/@rescript/runtime/Stdlib_TypedArray.res 41,307,615 41,381,813 +0.18%
format/RedBlackTreeNoComments.res 24,073,562 24,110,668 +0.15%
format/RedBlackTree.res 24,418,604 24,455,710 +0.15%
packages/@rescript/belt/src/Belt_List.res 118,135,109 118,256,501 +0.10%

Output size

In total: 25,962,507 → 25,963,676 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request d50face compared with base 643e9c0 in this run.

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.55556% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.22%. Comparing base (643e9c0) to head (d50face).

Files with missing lines Patch % Lines
compiler/typing/translcore.ml 95.55% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9344   +/-   ##
=======================================
  Coverage   82.21%   82.22%           
=======================================
  Files         460      460           
  Lines       65551    65596   +45     
=======================================
+ Hits        53891    53934   +43     
- Misses      11660    11662    +2     
Files with missing lines Coverage Δ
compiler/typing/translcore.ml 88.69% <95.55%> (+0.41%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9344

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9344

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9344

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9344

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9344

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9344

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9344

commit: d50face

@github-actions

Copy link
Copy Markdown

@cknitt
cknitt force-pushed the labelled-arg-order branch from 245c6ab to 513572d Compare November 20, 2026 21:10
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHt1hL7PvCPo3r2viL
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the labelled-arg-order branch from 513572d to d50face Compare November 23, 2026 06:47

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Labelled arguments passed out of order are evaluated in the declared order

1 participant