Skip to content

Fix variant coercion with as attribute #7058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

shulhi
Copy link
Member

@shulhi shulhi commented Sep 28, 2024

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
Copy link
Collaborator

@cristianoc cristianoc left a comment

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
Copy link
Member Author

shulhi commented Sep 28, 2024

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
Copy link
Collaborator

zth commented Sep 28, 2024

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
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
Copy link
Member Author

shulhi commented Sep 29, 2024

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

match args with
| [ Lconst a ] -> (
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
Collaborator

Choose a reason for hiding this comment

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

nice

@@ -532,7 +543,8 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
Lift.string (a ^ b)
| ( (Pstringrefs | Pstringrefu),
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
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
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.

Copy link
Member Author

@shulhi shulhi Oct 8, 2024

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
Collaborator

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
Copy link
Member

cknitt commented Oct 9, 2024

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

@cknitt
Copy link
Member

cknitt commented Oct 10, 2024

@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
Copy link
Member

cknitt commented Oct 10, 2024

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

@cristianoc
Copy link
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
Copy link
Member

cknitt commented Oct 10, 2024

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
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
Copy link
Member

cknitt commented Oct 10, 2024

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
Copy link
Member

cknitt commented Oct 10, 2024

Thanks again @shulhi! Merging.

@cknitt cknitt merged commit 1437815 into rescript-lang:master Oct 10, 2024
20 checks passed
@shulhi shulhi deleted the fix-variant-coercion-with-as-attribute branch October 10, 2024 12:52
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