Skip to content

Remove @return(undefined_to_opt) and %undefined_to_opt primitive - #7462

Merged
cknitt merged 4 commits into
rescript-lang:masterfrom
cknitt:remove-return-undefined-to-opt
Jun 19, 2025
Merged

cknitt merged 4 commits into
rescript-lang:masterfrom
cknitt:remove-return-undefined-to-opt

Conversation

@cknitt

@cknitt cknitt commented May 11, 2025 •

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

@pkg-pr-new

pkg-pr-new Bot commented May 11, 2025 •

Copy link
Copy Markdown

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

@cknitt
cknitt requested a review from cristianoc May 11, 2025 17:24
@cristianoc

Copy link
Copy Markdown
Collaborator

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.

@cknitt
cknitt force-pushed the remove-return-undefined-to-opt branch from 2eddf54 to fbb3a5c Compare May 19, 2025 13:31

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

One more round on this.
We do want to simplify the compiler, yet avoid to change observable behaviour.
In particular, the conversion from undefined to option should have the expected semantics: x => (x === undefined) ? None : (Some(x))

Will add a small commit on top to address the changes.

Comment thread compiler/core/lam.ml
| Return_null_to_opt -> prim ~primitive:Pnull_to_opt ~args:[result] loc
| Return_null_undefined_to_opt ->
prim ~primitive:Pnull_undefined_to_opt ~args:[result] loc
| Return_undefined_to_opt ->

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.

Removing @return(undefined_to_opt) sounds good, as it does not really have a clear use case.

Comment thread compiler/core/lam.ml
Lprim
{
primitive =
( Pundefined_to_opt | Pnull_to_opt | Pnull_undefined_to_opt

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.

Removing the primitive to convert undefined to option makes sense, as the undefined type is de-emphasized (no Stdlib_undefined module), so it would be weird to special case this conversion in the compiler.


module A = Belt_Array
external toOpt: opt<'a> => option<'a> = "%undefined_to_opt"
external toOpt: opt<'a> => option<'a> = "%identity"

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.

This change is fine as it is internal to Belt, and produces identical code.

Comment thread runtime/Js.res Outdated

external toOption: nullable<'a> => option<'a> = "%nullable_to_opt"
external undefinedToOption: undefined<'a> => option<'a> = "%undefined_to_opt"
external undefinedToOption: undefined<'a> => option<'a> = "%identity"

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.

This is an observable behaviour change, need to use an existing conversion function (now that the primitive does not exist anymore).

Comment thread runtime/Js_array.res Outdated
```
*/
@send
@return(undefined_to_opt)

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.

These are fine, as a conversion from undefined to optional seems arbitrary here.

Comment thread runtime/Js_undefined.res Outdated

external to_opt: t<'a> => option<'a> = "%undefined_to_opt"
external toOption: t<'a> => option<'a> = "%undefined_to_opt"
external to_opt: t<'a> => option<'a> = "%identity"

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.

Behavioural change exposed to the user, need to preserve original semantics.

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

Added conversion fix on top. Good to go for me.

@cknitt

cknitt commented May 20, 2025

Copy link
Copy Markdown
Member Author

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

@cknitt
cknitt force-pushed the remove-return-undefined-to-opt branch 2 times, most recently from c46839d to 96d12d0 Compare May 21, 2025 15:22
@cristianoc
cristianoc requested a review from Copilot June 13, 2025 11:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR removes the legacy undefined_to_opt functionality that produced incorrect JavaScript output and was never documented, as well as its associated patterns across the codebase. Key changes include:

  • Removing the %undefined_to_opt primitive from the compiler’s internal tables and AST.
  • Eliminating the handling of undefined_to_opt from external FFI types and attribute processing.
  • Adjusting JavaScript output (Js.js) to replace the removed primitive with undefinedToOption based on Primitive_option.fromUndefined.

Reviewed Changes

Copilot reviewed 43 out of 43 changed files in this pull request and generated no comments.

Show a summary per file
File Description
lib/es6/Js.js Adds export of undefinedToOption in place of the removed primitive.
compiler/ml/translcore.ml Removes %undefined_to_opt from the primitives table.
compiler/ml/printlambda.ml & others Removes references to Pundefined_to_opt from various modules.
compiler/frontend/* Deletes undefined_to_opt handling in FFI types and attributes.
compiler/core/* Cleans up pattern matching and conversion logic for undefined_to_opt.
Comments suppressed due to low confidence (3)

lib/es6/Js.js:71

  • [nitpick] Consider reviewing the naming of 'undefinedToOption' to ensure consistency with other exported functions in this module.
let undefinedToOption = Primitive_option.fromUndefined;

compiler/frontend/ast_external_process.ml:192

  • Ensure that there are tests in place to validate the external API behavior now that 'undefined_to_opt' has been removed.
match txt with

compiler/core/lam_compile_primitive.ml:95

  • Double-check that the removal of the Pundefined_to_opt branch does not affect downstream logic or error handling, and update tests accordingly.
Removed branch for Pundefined_to_opt

@cknitt
cknitt force-pushed the remove-return-undefined-to-opt branch from 96d12d0 to 79a872d Compare June 19, 2025 08:46
@cknitt
cknitt force-pushed the remove-return-undefined-to-opt branch from ee8dcf1 to 59debc7 Compare June 19, 2025 16:58
@cknitt
cknitt merged commit 2c0a4eb into rescript-lang:master Jun 19, 2025
@cknitt
cknitt deleted the remove-return-undefined-to-opt branch June 19, 2025 19:27
aspeddro pushed a commit to aspeddro/rescript-compiler that referenced this pull request Oct 31, 2026
…cript-lang#7462)

* Remove @return(undefined_to_opt) and %undefined_to_opt primitive

* Respect conversion semantics from undefined to option.

* Fix rewatch test

* CHANGELOG

---------

Co-authored-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
Signed-off-by: Pedro Castro <aspeddro@gmail.com>
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.

3 participants