Skip to content

Inline @inline functions in other modules without a flag - #9479

Merged
cknitt merged 1 commit into
closure-try-binderfrom
inline-exports-bodies
Nov 30, 2026
Merged

cknitt merged 1 commit into
closure-try-binderfrom
inline-exports-bodies

Conversation

@cknitt

@cknitt cknitt commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

@inline on a function now inlines its calls in other modules too, without -bs-cross-module-opt. This is PR 5 of the plan in #8733 (proposal 3), and replaces the compiler part of #8689 without its @inline(crossModule) payload: @inline means the same in every module.

An exported @inline function or functor stores its body in the .cmj when it's closed, not async and has no directive. The earlier PRs in this stack keep it safe:

Option.map, flatMap, getOr, mapOr, orElse, filter, forEach, isSome and isNone are now @inline, as #8689 marked them. So, for example, current->Option.isSome compiles to current !== undefined. That changes eight generated test files, all in this way.

Benchmarks: .cmj files get larger (tests +1.36%, runtime +2.39%), since they now store the bodies of @inline functions. That's the point of this PR. Allocations are flat (+0.01%).

New tests:

  • tests/tests/src/inline_export.res and inline_export_test.res (from Add opt-in cross-module inlining for Option helpers #8689): the Option helpers and an exported @inline function are inlined, a plain function is called, and fallback arguments are still evaluated eagerly.
  • tests/build_tests/projects/inline_function_rebuild: editing an @inline body on the same line, which leaves the .cmi as it is, compiles the caller again.

An @inline function containing try … catch is inlined too, since #9485 below.

A module whose uses are all inlined is still imported when it has side effects. A JS global with that module's name was then printed bare and read the import. The external-shadow pass now counts the modules imported for their side effects as bindings in scope, so the global is read through globalThis. Regression tests: inline_shadowed_import_test.res and an ounit case. Found by an experiment that marked every function in tests/tests @inline.

Part of stack #9486, after #9485.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D

@cknitt
cknitt changed the base branch from master to inline-fallback-unloadable-module November 29, 2026 08:04
@cknitt
cknitt added this pull request to stack #9474 November 29, 2026 08:04
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Changelog

🚀 New Feature

  • Inline @inline functions in the modules that use them too, not only in their own module, and mark Option.map, flatMap, getOr, mapOr, orElse, filter, forEach, isSome and isNone as @inline, so calls to them compile to the code they run. This applies to functions defined at the top level of a module.

Semantic Diff

1 commit · Full page

✏️ Inline @inline functions in other modules without a flag

9f3bc63 · cknitt + Claude · Nov 29, 2026, 07:54 UTC · edited

Why

@inline on a function inlined its calls only in its own module. Other modules inlined it only with -bs-cross-module-opt, which also inlined every small closed function, annotated or not. @inline now means the same in every module: an exported @inline function or functor stores its body in the .cmj, and other modules inline it, flag or not. The Option helpers that #8689 marked are @inline. This is PR 5 of the plan in #8733 (proposal 3) and replaces the compiler part of #8689, without the @inline(crossModule) payload.

JS modified (another module, without -bs-cross-module-opt)

// A.res
@inline let g = x => x + 1
let h = x => x * 2

// B.res
let a = A.g(3)
let b = A.h(4)

js · B

-let a = A.g(3);
-
 let b = A.h(4);
+
+let a = 4;

JS modified (an Option helper)

// tests/belt_tests/src/bs_stack_test.res
while current->Option.isSome { … }

js

-while (Stdlib_Option.isSome(current)) {
+while (current !== undefined) {

Representation modified (which bodies a .cmj stores)

mapping · Lam_stats_export.values_of_export, Js_cmj_format.get_result

 true, false, null, undefined ↦ always
-@inline function or functor, closed ↦ with -bs-cross-module-opt
+@inline function or functor, closed, not async, no directive ↦ always
 other closed function under size 5, closed functor ↦ with -bs-cross-module-opt

Review

  • What keeps a body from being stored? It must be closed, since it is copied without its environment, not async and without a directive, and it must not call a relative-path external (Keep bodies that call relative-path externals out of other modules #9475). A caller that can't load a module the body names calls the function instead (Call instead of inlining a body that names a module the caller can't load #9478).
  • Which functions does this cover? Those defined at the top level of a module. A function in a nested module (module M = { @inline let f = … }) and an @inline let rec are still inlined only in their own module.
  • Does the caller rebuild when the body changes? Yes, through the changed .cmj (Compile dependents again when a module's .cmj changes #9473); tests/build_tests/projects/inline_function_rebuild edits a body on the same line, which leaves the .cmi as it is.
  • Can a JS global be captured by the import of a module whose uses were all inlined? Not any more. Such a module is still imported when it has side effects, so Js_pass_external_shadow now counts the modules imported for their side effects as bindings in scope (Lam_compile_env.side_effect_modules, which populate_required_modules uses too). Before, it saw only the modules the generated code still named and printed the global bare, which then read the import (inline_shadowed_import_test.res). Found by marking every function in tests/tests @inline.

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

⚠️ Compiler benchmarks

Allocated words

In total: 672,866,366 → 672,951,260 (+0.01%).

Generated projects: 630,418,332 → 630,462,914 (+0.01%).

Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Option.res (inputs changed) 755,865 758,362 +0.33%
packages/@rescript/runtime/Stdlib_Nullable.res (inputs changed) 330,533 331,056 +0.16%
packages/@rescript/runtime/Stdlib_Null.res (inputs changed) 333,034 333,557 +0.16%

(inputs changed): this pull request changes runtime or Belt sources, so these benchmarks compile different code than on the base, and their changes don't count as regressions.

Instructions

In total: 9,225,758,933 → 9,226,139,623 (±0.00%).

Benchmark Base Head Change
stress/InlineChain.res 139,165,907 139,408,054 +0.17%

Output size

In total: 27,307,580 → 27,309,991 (+0.01%).

⚠️ Regression: 1 output is more than 1% larger.

Benchmark Base Head Change
output/runtime/*.cmj (inputs changed) 17,059 17,467 +2.39%
output/tests/*.cmj 98,026 99,310 +1.31%

(inputs changed): this pull request changes runtime or Belt sources, so these benchmarks compile different code than on the base, and their changes don't count as regressions.

Pull request 9f3bc63 compared with base 84a2717 in this run.

@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.59459% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.46%. Comparing base (87fe94d) to head (9f3bc63).

Files with missing lines Patch % Lines
compiler/backend/lam_stats_export.ml 91.66% 1 Missing ⚠️
tests/ounit_tests/ounit_external_shadow_tests.ml 92.30% 1 Missing ⚠️
Additional details and impacted files
@@                  Coverage Diff                   @@
##           closure-try-binder    #9479      +/-   ##
======================================================
+ Coverage               82.45%   82.46%   +0.01%     
======================================================
  Files                     465      465              
  Lines                   65987    66011      +24     
======================================================
+ Hits                    54408    54439      +31     
+ Misses                  11579    11572       -7     
Files with missing lines Coverage Δ
compiler/backend/js_cmj_format.ml 93.61% <100.00%> (+0.13%) ⬆️
compiler/backend/js_pass_external_shadow.ml 98.41% <100.00%> (+0.07%) ⬆️
compiler/backend/lam_compile_env.ml 88.23% <100.00%> (+1.87%) ⬆️
compiler/backend/lam_compile_main.ml 94.67% <100.00%> (+0.04%) ⬆️
compiler/backend/lam_stats_export.ml 94.44% <91.66%> (+0.32%) ⬆️
tests/ounit_tests/ounit_external_shadow_tests.ml 89.74% <92.30%> (+1.28%) ⬆️

... and 5 files with indirect coverage changes

🚀 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.

@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

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-29T14:07:32.132839Z 9f3bc63 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.

@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: 972e147bff

ℹ️ 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 tests/tests/src/inline_export_test.res Resolved
@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 9f3bc63

@cknitt
cknitt force-pushed the inline-exports-bodies branch from 972e147 to 3a2f64a Compare November 29, 2026 09:26
@cknitt
cknitt removed this pull request from stack #9474 November 29, 2026 09:28
@cknitt
cknitt changed the base branch from inline-fallback-unloadable-module to closure-try-binder November 29, 2026 09:29
@cknitt
cknitt added this pull request to stack #9486 November 29, 2026 09:29
@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 3a2f64a1b4

ℹ️ 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".

cknitt added a commit that referenced this pull request Nov 29, 2026
Why:
    Since #9479, other modules inline `@inline` functions, but apart from
    the nine Option helpers, the stdlib marked none, so every call to a
    one-line wrapper such as `Array.reduce` stayed a call through the
    wrapper. 72 functions are now `@inline`, chosen from the exported
    functions whose bodies other modules can store:
    - argument-reordering wrappers around externals: `Array.reduce` and its
      variants, `Array.concatAll`, `Promise.catch`, the iterator and
      generator `next` functions, `Dom_storage`, `Dict.delete`,
      `BigInt.toInt`, `Bool.toString`;
    - combinators that take a callback: `Null`, `Nullable` and `Result`
      `map`, `flatMap`, `mapOr`, `getOr`, `forEach` and the like, `Result`,
      `Pair` and `Date` `equal` and `compare`;
    - the `…Opt` lookups of `Array` and `String`, `Array.last`, `isEmpty`
      of `Array`, `String`, `Map` and `Set`, `List.add`, `head`, `tail`,
      and the `Ordering` helpers.
    `Int.Constants.minValue` and `maxValue` become `@inline` constants in
    `Stdlib_Int.resi`.

JS modified (a caller of the stdlib):
Example:
    let reversed = [1, 2, 3]->Array.reduce(list{}, List.add)
    let last = [1, 2, 3]->Array.last
Before (js):
    let reversed = Stdlib_Array.reduce([1, 2, 3], /* [] */0, Stdlib_List.add);

    let last = Stdlib_Array.last([1, 2, 3]);
After (js):
    let reversed = [1, 2, 3].reduce(Stdlib_List.add, /* [] */0);

    let a = [1, 2, 3];

    let last = a[a.length - 1 | 0];

JS modified (a Promise.catch handler):
Before (js):
    Stdlib_Promise.$$catch(promise, e => { … });
After (js):
    promise.catch(err => {
      let e = Primitive_exceptions.internalToException(err);
      …
    });

Review: What was left out? Loops (`zip`, `partition`, `filterMap`,
`Option.all*`), parsers wrapped in `try`, async functions, and larger
functions such as `clamp`. Also functions whose inlined code came out
worse in the tests: every `…OrThrow` (a `throw` block at each call, without
a source position), `Lazy.make` and `isEvaluated` (the runtime's lazy
representation in every caller), `JsError.throwWithMessage` (its throw
site lost its source position), `Option.equal` and `compare`, `Null` and
`Nullable` `equal` and `compare`, and `JsExn.fromException` (larger, with
no simplification), and `Iterator.next` and `nextValue`, which use a
module-local helper. Belt is left for its own pass. `JsError.panic` stays
`@inline` (#9480), since without it the runtime's own JavaScript changes.

JS modified (the Int constants):
Example:
    // Stdlib_Int.resi, in module Constants
    @inline(-2147483648)
    let minValue: int
Before (js): Stdlib_Int.mjs
    let Constants = {
      minValue: -2147483648,
      maxValue: 2147483647
    };
After (js): Stdlib_Int.mjs; other modules inline the constants
    let Constants = {};

Review: How is a wrong `@inline` caught? Warning 113: the runtime builds
with warnings as errors, so a function other modules cannot inline fails
the build. The runtime's and Belt's own JavaScript is unchanged.

Breaking: JavaScript that reads `Int.Constants.minValue` or `maxValue`
from the runtime's `Stdlib_Int` module gets `undefined`; ReScript code
inlines them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
cknitt added a commit that referenced this pull request Nov 29, 2026
Why:
    Since #9479, other modules inline `@inline` functions, but apart from
    the nine Option helpers, the stdlib marked none, so every call to a
    one-line wrapper such as `Array.reduce` stayed a call through the
    wrapper. 72 functions are now `@inline`, chosen from the exported
    functions whose bodies other modules can store:
    - argument-reordering wrappers around externals: `Array.reduce` and its
      variants, `Array.concatAll`, `Promise.catch`, the iterator and
      generator `next` functions, `Dom_storage`, `Dict.delete`,
      `BigInt.toInt`, `Bool.toString`;
    - combinators that take a callback: `Null`, `Nullable` and `Result`
      `map`, `flatMap`, `mapOr`, `getOr`, `forEach` and the like, `Result`,
      `Pair` and `Date` `equal` and `compare`;
    - the `…Opt` lookups of `Array` and `String`, `Array.last`, `isEmpty`
      of `Array`, `String`, `Map` and `Set`, `List.add`, `head`, `tail`,
      and the `Ordering` helpers.
    `Int.Constants.minValue` and `maxValue` become `@inline` constants in
    `Stdlib_Int.resi`.

JS modified (a caller of the stdlib):
Example:
    let reversed = [1, 2, 3]->Array.reduce(list{}, List.add)
    let last = [1, 2, 3]->Array.last
Before (js):
    let reversed = Stdlib_Array.reduce([1, 2, 3], /* [] */0, Stdlib_List.add);

    let last = Stdlib_Array.last([1, 2, 3]);
After (js):
    let reversed = [1, 2, 3].reduce(Stdlib_List.add, /* [] */0);

    let a = [1, 2, 3];

    let last = a[a.length - 1 | 0];

JS modified (a Promise.catch handler):
Before (js):
    Stdlib_Promise.$$catch(promise, e => { … });
After (js):
    promise.catch(err => {
      let e = Primitive_exceptions.internalToException(err);
      …
    });

Review: What was left out? Loops (`zip`, `partition`, `filterMap`,
`Option.all*`), parsers wrapped in `try`, async functions, and larger
functions such as `clamp`. Also functions whose inlined code came out
worse in the tests: every `…OrThrow` (a `throw` block at each call, without
a source position), `Lazy.make` and `isEvaluated` (the runtime's lazy
representation in every caller), `JsError.throwWithMessage` (its throw
site lost its source position), `Option.equal` and `compare`, `Null` and
`Nullable` `equal` and `compare`, and `JsExn.fromException` (larger, with
no simplification), and `Iterator.next` and `nextValue`, which use a
module-local helper. Belt is left for its own pass. `JsError.panic` stays
`@inline` (#9480), since without it the runtime's own JavaScript changes.

JS modified (the Int constants):
Example:
    // Stdlib_Int.resi, in module Constants
    @inline(-2147483648)
    let minValue: int
Before (js): Stdlib_Int.mjs
    let Constants = {
      minValue: -2147483648,
      maxValue: 2147483647
    };
After (js): Stdlib_Int.mjs; other modules inline the constants
    let Constants = {};

Review: How is a wrong `@inline` caught? Warning 113: the runtime builds
with warnings as errors, so a function other modules cannot inline fails
the build. The runtime's and Belt's own JavaScript is unchanged.

Breaking: JavaScript that reads `Int.Constants.minValue` or `maxValue`
from the runtime's `Stdlib_Int` module gets `undefined`; ReScript code
inlines them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the inline-exports-bodies branch from 3a2f64a to dc9b2a2 Compare November 29, 2026 09:57
cknitt added a commit that referenced this pull request Nov 29, 2026
Why:
    Since #9479, other modules inline `@inline` functions, but apart from
    the nine Option helpers, the stdlib marked none, so every call to a
    one-line wrapper such as `Array.reduce` stayed a call through the
    wrapper. 72 functions are now `@inline`, chosen from the exported
    functions whose bodies other modules can store:
    - argument-reordering wrappers around externals: `Array.reduce` and its
      variants, `Array.concatAll`, `Promise.catch`, the iterator and
      generator `next` functions, `Dom_storage`, `Dict.delete`,
      `BigInt.toInt`, `Bool.toString`;
    - combinators that take a callback: `Null`, `Nullable` and `Result`
      `map`, `flatMap`, `mapOr`, `getOr`, `forEach` and the like, `Result`,
      `Pair` and `Date` `equal` and `compare`;
    - the `…Opt` lookups of `Array` and `String`, `Array.last`, `isEmpty`
      of `Array`, `String`, `Map` and `Set`, `List.add`, `head`, `tail`,
      and the `Ordering` helpers.
    `Int.Constants.minValue` and `maxValue` become `@inline` constants in
    `Stdlib_Int.resi`.

JS modified (a caller of the stdlib):
Example:
    let reversed = [1, 2, 3]->Array.reduce(list{}, List.add)
    let last = [1, 2, 3]->Array.last
Before (js):
    let reversed = Stdlib_Array.reduce([1, 2, 3], /* [] */0, Stdlib_List.add);

    let last = Stdlib_Array.last([1, 2, 3]);
After (js):
    let reversed = [1, 2, 3].reduce(Stdlib_List.add, /* [] */0);

    let a = [1, 2, 3];

    let last = a[a.length - 1 | 0];

JS modified (a Promise.catch handler):
Before (js):
    Stdlib_Promise.$$catch(promise, e => { … });
After (js):
    promise.catch(err => {
      let e = Primitive_exceptions.internalToException(err);
      …
    });

Review: What was left out? Loops (`zip`, `partition`, `filterMap`,
`Option.all*`), parsers wrapped in `try`, async functions, and larger
functions such as `clamp`. Also functions whose inlined code came out
worse in the tests: every `…OrThrow` (a `throw` block at each call, without
a source position), `Lazy.make` and `isEvaluated` (the runtime's lazy
representation in every caller), `JsError.throwWithMessage` (its throw
site lost its source position), `Option.equal` and `compare`, `Null` and
`Nullable` `equal` and `compare`, and `JsExn.fromException` (larger, with
no simplification), and `Iterator.next` and `nextValue`, which use a
module-local helper. Belt is left for its own pass. `JsError.panic` stays
`@inline` (#9480), since without it the runtime's own JavaScript changes.

JS modified (the Int constants):
Example:
    // Stdlib_Int.resi, in module Constants
    @inline(-2147483648)
    let minValue: int
Before (js): Stdlib_Int.mjs
    let Constants = {
      minValue: -2147483648,
      maxValue: 2147483647
    };
After (js): Stdlib_Int.mjs; other modules inline the constants
    let Constants = {};

Review: How is a wrong `@inline` caught? Warning 113: the runtime builds
with warnings as errors, so a function other modules cannot inline fails
the build. The runtime's and Belt's own JavaScript is unchanged.

Breaking: JavaScript that reads `Int.Constants.minValue` or `maxValue`
from the runtime's `Stdlib_Int` module gets `undefined`; ReScript code
inlines them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the inline-exports-bodies branch from dc9b2a2 to bf6e379 Compare November 29, 2026 10:07
@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: bf6e379575

ℹ️ 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".

cknitt added a commit that referenced this pull request Nov 29, 2026
Why:
    Since #9479, other modules inline `@inline` functions, but apart from
    the nine Option helpers, the stdlib marked none, so every call to a
    one-line wrapper such as `Array.reduce` stayed a call through the
    wrapper. 72 functions are now `@inline`, chosen from the exported
    functions whose bodies other modules can store:
    - argument-reordering wrappers around externals: `Array.reduce` and its
      variants, `Array.concatAll`, `Promise.catch`, the iterator and
      generator `next` functions, `Dom_storage`, `Dict.delete`,
      `BigInt.toInt`, `Bool.toString`;
    - combinators that take a callback: `Null`, `Nullable` and `Result`
      `map`, `flatMap`, `mapOr`, `getOr`, `forEach` and the like, `Result`,
      `Pair` and `Date` `equal` and `compare`;
    - the `…Opt` lookups of `Array` and `String`, `Array.last`, `isEmpty`
      of `Array`, `String`, `Map` and `Set`, `List.add`, `head`, `tail`,
      and the `Ordering` helpers.
    `Int.Constants.minValue` and `maxValue` become `@inline` constants in
    `Stdlib_Int.resi`.

JS modified (a caller of the stdlib):
Example:
    let reversed = [1, 2, 3]->Array.reduce(list{}, List.add)
    let last = [1, 2, 3]->Array.last
Before (js):
    let reversed = Stdlib_Array.reduce([1, 2, 3], /* [] */0, Stdlib_List.add);

    let last = Stdlib_Array.last([1, 2, 3]);
After (js):
    let reversed = [1, 2, 3].reduce(Stdlib_List.add, /* [] */0);

    let a = [1, 2, 3];

    let last = a[a.length - 1 | 0];

JS modified (a Promise.catch handler):
Before (js):
    Stdlib_Promise.$$catch(promise, e => { … });
After (js):
    promise.catch(err => {
      let e = Primitive_exceptions.internalToException(err);
      …
    });

Review: What was left out? Loops (`zip`, `partition`, `filterMap`,
`Option.all*`), parsers wrapped in `try`, async functions, and larger
functions such as `clamp`. Also functions whose inlined code came out
worse in the tests: every `…OrThrow` (a `throw` block at each call, without
a source position), `Lazy.make` and `isEvaluated` (the runtime's lazy
representation in every caller), `JsError.throwWithMessage` (its throw
site lost its source position), `Option.equal` and `compare`, `Null` and
`Nullable` `equal` and `compare`, and `JsExn.fromException` (larger, with
no simplification), and `Iterator.next` and `nextValue`, which use a
module-local helper. Belt is left for its own pass. `JsError.panic` stays
`@inline` (#9480), since without it the runtime's own JavaScript changes.

JS modified (the Int constants):
Example:
    // Stdlib_Int.resi, in module Constants
    @inline(-2147483648)
    let minValue: int
Before (js): Stdlib_Int.mjs
    let Constants = {
      minValue: -2147483648,
      maxValue: 2147483647
    };
After (js): Stdlib_Int.mjs; other modules inline the constants
    let Constants = {};

Review: How is a wrong `@inline` caught? Warning 113: the runtime builds
with warnings as errors, so a function other modules cannot inline fails
the build. The runtime's and Belt's own JavaScript is unchanged.

Breaking: JavaScript that reads `Int.Constants.minValue` or `maxValue`
from the runtime's `Stdlib_Int` module gets `undefined`; ReScript code
inlines them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
cknitt added a commit that referenced this pull request Nov 29, 2026
Why:
    Since #9479, other modules inline `@inline` functions, but apart from
    the nine Option helpers, the stdlib marked none, so every call to a
    one-line wrapper such as `Array.reduce` stayed a call through the
    wrapper. 72 functions are now `@inline`, chosen from the exported
    functions whose bodies other modules can store:
    - argument-reordering wrappers around externals: `Array.reduce` and its
      variants, `Array.concatAll`, `Promise.catch`, the iterator and
      generator `next` functions, `Dom_storage`, `Dict.delete`,
      `BigInt.toInt`, `Bool.toString`;
    - combinators that take a callback: `Null`, `Nullable` and `Result`
      `map`, `flatMap`, `mapOr`, `getOr`, `forEach` and the like, `Result`,
      `Pair` and `Date` `equal` and `compare`;
    - the `…Opt` lookups of `Array` and `String`, `Array.last`, `isEmpty`
      of `Array`, `String`, `Map` and `Set`, `List.add`, `head`, `tail`,
      and the `Ordering` helpers.
    `Int.Constants.minValue` and `maxValue` become `@inline` constants in
    `Stdlib_Int.resi`.

JS modified (a caller of the stdlib):
Example:
    let reversed = [1, 2, 3]->Array.reduce(list{}, List.add)
    let last = [1, 2, 3]->Array.last
Before (js):
    let reversed = Stdlib_Array.reduce([1, 2, 3], /* [] */0, Stdlib_List.add);

    let last = Stdlib_Array.last([1, 2, 3]);
After (js):
    let reversed = [1, 2, 3].reduce(Stdlib_List.add, /* [] */0);

    let a = [1, 2, 3];

    let last = a[a.length - 1 | 0];

JS modified (a Promise.catch handler):
Before (js):
    Stdlib_Promise.$$catch(promise, e => { … });
After (js):
    promise.catch(err => {
      let e = Primitive_exceptions.internalToException(err);
      …
    });

Review: What was left out? Loops (`zip`, `partition`, `filterMap`,
`Option.all*`), parsers wrapped in `try`, async functions, and larger
functions such as `clamp`. Also functions whose inlined code came out
worse in the tests: every `…OrThrow` (a `throw` block at each call, without
a source position), `Lazy.make` and `isEvaluated` (the runtime's lazy
representation in every caller), `JsError.throwWithMessage` (its throw
site lost its source position), `Option.equal` and `compare`, `Null` and
`Nullable` `equal` and `compare`, and `JsExn.fromException` (larger, with
no simplification), and `Iterator.next` and `nextValue`, which use a
module-local helper. Belt is left for its own pass. `JsError.panic` stays
`@inline` (#9480), since without it the runtime's own JavaScript changes.

JS modified (the Int constants):
Example:
    // Stdlib_Int.resi, in module Constants
    @inline(-2147483648)
    let minValue: int
Before (js): Stdlib_Int.mjs
    let Constants = {
      minValue: -2147483648,
      maxValue: 2147483647
    };
After (js): Stdlib_Int.mjs; other modules inline the constants
    let Constants = {};

Review: How is a wrong `@inline` caught? Warning 113: the runtime builds
with warnings as errors, so a function other modules cannot inline fails
the build. The runtime's and Belt's own JavaScript is unchanged.

Breaking: JavaScript that reads `Int.Constants.minValue` or `maxValue`
from the runtime's `Stdlib_Int` module gets `undefined`; ReScript code
inlines them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
cknitt added a commit that referenced this pull request Nov 29, 2026
Why:
    Since #9479, other modules inline `@inline` functions, but apart from
    the nine Option helpers, the stdlib marked none, so every call to a
    one-line wrapper such as `Array.reduce` stayed a call through the
    wrapper. 72 functions are now `@inline`, chosen from the exported
    functions whose bodies other modules can store:
    - argument-reordering wrappers around externals: `Array.reduce` and its
      variants, `Array.concatAll`, `Promise.catch`, the iterator and
      generator `next` functions, `Dom_storage`, `Dict.delete`,
      `BigInt.toInt`, `Bool.toString`;
    - combinators that take a callback: `Null`, `Nullable` and `Result`
      `map`, `flatMap`, `mapOr`, `getOr`, `forEach` and the like, `Result`,
      `Pair` and `Date` `equal` and `compare`;
    - the `…Opt` lookups of `Array` and `String`, `Array.last`, `isEmpty`
      of `Array`, `String`, `Map` and `Set`, `List.add`, `head`, `tail`,
      and the `Ordering` helpers.
    `Int.Constants.minValue` and `maxValue` become `@inline` constants in
    `Stdlib_Int.resi`.

JS modified (a caller of the stdlib):
Example:
    let reversed = [1, 2, 3]->Array.reduce(list{}, List.add)
    let last = [1, 2, 3]->Array.last
Before (js):
    let reversed = Stdlib_Array.reduce([1, 2, 3], /* [] */0, Stdlib_List.add);

    let last = Stdlib_Array.last([1, 2, 3]);
After (js):
    let reversed = [1, 2, 3].reduce(Stdlib_List.add, /* [] */0);

    let a = [1, 2, 3];

    let last = a[a.length - 1 | 0];

JS modified (a Promise.catch handler):
Before (js):
    Stdlib_Promise.$$catch(promise, e => { … });
After (js):
    promise.catch(err => {
      let e = Primitive_exceptions.internalToException(err);
      …
    });

Review: What was left out? Loops (`zip`, `partition`, `filterMap`,
`Option.all*`), parsers wrapped in `try`, async functions, and larger
functions such as `clamp`. Also functions whose inlined code came out
worse in the tests: every `…OrThrow` (a `throw` block at each call, without
a source position), `Lazy.make` and `isEvaluated` (the runtime's lazy
representation in every caller), `JsError.throwWithMessage` (its throw
site lost its source position), `Option.equal` and `compare`, `Null` and
`Nullable` `equal` and `compare`, and `JsExn.fromException` (larger, with
no simplification), and `Iterator.next` and `nextValue`, which use a
module-local helper. Belt is left for its own pass. `JsError.panic` stays
`@inline` (#9480), since without it the runtime's own JavaScript changes.

JS modified (the Int constants):
Example:
    // Stdlib_Int.resi, in module Constants
    @inline(-2147483648)
    let minValue: int
Before (js): Stdlib_Int.mjs
    let Constants = {
      minValue: -2147483648,
      maxValue: 2147483647
    };
After (js): Stdlib_Int.mjs; other modules inline the constants
    let Constants = {};

Review: How is a wrong `@inline` caught? Warning 113: the runtime builds
with warnings as errors, so a function other modules cannot inline fails
the build. The runtime's and Belt's own JavaScript is unchanged.

Breaking: JavaScript that reads `Int.Constants.minValue` or `maxValue`
from the runtime's `Stdlib_Int` module gets `undefined`; ReScript code
inlines them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
cknitt added a commit that referenced this pull request Nov 29, 2026
Why:
    Since #9479, other modules inline `@inline` functions, but apart from
    the nine Option helpers, the stdlib marked none, so every call to a
    one-line wrapper such as `Array.reduce` stayed a call through the
    wrapper. 72 functions are now `@inline`, chosen from the exported
    functions whose bodies other modules can store:
    - argument-reordering wrappers around externals: `Array.reduce` and its
      variants, `Array.concatAll`, `Promise.catch`, the iterator and
      generator `next` functions, `Dom_storage`, `Dict.delete`,
      `BigInt.toInt`, `Bool.toString`;
    - combinators that take a callback: `Null`, `Nullable` and `Result`
      `map`, `flatMap`, `mapOr`, `getOr`, `forEach` and the like, `Result`,
      `Pair` and `Date` `equal` and `compare`;
    - the `…Opt` lookups of `Array` and `String`, `Array.last`, `isEmpty`
      of `Array`, `String`, `Map` and `Set`, `List.add`, `head`, `tail`,
      and the `Ordering` helpers.
    `Int.Constants.minValue` and `maxValue` become `@inline` constants in
    `Stdlib_Int.resi`.

JS modified (a caller of the stdlib):
Example:
    let reversed = [1, 2, 3]->Array.reduce(list{}, List.add)
    let last = [1, 2, 3]->Array.last
Before (js):
    let reversed = Stdlib_Array.reduce([1, 2, 3], /* [] */0, Stdlib_List.add);

    let last = Stdlib_Array.last([1, 2, 3]);
After (js):
    let reversed = [1, 2, 3].reduce(Stdlib_List.add, /* [] */0);

    let a = [1, 2, 3];

    let last = a[a.length - 1 | 0];

JS modified (a Promise.catch handler):
Before (js):
    Stdlib_Promise.$$catch(promise, e => { … });
After (js):
    promise.catch(err => {
      let e = Primitive_exceptions.internalToException(err);
      …
    });

Review: What was left out? Loops (`zip`, `partition`, `filterMap`,
`Option.all*`), parsers wrapped in `try`, async functions, and larger
functions such as `clamp`. Also functions whose inlined code came out
worse in the tests: every `…OrThrow` (a `throw` block at each call, without
a source position), `Lazy.make` and `isEvaluated` (the runtime's lazy
representation in every caller), `JsError.throwWithMessage` (its throw
site lost its source position), `Option.equal` and `compare`, `Null` and
`Nullable` `equal` and `compare`, and `JsExn.fromException` (larger, with
no simplification), and `Iterator.next` and `nextValue`, which use a
module-local helper. Belt is left for its own pass. `JsError.panic` stays
`@inline` (#9480), since without it the runtime's own JavaScript changes.

JS modified (the Int constants):
Example:
    // Stdlib_Int.resi, in module Constants
    @inline(-2147483648)
    let minValue: int
Before (js): Stdlib_Int.mjs
    let Constants = {
      minValue: -2147483648,
      maxValue: 2147483647
    };
After (js): Stdlib_Int.mjs; other modules inline the constants
    let Constants = {};

Review: How is a wrong `@inline` caught? Warning 113: the runtime builds
with warnings as errors, so a function other modules cannot inline fails
the build. The runtime's and Belt's own JavaScript is unchanged.

Breaking: JavaScript that reads `Int.Constants.minValue` or `maxValue`
from the runtime's `Stdlib_Int` module gets `undefined`; ReScript code
inlines them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
cknitt added a commit that referenced this pull request Nov 29, 2026
Why:
    Since #9479, other modules inline `@inline` functions, but apart from
    the nine Option helpers, the stdlib marked none, so every call to a
    one-line wrapper such as `Array.reduce` stayed a call through the
    wrapper. 72 functions are now `@inline`, chosen from the exported
    functions whose bodies other modules can store:
    - argument-reordering wrappers around externals: `Array.reduce` and its
      variants, `Array.concatAll`, `Promise.catch`, the iterator and
      generator `next` functions, `Dom_storage`, `Dict.delete`,
      `BigInt.toInt`, `Bool.toString`;
    - combinators that take a callback: `Null`, `Nullable` and `Result`
      `map`, `flatMap`, `mapOr`, `getOr`, `forEach` and the like, `Result`,
      `Pair` and `Date` `equal` and `compare`;
    - the `…Opt` lookups of `Array` and `String`, `Array.last`, `isEmpty`
      of `Array`, `String`, `Map` and `Set`, `List.add`, `head`, `tail`,
      and the `Ordering` helpers.
    `Int.Constants.minValue` and `maxValue` become `@inline` constants in
    `Stdlib_Int.resi`.

JS modified (a caller of the stdlib):
Example:
    let reversed = [1, 2, 3]->Array.reduce(list{}, List.add)
    let last = [1, 2, 3]->Array.last
Before (js):
    let reversed = Stdlib_Array.reduce([1, 2, 3], /* [] */0, Stdlib_List.add);

    let last = Stdlib_Array.last([1, 2, 3]);
After (js):
    let reversed = [1, 2, 3].reduce(Stdlib_List.add, /* [] */0);

    let a = [1, 2, 3];

    let last = a[a.length - 1 | 0];

JS modified (a Promise.catch handler):
Before (js):
    Stdlib_Promise.$$catch(promise, e => { … });
After (js):
    promise.catch(err => {
      let e = Primitive_exceptions.internalToException(err);
      …
    });

Review: What was left out? Loops (`zip`, `partition`, `filterMap`,
`Option.all*`), parsers wrapped in `try`, async functions, and larger
functions such as `clamp`. Also functions whose inlined code came out
worse in the tests: every `…OrThrow` (a `throw` block at each call, without
a source position), `Lazy.make` and `isEvaluated` (the runtime's lazy
representation in every caller), `JsError.throwWithMessage` (its throw
site lost its source position), `Option.equal` and `compare`, `Null` and
`Nullable` `equal` and `compare`, and `JsExn.fromException` (larger, with
no simplification), and `Iterator.next` and `nextValue`, which use a
module-local helper. Belt is left for its own pass. `JsError.panic` stays
`@inline` (#9480), since without it the runtime's own JavaScript changes.

JS modified (the Int constants):
Example:
    // Stdlib_Int.resi, in module Constants
    @inline(-2147483648)
    let minValue: int
Before (js): Stdlib_Int.mjs
    let Constants = {
      minValue: -2147483648,
      maxValue: 2147483647
    };
After (js): Stdlib_Int.mjs; other modules inline the constants
    let Constants = {};

Review: How is a wrong `@inline` caught? Warning 113: the runtime builds
with warnings as errors, so a function other modules cannot inline fails
the build. The runtime's and Belt's own JavaScript is unchanged.

Breaking: JavaScript that reads `Int.Constants.minValue` or `maxValue`
from the runtime's `Stdlib_Int` module gets `undefined`; ReScript code
inlines them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt marked this pull request as ready for review November 29, 2026 12:08
@cknitt
cknitt requested a review from cristianoc November 29, 2026 12:12
Why:
    `@inline` on a function inlined its calls only in its own module.
    Other modules inlined it only with -bs-cross-module-opt, which also
    inlined every small closed function, annotated or not. `@inline` now
    means the same in every module: an exported `@inline` function or
    functor stores its body in the .cmj, and other modules inline it,
    flag or not. The Option helpers that #8689 marked are `@inline`. This
    is PR 5 of the plan in #8733 (proposal 3) and replaces the compiler
    part of #8689, without the `@inline(crossModule)` payload.

JS modified (another module, without -bs-cross-module-opt):
Example:
    // A.res
    @inline let g = x => x + 1
    let h = x => x * 2

    // B.res
    let a = A.g(3)
    let b = A.h(4)
Before (js): B
    let a = A.g(3);

    let b = A.h(4);
After (js): B
    let b = A.h(4);

    let a = 4;

JS modified (an Option helper):
Example:
    // tests/belt_tests/src/bs_stack_test.res
    while current->Option.isSome { … }
Before (js):
    while (Stdlib_Option.isSome(current)) {
After (js):
    while (current !== undefined) {

Representation modified (which bodies a .cmj stores):
Before (mapping): Lam_stats_export.values_of_export, Js_cmj_format.get_result
    true, false, null, undefined ↦ always
    @inline function or functor, closed ↦ with -bs-cross-module-opt
    other closed function under size 5, closed functor ↦ with -bs-cross-module-opt
After (mapping): Lam_stats_export.values_of_export, Js_cmj_format.get_result
    true, false, null, undefined ↦ always
    @inline function or functor, closed, not async, no directive ↦ always
    other closed function under size 5, closed functor ↦ with -bs-cross-module-opt

Review: What keeps a body from being stored? It must be closed, since it
is copied without its environment, not async and without a directive, and
it must not call a relative-path external (#9475). A caller that can't
load a module the body names calls the function instead (#9478).

Review: Which functions does this cover? Those defined at the top level
of a module. A function in a nested module (`module M = { @inline let f
= … }`) and an `@inline let rec` are still inlined only in their own
module.

Review: Does the caller rebuild when the body changes? Yes, through the
changed .cmj (#9473); tests/build_tests/projects/inline_function_rebuild
edits a body on the same line, which leaves the .cmi as it is.

Review: Can a JS global be captured by the import of a module whose uses
were all inlined? Not any more. Such a module is still imported when it has
side effects, so `Js_pass_external_shadow` now counts the modules imported
for their side effects as bindings in scope (`Lam_compile_env.side_effect_modules`,
which `populate_required_modules` uses too). Before, it saw only the modules
the generated code still named and printed the global bare, which then read
the import (inline_shadowed_import_test.res). Found by marking every function
in tests/tests `@inline`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
cknitt added a commit that referenced this pull request Nov 29, 2026
Why:
    Since #9479, other modules inline `@inline` functions, but apart from
    the nine Option helpers, the stdlib marked none, so every call to a
    one-line wrapper such as `Array.reduce` stayed a call through the
    wrapper. 72 functions are now `@inline`, chosen from the exported
    functions whose bodies other modules can store:
    - argument-reordering wrappers around externals: `Array.reduce` and its
      variants, `Array.concatAll`, `Promise.catch`, the iterator and
      generator `next` functions, `Dom_storage`, `Dict.delete`,
      `BigInt.toInt`, `Bool.toString`;
    - combinators that take a callback: `Null`, `Nullable` and `Result`
      `map`, `flatMap`, `mapOr`, `getOr`, `forEach` and the like, `Result`,
      `Pair` and `Date` `equal` and `compare`;
    - the `…Opt` lookups of `Array` and `String`, `Array.last`, `isEmpty`
      of `Array`, `String`, `Map` and `Set`, `List.add`, `head`, `tail`,
      and the `Ordering` helpers.
    `Int.Constants.minValue` and `maxValue` become `@inline` constants in
    `Stdlib_Int.resi`.

JS modified (a caller of the stdlib):
Example:
    let reversed = [1, 2, 3]->Array.reduce(list{}, List.add)
    let last = [1, 2, 3]->Array.last
Before (js):
    let reversed = Stdlib_Array.reduce([1, 2, 3], /* [] */0, Stdlib_List.add);

    let last = Stdlib_Array.last([1, 2, 3]);
After (js):
    let reversed = [1, 2, 3].reduce(Stdlib_List.add, /* [] */0);

    let a = [1, 2, 3];

    let last = a[a.length - 1 | 0];

JS modified (a Promise.catch handler):
Before (js):
    Stdlib_Promise.$$catch(promise, e => { … });
After (js):
    promise.catch(err => {
      let e = Primitive_exceptions.internalToException(err);
      …
    });

Review: What was left out? Loops (`zip`, `partition`, `filterMap`,
`Option.all*`), parsers wrapped in `try`, async functions, and larger
functions such as `clamp`. Also functions whose inlined code came out
worse in the tests: every `…OrThrow` (a `throw` block at each call, without
a source position), `Lazy.make` and `isEvaluated` (the runtime's lazy
representation in every caller), `JsError.throwWithMessage` (its throw
site lost its source position), `Option.equal` and `compare`, `Null` and
`Nullable` `equal` and `compare`, and `JsExn.fromException` (larger, with
no simplification), and `Iterator.next` and `nextValue`, which use a
module-local helper. Belt is left for its own pass. `JsError.panic` stays
`@inline` (#9480), since without it the runtime's own JavaScript changes.

JS modified (the Int constants):
Example:
    // Stdlib_Int.resi, in module Constants
    @inline(-2147483648)
    let minValue: int
Before (js): Stdlib_Int.mjs
    let Constants = {
      minValue: -2147483648,
      maxValue: 2147483647
    };
After (js): Stdlib_Int.mjs; other modules inline the constants
    let Constants = {};

Review: How is a wrong `@inline` caught? Warning 113: the runtime builds
with warnings as errors, so a function other modules cannot inline fails
the build. The runtime's and Belt's own JavaScript is unchanged.

Breaking: JavaScript that reads `Int.Constants.minValue` or `maxValue`
from the runtime's `Stdlib_Int` module gets `undefined`; ReScript code
inlines them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the inline-exports-bodies branch from bf6e379 to 9f3bc63 Compare November 29, 2026 14:05
@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 9f3bc63dc4

ℹ️ 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".

@cknitt
cknitt merged commit 276a746 into master Nov 30, 2026
22 of 23 checks passed
@cknitt
cknitt deleted the inline-exports-bodies branch November 30, 2026 07:53
cknitt added a commit that referenced this pull request Nov 30, 2026
Why:
    Since #9479, other modules inline `@inline` functions, but apart from
    the nine Option helpers, the stdlib marked none, so every call to a
    one-line wrapper such as `Array.reduce` stayed a call through the
    wrapper. 72 functions are now `@inline`, chosen from the exported
    functions whose bodies other modules can store:
    - argument-reordering wrappers around externals: `Array.reduce` and its
      variants, `Array.concatAll`, `Promise.catch`, the iterator and
      generator `next` functions, `Dom_storage`, `Dict.delete`,
      `BigInt.toInt`, `Bool.toString`;
    - combinators that take a callback: `Null`, `Nullable` and `Result`
      `map`, `flatMap`, `mapOr`, `getOr`, `forEach` and the like, `Result`,
      `Pair` and `Date` `equal` and `compare`;
    - the `…Opt` lookups of `Array` and `String`, `Array.last`, `isEmpty`
      of `Array`, `String`, `Map` and `Set`, `List.add`, `head`, `tail`,
      and the `Ordering` helpers.
    `Int.Constants.minValue` and `maxValue` become `@inline` constants in
    `Stdlib_Int.resi`.

JS modified (a caller of the stdlib):
Example:
    let reversed = [1, 2, 3]->Array.reduce(list{}, List.add)
    let last = [1, 2, 3]->Array.last
Before (js):
    let reversed = Stdlib_Array.reduce([1, 2, 3], /* [] */0, Stdlib_List.add);

    let last = Stdlib_Array.last([1, 2, 3]);
After (js):
    let reversed = [1, 2, 3].reduce(Stdlib_List.add, /* [] */0);

    let a = [1, 2, 3];

    let last = a[a.length - 1 | 0];

JS modified (a Promise.catch handler):
Before (js):
    Stdlib_Promise.$$catch(promise, e => { … });
After (js):
    promise.catch(err => {
      let e = Primitive_exceptions.internalToException(err);
      …
    });

Review: What was left out? Loops (`zip`, `partition`, `filterMap`,
`Option.all*`), parsers wrapped in `try`, async functions, and larger
functions such as `clamp`. Also functions whose inlined code came out
worse in the tests: every `…OrThrow` (a `throw` block at each call, without
a source position), `Lazy.make` and `isEvaluated` (the runtime's lazy
representation in every caller), `JsError.throwWithMessage` (its throw
site lost its source position), `Option.equal` and `compare`, `Null` and
`Nullable` `equal` and `compare`, and `JsExn.fromException` (larger, with
no simplification), and `Iterator.next` and `nextValue`, which use a
module-local helper. Belt is left for its own pass. `JsError.panic` stays
`@inline` (#9480), since without it the runtime's own JavaScript changes.

JS modified (the Int constants):
Example:
    // Stdlib_Int.resi, in module Constants
    @inline(-2147483648)
    let minValue: int
Before (js): Stdlib_Int.mjs
    let Constants = {
      minValue: -2147483648,
      maxValue: 2147483647
    };
After (js): Stdlib_Int.mjs; other modules inline the constants
    let Constants = {};

Review: How is a wrong `@inline` caught? Warning 113: the runtime builds
with warnings as errors, so a function other modules cannot inline fails
the build. The runtime's and Belt's own JavaScript is unchanged.

Breaking: JavaScript that reads `Int.Constants.minValue` or `maxValue`
from the runtime's `Stdlib_Int` module gets `undefined`; ReScript code
inlines them.


Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D

Signed-off-by: Christoph Knittel <ck@cca.io>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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