Skip to content

Leave source locations out of bodies stored in .cmj files - #9470

Merged
cknitt merged 1 commit into
masterfrom
strip-cmj-locations
Nov 30, 2026
Merged

cknitt merged 1 commit into
masterfrom
strip-cmj-locations

Conversation

@cknitt

@cknitt cknitt commented Nov 28, 2026

Copy link
Copy Markdown
Member

Function bodies stored in a .cmj for other modules to inline no longer keep source locations. This is PR 1 of the plan in #8733 (problem 2.10).

Before, a stored body kept the locations of its applications, primitives and functions, and -no-keep-locs doesn't cover them. With -bs-cross-module-opt, which the runtime and Belt are built with, that meant:

  • a line shift changed the .cmj;
  • the runtime's published .cmj files contained the absolute paths of the machine that built them.

The next PR in the plan rebuilds dependents when a .cmj changes. Without this one, it would rebuild the direct dependents of a module with a .resi on every line shift. Nothing reads these locations once a body is inlined in another module.

The new Lambda_traverse.drop_locations sets every location to Location.none, building through Lambda's constructors like the rest of that module. Generated JavaScript doesn't change, for the runtime, Belt or the test suite.

New tests: two cases in ounit_lambda_traverse_tests.ml. One checks that a term with locations under nodes without them comes back with none. The other checks that a term without locations is handed back as it is.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D

@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Changelog

🏠 Internal

  • Leave source locations out of the function bodies stored in .cmj files for inlining, so a .cmj no longer changes when only line numbers do and no longer contains build paths.

Semantic Diff

1 commit · Full page

Leave source locations out of bodies stored in .cmj files

fafe0cc · cknitt + Claude · Nov 28, 2026, 20:48 UTC

Why

A function body stored in a .cmj for other modules to inline kept the locations of its applications, primitives and functions, and -no-keep-locs does not cover them. So with -bs-cross-module-opt a line shift changed the .cmj, and the runtime's published .cmj files held the absolute paths of the machine that built them. Rebuilding dependents when a .cmj changes (#8733, PR 2) would then rebuild them on every line shift. Nothing reads these locations once the body is inlined elsewhere.

Cmj modified (stored body)

// A.res, compiled with -bs-cross-module-opt, then again with a blank
// line added at the top
@inline let g = x => x + 1
let h = x => x * 2
let flag = true

text

-A.cmj: 200 bytes, names /…/A.res
-after the line shift: A.cmj changes
+A.cmj: 170 bytes, names no file
+after the line shift: A.cmj is byte for byte the same

Review

  • Does inlining change? No. A dependent's JS is the same, and so is the runtime's and Belt's.
  • Why build through Lambda's constructors? The rest of Lambda_traverse does, so a stored body is normalized like any other term.

@codecov

codecov Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.45%. Comparing base (6b9bc07) to head (fafe0cc).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9470   +/-   ##
=======================================
  Coverage   82.44%   82.45%           
=======================================
  Files         465      465           
  Lines       65935    65961   +26     
=======================================
+ Hits        54357    54385   +28     
+ Misses      11578    11576    -2     
Files with missing lines Coverage Δ
compiler/backend/lam_stats_export.ml 93.47% <100.00%> (+0.14%) ⬆️
compiler/typing/lambda_traverse.ml 94.35% <100.00%> (+0.26%) ⬆️
tests/ounit_tests/ounit_lambda_traverse_tests.ml 100.00% <100.00%> (ø)

... and 1 file 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.

@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

🎉 Compiler benchmarks

Allocated words

In total: 672,527,634 → 672,518,210 (±0.00%).

Generated projects: 630,046,858 → 630,047,529 (±0.00%).

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

Instructions

In total: 9,225,541,339 → 9,225,692,490 (±0.00%).

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

Output size

In total: 27,316,430 → 27,303,219 (-0.05%).

🎉 Improvement: 2 outputs are more than 1% smaller.

Benchmark Base Head Change
output/belt/*.cmj 42,889 33,211 -22.57%
output/runtime/*.cmj 19,973 17,059 -14.59%
output/tests/*.cmj 98,809 97,934 -0.89%

Pull request fafe0cc compared with base 6b9bc07 in this run.

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: fafe0cc

@github-actions

Copy link
Copy Markdown

@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-29T07:16:52.039707Z 7b26dcc 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 7b26dccb5f

ℹ️ 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 marked this pull request as ready for review November 29, 2026 07:19
cknitt added a commit that referenced this pull request Nov 29, 2026
Why:
    Dependents read a module's .cmj: the values they copy in (true, false,
    null and undefined always, more with -bs-cross-module-opt), the arities
    they call with, and whether the module has side effects, which decides
    whether they import it. The build system only compared .cmi files, so a
    change the interface doesn't show left their JavaScript stale. Without a
    .resi this was mostly hidden, because the .cmi holds declaration
    locations; with one, every such change went unnoticed. This is the
    build-system part of #8689, and PR 2 of the plan in #8733. #9470 keeps
    line shifts from changing a .cmj that stores bodies.

Representation modified (publishing a compile):
Before (mapping): publish_compiler_artifacts
    .cmi changed ↦ dependents compiled again
    .cmj changed, .cmi unchanged ↦ nothing
    .cmj ↦ written by the compiler, checked to exist
After (mapping): publish_compiler_artifacts
    .cmi changed ↦ dependents compiled again
    .cmj changed, .cmi unchanged ↦ dependents compiled again; the .cmi's modification time moves, for the next build
    .cmj ↦ held until the compile is published, written only when it changed, kept as before when the compile fails

JS modified (dependent of a module with an interface):
Example:
    // A.resi
    let flag: bool
    // A.res, after the first build: was `let flag = true`
    let flag = false
    Console.log("effect")
    // B.res
    let f = A.flag
Before (js): B.mjs after the second build
    let f = true;

    export {
      f,
    }
    /* No side effect */
