Repository navigation
Conversation
Changelog🐛 Bug fix
Semantic Diff4 commits · Full page Emit source map mappings for pattern-match arms, branch tests, and block boundariesbc67a45 · mununki · Nov 28, 2026, 19:50 UTC
SourceMap modified (pattern-match arms and dispatch tests) let opt = o =>
switch o {
| Some(Some(x)) => x + 1
| Some(None) => 0
| None => -1
}
function opt(o) { @0 -> 1:10 o =>
- if (o === undefined) { @6 -> 3:4 Some(Some(x)) => x + 1
- return -1; (none)
- } (none)
- let x = Primitive_option.valFromOption(o); @10 -> 4:4 Some(None) => 0
- if (x !== undefined) { @6 -> 3:9 Some(x)) => x + 1
- return x + 1 | 0; @11 -> 3:21 x + 1
- } else { (none)
- return 0; (none)
- } (none)
-} (none)
+ if (o === undefined) { @2 -> 2:2 switch o {
+ return -1; @4 -> 5:13 -1
+ } @2 -> 5:14 (end)
+ let x = Primitive_option.valFromOption(o); @2 -> 2:2 switch o {
+ if (x !== undefined) { @2 -> 2:2 switch o {
+ return x + 1 | 0; @4 -> 3:21 x + 1
+ } else { @2 -> 3:26 (end) @3 -> 4:18 0
+ return 0; @4 -> 4:18 0
+ } @2 -> 4:19 (end)
+} @0 -> 6:3 (end)Fixes #9387 Review
Avoid branch-location overhead in .cmj exports and non-sourcemap builds2abd577 · mununki · Nov 29, 2026, 05:24 UTC
Handle dispatch prologues, preserve Seq source_loc, and cut pass allocations74f49b0 · mununki · Nov 29, 2026, 06:00 UTC
Hoist source-map helpers out of Lam_compile.compilec7e1138 · mununki · Nov 29, 2026, 07:14 UTC
|
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ce8391d087
ℹ️ 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".
| | ( Some outer, | ||
| [ | ||
| { | ||
| statement_desc = If _ | Int_switch _ | String_switch _; | ||
| source_loc = Some inner; |
There was a problem hiding this comment.
Recognize dispatch blocks with shared-destructuring prologues
For a non-tail list match with empty, singleton, pair, and fallback arms assigning a passed-in ref, the nonempty branch contains a shared let match = l.tl followed by another dispatch test. This singleton-only check rejects that [Variable; If] block, so the printer maps its opening brace to the enclosing switch and its closing brace to a descendant arm's end. Executing only the empty-list arm consequently produces a zero-count V8 span that includes that executed arm, incorrectly reporting it as uncovered. Recognize same-switch dispatch through the shared-destructuring prologue, or retain explicit dispatch-boundary locations instead of deriving this block's extent from its descendants.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in bab5fe8: updated is_same_dispatch_block in compiler/backend/js_dump.ml to step through nested blocks and same-switch Variable destructuring/declaration prologues before matching the inner dispatch statement, and preserved source_loc when unwrapping Seq (_, Undefined) wrappers around unit/ref-assignment arms (Js_exp_make.econd, remove_pure_sub_exp, Js_stmt_make.exp, Js_analyzer.rev_flatten_seq). Added non-tail list match V8 coverage regression tests (lstRef / lstRefPair) in tests/project_tests/source_map/.
| let else_loc = | ||
| match nest.source_loc with | ||
| | Some _ as loc -> loc | ||
| | None -> block_first_arm_loc [nest] |
There was a problem hiding this comment.
Bypass location searches when source maps are disabled
With source maps disabled, JS IR source locations are None, so every printed else if takes this fallback and recursively searches the entire remaining chain for a location that cannot exist. Repeating that search at each branch introduces quadratic printer work: a 1,024-branch chain requires roughly one million additional statement visits, even though its emitted output is linear in the number of branches. Large generated conditional or guard chains therefore incur this regression in the default compilation mode. Skip location discovery when mapping is inactive, or cache the subtree locations.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 6067aa9: guarded block and case boundary location lookups in compiler/backend/js_dump.ml behind Js_source_map.is_active (), and limited the else if fallback to nest's immediate then branch so chained conditionals stay linear even when source maps are enabled.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9469 +/- ##
==========================================
- Coverage 82.44% 82.39% -0.05%
==========================================
Files 465 465
Lines 65935 66492 +557
==========================================
+ Hits 54357 54783 +426
- Misses 11578 11709 +131
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
…ock boundaries
Why:
Pattern-match arms and `if` branches returning a constant or variable
(`| Empty => "empty"`, `| None => -1`, `| _ => x`) emitted no source map
mapping, because `Lambda.t` carried `Location.t` only on `Lprim`,
`Lapply`, and `Lfunction`. Branch dispatch tests (`typeof s !== "object"`,
`o === undefined`, `switch (s.TAG)`) and shared destructuring
(`Primitive_option.valFromOption(o)`) either had no mapping or borrowed a
sibling arm's location, and block/function closing braces had no end
mappings, causing V8 coverage tools (`c8` / `v8-to-istanbul`) to drop
unexecuted ranges and report unexecuted arms as covered (rescript-lang#9387).
SourceMap modified (pattern-match arms and dispatch tests):
Example:
let opt = o =>
switch o {
| Some(Some(x)) => x + 1
| Some(None) => 0
| None => -1
}
Before (text):
function opt(o) { @0 -> 1:10 o =>
if (o === undefined) { @6 -> 3:4 Some(Some(x)) => x + 1
return -1; (none)
} (none)
let x = Primitive_option.valFromOption(o); @10 -> 4:4 Some(None) => 0
if (x !== undefined) { @6 -> 3:9 Some(x)) => x + 1
return x + 1 | 0; @11 -> 3:21 x + 1
} else { (none)
return 0; (none)
} (none)
} (none)
After (text):
function opt(o) { @0 -> 1:10 o =>
if (o === undefined) { @2 -> 2:2 switch o {
return -1; @4 -> 5:13 -1
} @2 -> 5:14 (end)
let x = Primitive_option.valFromOption(o); @2 -> 2:2 switch o {
if (x !== undefined) { @2 -> 2:2 switch o {
return x + 1 | 0; @4 -> 3:21 x + 1
} else { @2 -> 3:26 (end) @3 -> 4:18 0
return 0; @4 -> 4:18 0
} @2 -> 4:19 (end)
} @0 -> 6:3 (end)
Review: Does attaching locations to Lambda branch nodes change optimizations or generated JS?
No. Leaf nodes (`Lconst`, `Lvar`) are untouched, and `Lambda_traverse.make_key`
and `Lambda.eq_approx` ignore all branch and node locations. Generated JS is
byte-identical with source maps enabled or disabled (apart from the
`sourceMappingURL` comment).
Review: How are non-1:1 arm optimizations (merged, split, or reordered branches) handled?
Shared destructuring (`relocate_cell_args`) and dispatch tests map to the
enclosing `switch`/`if` rather than a sibling arm. Fallthrough `case` labels
keep each arm's `pat_loc`, inlined static exits (`Lam_pass_exits`) propagate
`branch_loc` to every inlined site, and same-switch nested dispatch blocks
(`if (typeof s !== "object")`) map `{` and `}` to `switch` start so
interleaved executed arms are never marked uncovered.
Fixes rescript-lang#9387
Signed-off-by: mununki <woonki.moon@gmail.com>
Why:
1. Lambda.no_if_locs was exported in lambda.mli without external callers,
triggering reanalyze's dead-code check; keep it internal and share
both no_if_locs and no_branch_loc when locations are absent.
2. Marshaling branch, switch, and handler locations inside
persistent_closed_lambda inflated runtime .cmj files (+1.42%);
strip branch locations before exporting Lambda terms to .cmj.
3. Printing chained else-if trees scanned remaining else branches
quadratically when nested If statements carried no source_loc, and
divide_variant / Lam_compile repeated location work when source maps
were disabled (+1.34% instructions on PolyVariantSwitch.res); guard
source-map boundary lookups behind active source-map state and share
unchanged pattern-match cells.
Signed-off-by: mununki <woonki.moon@gmail.com>
…cations
Why:
1. Non-tail pattern matches with shared destructuring prologues (such as
`let match = l.tl; let x = l.hd; if (match !== 0) ...`) were rejected
by the singleton-only `is_same_dispatch_block` check, mapping the
dispatch block's opening brace to the enclosing `switch` and its
closing brace to a descendant arm end so executing only the empty-list
arm marked that executed arm uncovered in V8 coverage. Step through
same-switch variable prologues and nested blocks in
`is_same_dispatch_block`.
2. `Js_exp_make.econd`, `remove_pure_sub_exp`, `Js_stmt_make.exp`, and
`Js_analyzer.rev_flatten_seq` dropped `source_loc` when unwrapping
`Seq (_, Undefined)` wrappers around unit/ref-assignment arms. Preserve
the outer `source_loc` when the unwrapped child has none.
3. `Lam_pass_exits.simplif_with_loc` allocated a `(lam, loc)` pair on
every visited node, and `Lswitch`/`Lstringswitch` traversals across
backend/typing passes allocated closures per switch, causing >1%
allocation regressions on Result, Primitive_object, and no-op/leaf-edit
benchmarks. Track inlined handler locations via a single ref and
eliminate per-node/per-switch closure allocations.
Signed-off-by: mununki <woonki.moon@gmail.com>
Why:
`Lam_compile_main.compile` calls `Lam_compile.compile_lambda ~output_prefix`
once per top-level `Lam_group.t`, which re-enters `Lam_compile.compile`.
Keeping the nine source-map helper functions nested inside `compile`
capturing `has_source_map` allocated fresh closures for every top-level
group, regressing allocations by +1.32% on `build/Namespace/edit` (1,000
top-level namespace alias groups) and +0.71% on `stress/InlineChain.res`.
Hoist the source-map helpers to module top-level so `Lam_compile.compile`
allocates no per-group closures, checking `Js_source_map.enabled ()`
inside each helper and caching it locally in `compile_ifthenelse`.
Signed-off-by: mununki <woonki.moon@gmail.com>
cb366f7 to
c7e1138
Compare
Fixes #9387
Summary
Location.tand per-armbranch_loc(pat_loc+body_loc) to Lambda branch nodes (Lswitch,Lstringswitch,Lifthenelse,Lstaticcatch), leaving leaf nodes (Lconst,Lvar) untouched so optimizations and generated JS stay unchanged.Matching,Switch,Polyvar_pattern_match,Translcore) and Lambda optimization passes (Lam_pass_exits,Lam_pass_guard_raises,Lam_pass_remove_alias,Lam_bounded_vars), relocating shared destructuring primitives (such asPrimitive_option.valFromOption) to the enclosingswitchlocation instead of a sibling arm.source_locthrough JS IR lowering (Lam_compile,Js_stmt_make,Js_exp_make,Js_pass_flatten), including untagged variant plans (compile_untagged_plan) andif/ternary collapses.Js_dump(mark_source_loc_end) for functions,if/elseblocks,switchblocks,case,default:, andbreak;, mapping same-switch nested dispatch blocks (if (typeof s !== "object")) to theswitchstart so interleaved executed arms are not misattributed.NODE_V8_COVERAGEremapping tests intests/project_tests/source_map/, plus OUnit tests inounit_exits_tests.mlandounit_lambda_traverse_tests.ml.Checklist
changes/README.md), or no release notes are needed and a maintainer should applychangelog:skip.git commit -s).