Skip to content

Fix variant coercion with as attribute - #7058

Merged
cknitt merged 4 commits into
rescript-lang:masterfrom
shulhi:fix-variant-coercion-with-as-attribute
Oct 10, 2024
Merged

cknitt merged 4 commits into
rescript-lang:masterfrom
shulhi:fix-variant-coercion-with-as-attribute

Conversation

@shulhi

@shulhi shulhi commented Sep 28, 2024

Copy link
Copy Markdown
Member

Fix for #7036

@shulhi
shulhi marked this pull request as ready for review September 28, 2024 09:33
@shulhi
shulhi requested a review from zth September 28, 2024 09:33

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering why int is special.
Could one construct another issue analogous to the motivating one using eg string?

@shulhi

shulhi commented Sep 28, 2024

Copy link
Copy Markdown
Member Author

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 Const_int to begin with.

@zth

zth commented Sep 28, 2024

Copy link
Copy Markdown
Member

Constructors used to always compile to int. Does that have to do with it perhaps?

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 @as.

@cristianoc

Copy link
Copy Markdown
Collaborator

Constructors used to always compile to int. Does that have to do with it perhaps?

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 @as.

I was thinking the same: constructors used to always compile to int.
Though it would be good to double check that the analogous example for strings is not problematic, just in case.

@shulhi
shulhi force-pushed the fix-variant-coercion-with-as-attribute branch 2 times, most recently from 79ded60 to 0ba6d8a Compare September 28, 2024 12:59
@shulhi
shulhi marked this pull request as draft September 28, 2024 13:51
@shulhi

shulhi commented Sep 29, 2024 •

Copy link
Copy Markdown
Member Author

@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 @as attribute, so it is consistent. It seems more apt that the fix is done here given that the bug is only related to primitives operation.

Also I added more tests.

Comment thread jscomp/core/lam.ml Outdated
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Comment thread jscomp/core/lam.ml Outdated
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@shulhi shulhi Oct 8, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After further checking, there are only several cases that match Lconst (Const_int ...) like in the following code,

https://github.com/shulhi/rescript-compiler/blob/e6fb6e54e2f949b9b08a4d8da8cb1c0f32fd948c/compiler/core/lam_compile.ml#L1774-L1783

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.

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.

@shulhi I don't follow entirely, how is the runtime representation changed?

@shulhi
shulhi force-pushed the fix-variant-coercion-with-as-attribute branch 2 times, most recently from 6a19249 to e6fb6e5 Compare October 8, 2024 11:55
@shulhi
shulhi marked this pull request as ready for review October 8, 2024 12:34
@cknitt

cknitt commented Oct 9, 2024

Copy link
Copy Markdown
Member

@shulhi Thanks a lot! 👍 Could you add a CHANGELOG entry?

@cknitt

cknitt commented Oct 10, 2024

Copy link
Copy Markdown
Member

@shulhi Thanks a lot! 👍 Could you add a CHANGELOG entry?

And could you please rebase?

@shulhi
shulhi force-pushed the fix-variant-coercion-with-as-attribute branch from e6fb6e5 to b922e7b Compare October 10, 2024 10:37
@shulhi
shulhi force-pushed the fix-variant-coercion-with-as-attribute branch from 843970b to cadb22c Compare October 10, 2024 10:45
@cknitt

cknitt commented Oct 10, 2024

Copy link
Copy Markdown
Member

Doesn't build because of the formatting, please run make format.

@cristianoc

Copy link
Copy Markdown
Collaborator

Doesn't build because of the formatting, please run make format.

How about adding a very visible message at the end to suggest doing make format, when the format check fails.
That might save a bunch of iterations in future.

@cknitt

cknitt commented Oct 10, 2024

Copy link
Copy Markdown
Member

Currently it says

OCaml code formatting issues found.

We can change that to

OCaml code formatting issues found. Please run `make format`.

@cristianoc

Copy link
Copy Markdown
Collaborator

Currently it says

OCaml code formatting issues found.

We can change that to

OCaml code formatting issues found. Please run `make format`.

In flashing red.

@cknitt

cknitt commented Oct 10, 2024

Copy link
Copy Markdown
Member

This is what it looks like currently. Not quite "flashing red", but visible enough I think?

Bildschirmfoto 2024-10-10 um 14 30 19

@cknitt

cknitt commented Oct 10, 2024

Copy link
Copy Markdown
Member

Thanks again @shulhi! Merging.

@cknitt
cknitt merged commit 1437815 into rescript-lang:master Oct 10, 2024
@shulhi
shulhi deleted the fix-variant-coercion-with-as-attribute branch October 10, 2024 12:52
illusionalsagacity added a commit to illusionalsagacity/rescript-compiler that referenced this pull request Nov 18, 2026
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>
cknitt pushed a commit that referenced this pull request Nov 19, 2026
…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>
cknitt added a commit that referenced this pull request Nov 21, 2026
* 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>
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.

4 participants