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

513572d · 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/ml/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: 639,870,498 → 640,034,127 (+0.03%).

Generated projects: 629,805,682 → 629,823,070 (±0.00%).

Benchmark Base Head Change
stress/NestedClosures.res 10,442,343 10,466,598 +0.23%

Instructions

In total: 9,215,369,410 → 9,217,643,071 (+0.02%).

Benchmark Base Head Change
stress/NestedClosures.res 528,225,458 529,114,143 +0.17%

Output size

In total: 25,920,835 → 25,922,113 (±0.00%).

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

Pull request 513572d compared with base a826170 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.18%. Comparing base (a826170) to head (513572d).

Files with missing lines Patch % Lines
compiler/ml/translcore.ml 95.55% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9344   +/-   ##
=======================================
  Coverage   82.17%   82.18%           
=======================================
  Files         458      458           
  Lines       65678    65723   +45     
=======================================
+ Hits        53971    54014   +43     
- Misses      11707    11709    +2     
Files with missing lines Coverage Δ
compiler/ml/translcore.ml 88.79% <95.55%> (+0.40%) ⬆️
🚀 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: 513572d

@github-actions

Copy link
Copy Markdown

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 245c6ab to 513572d Compare November 20, 2026 21:10

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