rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Remove @return(undefined_to_opt) and %undefined_to_opt primitive

Open cknitt opened this issue 7 months ago • 3 comments

@return(undefined_to_opt)
@send external get: (t<'k, 'v>, 'k) => option<'v> = "get"

produces compiler output like

let v = m.get("A");

let v$1 = v === undefined ? undefined : Primitive_option.some(v);

which doesn't make any sense. This is because it is a leftover from the days when options were compiled differently and None was not represented as undefined in the JS output yet.

As this feature was never documented, remove it.

Similarly, I think we can replace %undefined_to_opt by %identity. This leads to slight changes in the compiler output. @cristianoc could you have a look to check if everything is indeed fine?

cknitt avatar May 11 '25 14:05 cknitt

Open in StackBlitz

rescript

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

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7462
@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7462
@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7462
@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7462
@rescript/win32-x64

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

commit: 59debc7

pkg-pr-new[bot] avatar May 11 '25 15:05 pkg-pr-new[bot]

There's some behaviour change after this PR, which can be illustrated through array pop.

This pops an array with a single element and prints whether the resulting value is absent, or prints the value inside the option if present https://rescript-lang.org/try?version=v11.1.4&module=esmodule&code=DYUwLgBGIM5gCgewA4QLwQG7oHwCgIIYB3ASzAGMALCAKRgDoBBAJxYEMBPB5FACgDamALoBKCAG8CEAD4QAcogB2IXBADCymIlANgiAOZ8AROwBGMEErDHR0uQGVEAWxB8AHuLQ4NWnSD1DDztCAF88PE0lbV19I2MAHSUAKmSIFDBSZQAeUmsfVNs8aDgkZD5FFTsShH4nVz4AZlE7SL9YoMSUtIyspWzenLywHALkopqyvnq3SpAW4tha8pmmhaA

If we store in the array an element of option<int>, there's no difference before and after the PR. Notice in particular that popping [None] returns "absent", just like for []. So the presence of an element is not respected.

If we move to elements of option<option<int>>, popping [Some(None)] returns that a value is present, and the value is Some(None) before this PR, but None after this PR.

So the more legit case of storing a single optional value in the array does not behave in the expected way anyway. And the pretty weird case of storing double options behaves differently.

cristianoc avatar May 12 '25 08:05 cristianoc

@cristianoc Thanks a lot! Will rebase to get it to build again.

cknitt avatar May 20 '25 12:05 cknitt