Skip to content

[12.x] Compare constructor constants by runtime value - #9319

Merged
cknitt merged 8 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-as-int-constant-equality
Nov 21, 2026
Merged

cknitt merged 8 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-as-int-constant-equality

Conversation

@illusionalsagacity

@illusionalsagacity illusionalsagacity commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Refs #9317. Follows #9318, fixing the same root cause in two other folding paths.

Problem

Since #7058, Lam_constant_convert stores the runtime value of an @as(int) constructor in Const_int.i, while every other constructor keeps its position in the type there. Two places compare constants by i alone, so an @as(int) constructor is treated as equal to the constructor whose position matches its value:

type t = | @as(1) A | B | C

let eq = A == B // folded to true; A is 1 and B is "B" at runtime

type literals = | @as(1) U | @as("x") V
let eq2 = U == V // folded to true

type source = X | Y | Z
let toT = (x: source): t =>
  switch x {
  | X => B
  | Y => A
  | Z => B
  }

Before, all three arms of toT are merged, so toT(Y) returns "B":

let eq = true;

function toT(x) {
  switch (x) {
    case "X" :
    case "Y" :
    case "Z" :
      return "B";
  }
}

After:

let eq = 1 === "B";

function toT(x) {
  switch (x) {
    case "Y" :
      return 1;
    case "X" :
    case "Z" :
      return "B";
  }
}

The comparisons are now left to the runtime, as on master.

Fix

Lam_constant.int_value gives the JavaScript value a Const_int is compiled to, following Lam_compile_const.translate:

  • a number, for plain ints, @as(int) constructors and [];
  • a string, for a constructor's name or its @as("…") value;
  • any other @as literal as written.

Lam_constant.eq_approx, which Lam_compile.group_apply uses to merge switch arms, compares those values. Folding Pintcomp in Lam.prim compares numbers with numbers, and strings with strings for == and !=. Any other pair, such as a number against a string or float literals that may be spelled differently, is left unfolded.

An @as string is kept as written, so two different spellings can be the same JavaScript string, such as "\u0041" and "A". Strings are compared with Ast_utf8_string.literals_equal, which Js_exp_make.str_equal now uses too: equal spellings are equal, different spellings without escapes or unicode are different, and anything else is left to the runtime.

Tests

tests/tests/src/variant_as_int_equality_test.res checks each comparison with constants, which may fold, and through an @inline(never) id, which runs at runtime. It covers:

  • @as(int) constructors against the constructor at their position, and constructors against themselves;
  • a coerced @as(int) constructor against an int;
  • @as strings spelled differently with the same value;
  • @as(null), @as(true) and @as(1.5) constructors against the @as(int) constructor at their position;
  • <, <=, > and >= between constructors, folded and at runtime;
  • the result of each arm of switches returning different constructors.

Without the compiler changes, 5 of its 8 tests fail. With them they pass, and no other module in tests/tests changes output. tests/ounit_tests/ounit_unicode_tests.ml checks each result of Ast_utf8_string.literals_equal.

Lam.switch folded a constant scrutinee by looking its integer up in
sw_consts, which is keyed by constructor position. Since rescript-lang#7058,
Lam_constant_convert stores the runtime value of an @as(int) constructor
in that integer, so the fold took the case whose position equals the
constructor's runtime value: with `type t = | @as(1) A | @as(0) B`,
`name(A)` folded to B's case.

Look a constant constructor up by its tag in sw_names.consts instead,
and a constant block by its constructor name in sw_names.blocks, leaving
the switch unfolded when it is not found. This also fixes folding after
a coercion to a variant with another constructor order, which picked
the case at the constructor's position in the source type.

Fixes rescript-lang#9317.

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

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

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/darwin-arm64

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

@rescript/darwin-x64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 27f1333

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Since rescript-lang#7058, Lam_constant_convert stores the runtime value of an
@as(int) constructor in Const_int.i, while other constructors keep their
position in the type there. Folding Pintcomp and Lam_constant.eq_approx
compared i alone, so an @as(int) constructor was taken as equal to the
constructor at the position matching its value: with
`type t = | @as(1) A | B`, `A == B` folded to true, and switch arms
returning A and B were merged.

Add Lam_constant.int_value, the JavaScript value a Const_int is compiled
to, and compare that instead. A comparison of a number with a string, or
of literals that may be spelled differently, is left to the runtime.

Refs rescript-lang#9317.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
@illusionalsagacity
illusionalsagacity force-pushed the fix-as-int-constant-equality branch from bad1c4e to 3ff06de Compare November 18, 2026 19:32
@cknitt

cknitt commented Nov 19, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 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-21T14:10:51.641360Z 27f1333 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: bbe7e3bda3

ℹ️ 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 compiler/core/lam.ml Resolved
An @as string is kept as written, so two different spellings can be the
same JavaScript string, such as an escape for "A" and the plain "A".
Folding Pintcomp compared the spellings, so such a comparison folded to
false.

Move the rule Js_exp_make.str_equal uses for string literals into
Ast_utf8_string.literals_equal, and use it for the fold too: equal
spellings are equal, and spellings without escapes or unicode that
differ are different; anything else is left to the runtime.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
@illusionalsagacity
illusionalsagacity marked this pull request as ready for review November 19, 2026 15:00
Cover constructors with @as(null), @as(true) and @as(1.5) at the
position matching an @as(int) value, both in comparisons and in switch
arms that would otherwise be merged. Without the previous commits, both
new tests fail.

Fix the test's doc comment, which said every constant comparison folds;
those whose result can't be known from the constants are left to the
runtime. Drop the @as(int) arm of Lam_constant.int_value that converted
the value again, since Lam_constant_convert already stores it in i.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Check that <, <=, > and >= between constructors give the same result
folded as at runtime. Before this change, with `@as(1) A | B`, `A <= B`
folded to true, as both stored 1, while `1 <= "B"` is false.

Add unit tests for each result of Ast_utf8_string.literals_equal.

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

Copy link
Copy Markdown
Contributor Author

@cknitt mind taking another look?

@cknitt

cknitt commented Nov 21, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 27f13335b5

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

Thanks, looks good! Checked locally: the ounit tests and the full mocha suite pass, and the generated output of no other test module changes. The escaped @as string case from the Codex review now stays a runtime comparison.

Small thing before merging: please update the description. #9318 is merged now, so the "stacked on #9318" note can go, and the test file has 8 tests now, not 4.

@illusionalsagacity

Copy link
Copy Markdown
Contributor Author

Updated the description, thanks 👍

@cknitt
cknitt merged commit 9c830f4 into rescript-lang:maintenance/12.x Nov 21, 2026
26 checks passed
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