After (js): B.mjs after the second build
    import * as A from "./A.mjs";

    let f = false;

    export {
      f,
    }
    /* A Not a pure module */

Review: Does watch mode compile more? Only when a .cmj changes. With
default flags it holds no bodies, so it changes only when an exported
value's arity, one of the four copied constants or the module's purity
changes, and each of those changes the dependents' JavaScript.

Review: Why move the .cmi's modification time? A later build decides from
.cmi times whether dependents are current, so it sees the .cmj change too
without reading every .cmj.

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 strip-cmj-locations branch from 7b26dcc to 1ff5fd8 Compare November 29, 2026 07:36
@cknitt
cknitt removed this pull request from stack #9474 November 29, 2026 09:28
@cknitt
cknitt added this pull request to stack #9486 November 29, 2026 09:29
Why:
    A function body stored in a .cmj for other modules to inline kept the
    locations of its applications, primitives and functions, and
    -no-keep-locs does not cover them. So with -bs-cross-module-opt a line
    shift changed the .cmj, and the runtime's published .cmj files held the
    absolute paths of the machine that built them. Rebuilding dependents when
    a .cmj changes (#8733, PR 2) would then rebuild them on every line shift.
    Nothing reads these locations once the body is inlined elsewhere.

Cmj modified (stored body):
Example:
    // A.res, compiled with -bs-cross-module-opt, then again with a blank
    // line added at the top
    @inline let g = x => x + 1
    let h = x => x * 2
    let flag = true
Before (text):
    A.cmj: 200 bytes, names /…/A.res
    after the line shift: A.cmj changes
After (text):
    A.cmj: 170 bytes, names no file
    after the line shift: A.cmj is byte for byte the same

Review: Does inlining change? No. A dependent's JS is the same, and so is
the runtime's and Belt's.

Review: Why build through Lambda's constructors? The rest of
Lambda_traverse does, so a stored body is normalized like any other term.

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 strip-cmj-locations branch from 1ff5fd8 to fafe0cc Compare November 29, 2026 09:57
@cknitt
cknitt requested a review from cristianoc November 29, 2026 12:12
@cknitt
cknitt merged commit 5260f3a into master Nov 30, 2026
23 checks passed
@cknitt
cknitt deleted the strip-cmj-locations branch November 30, 2026 07:53
cknitt added a commit that referenced this pull request Nov 30, 2026
* Compile dependents again when a module's .cmj changes

Why:
    Dependents read a module's .cmj: the values they copy in (true, false,
    null and undefined always, more with -bs-cross-module-opt), the arities
    they call with, and whether the module has side effects, which decides
    whether they import it. The build system only compared .cmi files, so a
    change the interface doesn't show left their JavaScript stale. Without a
    .resi this was mostly hidden, because the .cmi holds declaration
    locations; with one, every such change went unnoticed. This is the
    build-system part of #8689, and PR 2 of the plan in #8733. #9470 keeps
    line shifts from changing a .cmj that stores bodies.

Representation modified (publishing a compile):
Before (mapping): publish_compiler_artifacts
    .cmi changed ↦ dependents compiled again
    .cmj changed, .cmi unchanged ↦ nothing
    .cmj ↦ written by the compiler, checked to exist
After (mapping): publish_compiler_artifacts
    .cmi changed ↦ dependents compiled again
    .cmj changed, .cmi unchanged ↦ dependents compiled again; the .cmi's modification time moves, for the next build
    .cmj ↦ held until the compile is published, written only when it changed, kept as before when the compile fails

JS modified (dependent of a module with an interface):
Example:
    // A.resi
    let flag: bool
    // A.res, after the first build: was `let flag = true`
    let flag = false
    Console.log("effect")
    // B.res
    let f = A.flag
Before (js): B.mjs after the second build
    let f = true;

    export {
      f,
    }
    /* No side effect */
After (js): B.mjs after the second build
    import * as A from "./A.mjs";

    let f = false;

    export {
      f,
    }
    /* A Not a pure module */

Review: Does watch mode compile more? Only when a .cmj changes. With
default flags it holds no bodies, so it changes only when an exported
value's arity, one of the four copied constants or the module's purity
changes, and each of those changes the dependents' JavaScript.

Review: Why move the .cmi's modification time? A later build decides from
.cmi times whether dependents are current, so it sees the .cmj change too
without reading every .cmj.

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>

* Date the interface before writing a changed .cmj

A changed .cmj was written before its .cmi was dated. A build killed in
between found the .cmj unchanged the next time and left the dependents
stale, and a failed date reported the interface as unchanged. The .cmi is
now dated, and the change recorded, right before the .cmj is written. A
review by Claude (Fable model) found this.

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>

---------

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