Skip to content

Commit 09f332b

Browse files
mediremianmonteiro
andcommitted
Fix option value truthiness optimisation
Co-authored-by: Antonio Nuno Monteiro <[email protected]>
1 parent 6859c8e commit 09f332b

File tree

5 files changed

+67
-23
lines changed

5 files changed

+67
-23
lines changed

compiler/core/lam_pass_remove_alias.ml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@
2424

2525
type outcome = Eval_false | Eval_true | Eval_unknown
2626

27-
let id_is_for_sure_true_in_boolean (tbl : Lam_stats.ident_tbl) id =
27+
let rec 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 _)
29+
| Some
30+
(Normal_optional
31+
( Lprim {loc = _; primitive = Psome_not_nest; args = [Lvar id']}
32+
| Lvar id' )) ->
33+
id_is_for_sure_true_in_boolean tbl id'
34+
| Some
35+
(Normal_optional
36+
(Lconst (Const_js_false | Const_js_null | Const_js_undefined _))) ->
37+
Eval_false
3038
| Some (Normal_optional _)
39+
| Some (ImmutableBlock _)
3140
| Some (MutableBlock _)
3241
| Some (Constant (Const_block _ | Const_js_true)) ->
3342
Eval_true

tests/tests/src/incorrect_option_optimisation.mjs

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/tests/src/incorrect_option_optimisation.res

Lines changed: 0 additions & 10 deletions
This file was deleted.
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)