Skip to content

Fix option optimisation compiler bug #7766

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
merged 4 commits into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#### :bug: Bug fix

- Fix option optimisation that resulted in incorrect JS output. https://github.com/rescript-lang/rescript/pull/7766
- Fix formatting of nested records in `.resi` files. https://github.com/rescript-lang/rescript/pull/7741
- Don't format and don't check formatting of dependencies. https://github.com/rescript-lang/rescript/pull/7748
- Fix `rescript-editor-analysis semanticTokens` returning invalid JSON in certain cases. https://github.com/rescript-lang/rescript/pull/7750
Expand Down
13 changes: 11 additions & 2 deletions compiler/core/lam_pass_remove_alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@

type outcome = Eval_false | Eval_true | Eval_unknown

let id_is_for_sure_true_in_boolean (tbl : Lam_stats.ident_tbl) id =
let rec id_is_for_sure_true_in_boolean (tbl : Lam_stats.ident_tbl) id =
match Hash_ident.find_opt tbl id with
| Some (ImmutableBlock _)
| Some
(Normal_optional
( Lprim {loc = _; primitive = Psome_not_nest; args = [Lvar id']}
| Lvar id' )) ->
id_is_for_sure_true_in_boolean tbl id'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this case actively used in the fix?
Wondering how to make sure there's no infinite loop, whether such an invariant for tables is guaranteed to hold.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this case the boolean example function is not compiled correctly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then I don't understand the logic. It should be an optimization so omitting a case should not turn on more optimization and get a wrong compilation.
I guess the default case should be false not true.

Copy link
Collaborator

Choose a reason for hiding this comment

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

While Normal_optional below, the catch-all case, returns true.
I think it should return false by default, and under certain conditions return true.
So, removing this line should turn off a possibly desired optimization, instead of triggering an incorrect optimization.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've made the default case Eval_unknown (no optimisation), and we no longer have a potentially infinite loop 👍

| Some
(Normal_optional
(Lconst (Const_js_false | Const_js_null | Const_js_undefined _))) ->
Eval_false
| Some (Normal_optional _)
| Some (ImmutableBlock _)
| Some (MutableBlock _)
| Some (Constant (Const_block _ | Const_js_true)) ->
Eval_true
Expand Down
35 changes: 35 additions & 0 deletions tests/tests/src/option_optimisation.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";

function boolean(val1, val2) {
let a = val1;
let b = val2;
if (b || a) {
return "a";
} else {
return "b";
}
}

function $$null(val1, val2) {
let a = Primitive_option.some(val1);
let b = Primitive_option.some(val2);
let tmp = Primitive_option.valFromOption(b);
if (tmp !== null && tmp !== undefined) {
return "a";
}
tmp === null;
let tmp$1 = Primitive_option.valFromOption(a);
if (tmp$1 == null) {
return "b";
} else {
return "a";
}
}

export {
boolean,
$$null,
}
/* No side effect */
21 changes: 21 additions & 0 deletions tests/tests/src/option_optimisation.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let boolean = (~val1: bool, ~val2: bool) => {
let a = Some(val1)
let b = Some(val2)

switch (a, b) {
| (_, Some(true))
| (Some(true), _) => "a"
| _ => "b"
}
}

let null = (~val1: Nullable.t<int>, ~val2: Nullable.t<int>) => {
let a = Some(val1)
let b = Some(val2)

switch (a, b) {
| (_, Some(Value(_)))
| (Some(Value(_)), _) => "a"
| _ => "b"
}
}
Loading