Skip to content

[12.x] Compare tag_info when merging constant blocks - #9386

Open
illusionalsagacity wants to merge 1 commit into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-block-arm-merge
Open

illusionalsagacity wants to merge 1 commit into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-block-arm-merge

Conversation

@illusionalsagacity

@illusionalsagacity illusionalsagacity commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #9385.

type small = Second(int)                // Second is position 0
type big = First(int) | Second(int)     // First is position 0, Second is 1

let f = (c: bool): big =>
  if c {
    First(1)
  } else {
    (Second(1): small :> big)            // still tagged 0, but printed "Second"
  }

Before, the if is dropped, so f(false) returns First(1):

function f(c) {
  return {
    TAG: "First",
    _0: 1
  };
}

After:

function f(c) {
  if (c) {
    return {
      TAG: "First",
      _0: 1
    };
  } else {
    return {
      TAG: "Second",
      _0: 1
    };
  }
}

A block's tag is its constructor's position. Second(1) is created as a small, where Second is constructor 0, so after the coercion it has the same tag and field as First(1). Js_analyzer.eq_expression (merging if/else branches) and Lam_constant.eq_approx (merging switch arms) ignored tag_info, which holds the constructor and record field names, so they treated the two as equal. Both now compare it. Master already keeps these apart.

Slight deoptimization

tag_info is compared whole, so it also compares parts that don't change the printed block, such as the source type's constructor count and the locations of attributes like doc comments. A few branches that print the same are no longer merged:

type small = First(int)
type big = First(int) | Second(int)

let f = (c: bool): big =>
  if c {
    First(1)
  } else {
    (First(1): small :> big)
  }

Before, this compiled to a single return {TAG: "First", _0: 1}; now both branches are kept. Master gives the same output as this PR. Comparing only the printed parts would mean comparing attributes without their locations, so this keeps the simpler, conservative comparison.

Tests

tests/tests/src/block_arm_merge_test.res covers if branches and switch arms returning coerced constructors and records. All 3 tests fail without the change, and no other module in tests/tests changes output.

A block's tag is its constructor's position in its type, so after a
coercion two blocks with the same tag and fields can have different
constructor names or record field names, and compile to different
JavaScript. Lam_constant.eq_approx, used to merge switch arms, and
Js_analyzer.eq_expression, used to merge if/else branches, compared only
the tag and fields, so such arms were merged and returned the wrong
value. Compare tag_info too.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
@pkg-pr-new

pkg-pr-new Bot commented Nov 22, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/darwin-arm64

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

@rescript/darwin-x64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 6983d0f

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.

1 participant