Skip to content

Fix argument evaluation order under function inlining - #8572

Merged
cristianoc merged 1 commit into
masterfrom
fix/beta-reduce-arg-order
Aug 20, 2026
Merged

Fix argument evaluation order under function inlining#8572
cristianoc merged 1 commit into
masterfrom
fix/beta-reduce-arg-order

Conversation

@cristianoc

Copy link
Copy Markdown
Collaborator

What

Lam_beta_reduce.propagate_beta_reduce (and its _with_map variant) built the let-bindings for non-substitutable inlined-call arguments by prepending during fold_left2 — leaving the list in reverse parameter order — and then wrapped the body with fold_right, which makes the first parameter's binding innermost. Net effect: when the inliner fires and arguments can't be substituted directly (anything that isn't a constant or variable), the last argument is evaluated first.

Repro (current master):

let helper = (x, y) => equalish(x, copy(y))
let _ = helper(effA(0), effB(0)) // effB's side effects run before effA's

The reversed order is visible in checked-in output today: bs_set_int_test.mjs evaluates setEqArray's second argument before its first (this PR regenerates it to call order).

Fix

Fold left over the reversed binding list, so the first parameter's binding is outermost and arguments evaluate in call order. Two-line change, applied to both variants.

Tests

  • New inline_arg_order_test.res fixture: an inlinable helper applied to recursive (hence non-substitutable) effectful arguments; the checked-in JS pins effA before effB, so a regression flips the snapshot.
  • bs_set_int_test.mjs regenerated to source order — the only other snapshot that changes across the full suite.

Found while reviewing the generated-code changes of #8570 (which widens inliner exposure and would have made this reachable more often); the bug itself predates that stack and reproduces on master.

🤖 Generated with Claude Code

Lam_beta_reduce built the bindings for non-substitutable arguments by
prepending during fold_left2 (reverse parameter order) and then wrapped
the body with fold_right, making the first parameter's binding innermost
- so the last argument was evaluated first. Fold left instead, so the
first parameter's binding is outermost and arguments evaluate in call
order.

The reversed order was visible in checked-in output: bs_set_int_test.mjs
evaluated setEqArray's second argument before its first. The new
inline_arg_order_test fixture pins the order with recursive (hence
non-substitutable) effectful arguments; its checked-in JS would show the
regression directly.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
@cristianoc
cristianoc force-pushed the fix/beta-reduce-arg-order branch from 96751a9 to 9deb399 Compare August 20, 2026 10:11

@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: 96751a9ee6

ℹ️ 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 CHANGELOG.md Outdated
}
}

let _ = {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Replace the side-effect-only wildcard binding with ignore

This fixture evaluates the call for effA/effB side effects and intentionally discards its Boolean result, so let _ = ... is precisely the side-effect-only wildcard-binding pattern prohibited by the repository guidance. Use ignore(helper(effA(0), effB(0))) (or the equivalent pipeline form) so the intent is explicit without relying on a wildcard binding.

AGENTS.md reference: AGENTS.md:L43-L44

Useful? React with 👍 / 👎.

@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.84%. Comparing base (5e23370) to head (9deb399).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #8572   +/-   ##
=======================================
  Coverage   75.84%   75.84%           
=======================================
  Files         475      475           
  Lines       62835    62835           
=======================================
  Hits        47655    47655           
  Misses      15180    15180           
Files with missing lines Coverage Δ
compiler/core/lam_beta_reduce.ml 91.30% <100.00%> (ø)
🚀 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 Aug 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@8572

@rescript/belt

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/belt@8572

@rescript/darwin-arm64

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

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@8572

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/runtime@8572

@rescript/win32-x64

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

commit: 9deb399

@github-actions

Copy link
Copy Markdown

@cknitt cknitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🎉

@cristianoc
cristianoc merged commit 124bc4b into master Aug 20, 2026
29 checks passed
@cristianoc
cristianoc deleted the fix/beta-reduce-arg-order branch August 20, 2026 13:47
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.

2 participants