Repository navigation
Conversation
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>
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
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>
bad1c4e to
3ff06de
Compare
|
@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: 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".
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>
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>
|
@cknitt mind taking another look? |
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
left a comment
There was a problem hiding this comment.
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.
|
Updated the description, thanks 👍 |
Refs #9317. Follows #9318, fixing the same root cause in two other folding paths.
Problem
Since #7058,
Lam_constant_convertstores the runtime value of an@as(int)constructor inConst_int.i, while every other constructor keeps its position in the type there. Two places compare constants byialone, so an@as(int)constructor is treated as equal to the constructor whose position matches its value:Before, all three arms of
toTare merged, sotoT(Y)returns"B":After:
The comparisons are now left to the runtime, as on master.
Fix
Lam_constant.int_valuegives the JavaScript value aConst_intis compiled to, followingLam_compile_const.translate:@as(int)constructors and[];@as("…")value;@asliteral as written.Lam_constant.eq_approx, whichLam_compile.group_applyuses to merge switch arms, compares those values. FoldingPintcompinLam.primcompares 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
@asstring is kept as written, so two different spellings can be the same JavaScript string, such as"\u0041"and"A". Strings are compared withAst_utf8_string.literals_equal, whichJs_exp_make.str_equalnow 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.reschecks 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;@as(int)constructor against an int;@asstrings 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;Without the compiler changes, 5 of its 8 tests fail. With them they pass, and no other module in
tests/testschanges output.tests/ounit_tests/ounit_unicode_tests.mlchecks each result ofAst_utf8_string.literals_equal.