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 all 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: 7 additions & 6 deletions compiler/core/lam_pass_remove_alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ type outcome = Eval_false | Eval_true | Eval_unknown

let 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 _)
| Some (MutableBlock _)
| Some (Constant (Const_block _ | Const_js_true)) ->
Eval_true
| Some
(Normal_optional
(Lconst (Const_js_false | Const_js_null | Const_js_undefined _))) ->
Eval_false
| Some (Constant Const_js_true) -> Eval_true
| Some (Constant (Const_int {i})) -> if i = 0l then Eval_false else Eval_true
| Some (Constant (Const_js_false | Const_js_null | Const_js_undefined _)) ->
Eval_false
| Some
( Constant _ | Module _ | FunctionId _ | Exception | Parameter | NA
( Normal_optional _ | ImmutableBlock _ | MutableBlock _ | Constant _
| Module _ | FunctionId _ | Exception | Parameter | NA
| OptionalBlock (_, (Undefined | Null | Null_undefined)) )
| None ->
Eval_unknown
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