Skip to content

Commit e92879e

Browse files
mediremianmonteiro
andauthored
Fix option optimisation compiler bug (#7766)
* Add failing test for option optimisation * Fix option value truthiness optimisation Co-authored-by: Antonio Nuno Monteiro <[email protected]> * Add CHANGELOG entry * Simplify boolean optimisation --------- Co-authored-by: Antonio Nuno Monteiro <[email protected]>
1 parent 2cd9996 commit e92879e

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
#### :bug: Bug fix
4343

44+
- Fix option optimisation that resulted in incorrect JS output. https://github.com/rescript-lang/rescript/pull/7766
4445
- Fix formatting of nested records in `.resi` files. https://github.com/rescript-lang/rescript/pull/7741
4546
- Don't format and don't check formatting of dependencies. https://github.com/rescript-lang/rescript/pull/7748
4647
- Fix `rescript-editor-analysis semanticTokens` returning invalid JSON in certain cases. https://github.com/rescript-lang/rescript/pull/7750

compiler/core/lam_pass_remove_alias.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ type outcome = Eval_false | Eval_true | Eval_unknown
2626

2727
let id_is_for_sure_true_in_boolean (tbl : Lam_stats.ident_tbl) id =
2828
match Hash_ident.find_opt tbl id with
29-
| Some (ImmutableBlock _)
30-
| Some (Normal_optional _)
31-
| Some (MutableBlock _)
32-
| Some (Constant (Const_block _ | Const_js_true)) ->
33-
Eval_true
29+
| Some
30+
(Normal_optional
31+
(Lconst (Const_js_false | Const_js_null | Const_js_undefined _))) ->
32+
Eval_false
33+
| Some (Constant Const_js_true) -> Eval_true
3434
| Some (Constant (Const_int {i})) -> if i = 0l then Eval_false else Eval_true
3535
| Some (Constant (Const_js_false | Const_js_null | Const_js_undefined _)) ->
3636
Eval_false
3737
| Some
38-
( Constant _ | Module _ | FunctionId _ | Exception | Parameter | NA
38+
( Normal_optional _ | ImmutableBlock _ | MutableBlock _ | Constant _
39+
| Module _ | FunctionId _ | Exception | Parameter | NA
3940
| OptionalBlock (_, (Undefined | Null | Null_undefined)) )
4041
| None ->
4142
Eval_unknown
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";
4+
5+
function boolean(val1, val2) {
6+
let a = val1;
7+
let b = val2;
8+
if (b || a) {
9+
return "a";
10+
} else {
11+
return "b";
12+
}
13+
}
14+
15+
function $$null(val1, val2) {
16+
let a = Primitive_option.some(val1);
17+
let b = Primitive_option.some(val2);
18+
let tmp = Primitive_option.valFromOption(b);
19+
if (tmp !== null && tmp !== undefined) {
20+
return "a";
21+
}
22+
tmp === null;
23+
let tmp$1 = Primitive_option.valFromOption(a);
24+
if (tmp$1 == null) {
25+
return "b";
26+
} else {
27+
return "a";
28+
}
29+
}
30+
31+
export {
32+
boolean,
33+
$$null,
34+
}
35+
/* No side effect */
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let boolean = (~val1: bool, ~val2: bool) => {
2+
let a = Some(val1)
3+
let b = Some(val2)
4+
5+
switch (a, b) {
6+
| (_, Some(true))
7+
| (Some(true), _) => "a"
8+
| _ => "b"
9+
}
10+
}
11+
12+
let null = (~val1: Nullable.t<int>, ~val2: Nullable.t<int>) => {
13+
let a = Some(val1)
14+
let b = Some(val2)
15+
16+
switch (a, b) {
17+
| (_, Some(Value(_)))
18+
| (Some(Value(_)), _) => "a"
19+
| _ => "b"
20+
}
21+
}

0 commit comments

Comments
 (0)