Repository navigation
Fix variant coercion with as attribute - #7058
Conversation
cristianoc
left a comment
There was a problem hiding this comment.
Wondering why int is special.
Could one construct another issue analogous to the motivating one using eg string?
I thought about that too, but I was not quite sure why it is always converted to |
|
Constructors used to always compile to Great fix. Like @cristianoc I also wonder whether there are more places to fix here, since int is just one of the things you can set the payload as with |
I was thinking the same: constructors used to always compile to int. |
79ded60 to
0ba6d8a
Compare
|
@zth I took a different approach in e238013 (compared to c8a8e08). Maybe this is a better approach because it doesn't change how the constructor is represented with/without Also I added more tests. |
| match (prim, a) with | ||
| | Pnegint, Const_int { i } -> Lift.int (Int32.neg i) | ||
| | Pnegint, Const_int { i; comment = pointer_info} -> | ||
| let ii = extract_const_as pointer_info i in |
| Const_string { s = a; unicode = false }, | ||
| Const_int { i = b } ) -> ( | ||
| Const_int { i = b; comment = pointer_info } ) -> ( | ||
| let b = extract_const_as pointer_info b in |
There was a problem hiding this comment.
At this point, after seeing all these case, why not having this directly inside Const_int?
Or rather, having this done, in whatever form, at Const_int creation (or earlier)?
There was a problem hiding this comment.
Another way of asking the question: are there cases where the "original" int is what's needed, and cases where the override one is what's needed.
If both are needed, 2 values need to be accessed/stored.
Otherwise, it's cleaner to ever only have 1.
There was a problem hiding this comment.
After further checking, there are only several cases that match Lconst (Const_int ...) like in the following code,
I've fallback to the original implementation since it is simpler but it does change the representation of variant in runtime (just for int case). I don't think it affect anything else and I've added several more coercion tests just in case to cover cases like in the above linked code.
Another way of asking the question: are there cases where the "original" int is what's needed, and cases where the override one is what's needed.
As far as I understand, the override one is still needed for case where the as payload is not an int. The "original" int is needed for runtime representation for the variants, and when the case is int both override and original values are the same.
There was a problem hiding this comment.
@shulhi I don't follow entirely, how is the runtime representation changed?
6a19249 to
e6fb6e5
Compare
|
@shulhi Thanks a lot! 👍 Could you add a CHANGELOG entry? |
And could you please rebase? |
e6fb6e5 to
b922e7b
Compare
843970b to
cadb22c
Compare
|
Doesn't build because of the formatting, please run |
How about adding a very visible message at the end to suggest doing |
|
Currently it says We can change that to |
In flashing red. |
|
Thanks again @shulhi! Merging. |
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>
…9318) * Fix constant folding of a switch on an @as(int) constructor Lam.switch folded a constant scrutinee by looking its integer up in sw_consts, which is keyed by constructor position. Since #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 #9317. Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com> * Link PR in CHANGELOG entry Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com> * Shorten CHANGELOG entry Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com> --------- Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
* Fix constant folding of a switch on an @as(int) constructor Lam.switch folded a constant scrutinee by looking its integer up in sw_consts, which is keyed by constructor position. Since #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 #9317. Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com> * Link PR in CHANGELOG entry Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com> * Shorten CHANGELOG entry Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com> * Compare constructor constants by runtime value Since #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 #9317. Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com> * Keep differently spelled @as strings unfolded 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> * Test @as literal constructors in comparisons and merged arms 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> * Test ordering of constructors and Ast_utf8_string.literals_equal 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> --------- Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com> Co-authored-by: Christoph Knittel <ck@cca.io>

Fix for #7036