Skip to content
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 @@ -46,6 +46,7 @@
- Add (dev-)dependencies to build schema. https://github.com/rescript-lang/rescript/pull/7892
- Dedicated error for dict literal spreads. https://github.com/rescript-lang/rescript/pull/7901
- Dedicated error message for when mixing up `:` and `=` in various positions. https://github.com/rescript-lang/rescript/pull/7900
- Improve option optimization for constants. https://github.com/rescript-lang/rescript/pull/7913

# 12.0.0-beta.11

Expand Down
7 changes: 7 additions & 0 deletions compiler/core/lam_pass_remove_alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ let id_is_for_sure_true_in_boolean (tbl : Lam_stats.ident_tbl) id =
| None ->
Eval_unknown

let is_const_some (cst : Lam_constant.t) : bool =
match cst with
| Const_some _ -> true
| Const_block (_, (Lambda.Blk_some | Lambda.Blk_some_not_nested), _) -> true
| _ -> false

let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
let rec simpl (lam : Lam.t) : Lam.t =
match lam with
Expand Down Expand Up @@ -78,6 +84,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
((Lprim {primitive = Pis_not_none; args = [Lvar id]} as l1), l2, l3)
-> (
match Hash_ident.find_opt meta.ident_tbl id with
| Some (Constant c) when is_const_some c -> simpl l2
| Some (ImmutableBlock _ | MutableBlock _ | Normal_optional _) -> simpl l2
| Some (OptionalBlock (l, Null)) ->
Lam.if_
Expand Down
47 changes: 17 additions & 30 deletions tests/tests/src/gpr_3980_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,33 @@

import * as Js_math from "@rescript/runtime/lib/es6/Js_math.js";

if (1 !== 1) {
throw {
RE_EXN_ID: "Assert_failure",
_1: [
"gpr_3980_test.res",
15,
7
],
Error: new Error()
};
}

let match = 1;

if (match !== undefined) {
if (match !== 1) {
if (match !== 1) {
if (match !== 2) {
throw {
RE_EXN_ID: "Assert_failure",
_1: [
"gpr_3980_test.res",
15,
7
13,
9
],
Error: new Error()
};
}
let match$1 = 1;
if (match$1 !== 1) {
if (match$1 !== 2) {
throw {
RE_EXN_ID: "Assert_failure",
_1: [
"gpr_3980_test.res",
13,
9
],
Error: new Error()
};
}
Js_math.floor(1);
}

} else {
throw {
RE_EXN_ID: "Assert_failure",
_1: [
"gpr_3980_test.res",
15,
7
],
Error: new Error()
};
Js_math.floor(1);
}

/* Not a pure module */
11 changes: 11 additions & 0 deletions tests/tests/src/option_optimisation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,19 @@ function $$null(val1, val2) {
}
}

function constant() {
console.log(42);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Before this change:

function constant() {
  let opt = 42;
  if (opt !== undefined) {
    console.log(opt);
    return;
  }
  
}


function param(opt) {
let x = opt;
console.log(x);
}

export {
boolean,
$$null,
constant,
param,
}
/* No side effect */
15 changes: 15 additions & 0 deletions tests/tests/src/option_optimisation.res
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ let null = (~val1: Nullable.t<int>, ~val2: Nullable.t<int>) => {
| _ => "b"
}
}

let constant = () => {
let opt = Some(42)
switch opt {
| Some(x) => Console.log(x)
| None => ()
}
}

let param = (opt: int) => {
switch Some(opt) {
| Some(x) => Console.log(x)
| None => ()
}
}
59 changes: 11 additions & 48 deletions tests/tests/src/record_regression.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -99,66 +99,29 @@ pm0 = ir0.TAG === "V0" ? [

let pm1;

if (ir1.TAG === "V0") {
let x1 = "v1";
let x0 = "v0";
pm1 = x1 !== undefined ? [
x0,
x1,
3
] : [
x0,
"n/a",
3
];
} else {
pm1 = [
pm1 = ir1.TAG === "V0" ? [
"v0",
"v1",
3
] : [
"v0",
"n/a",
"v1"
];
}

let pm2;

if (ir2.TAG === "V0") {
let x1$1 = "v1";
let x0$1 = "v0";
if (x1$1 !== undefined) {
let x2 = 2;
pm2 = x2 !== undefined ? [
x0$1,
x1$1,
x2,
3
] : [
x0$1,
x1$1,
0,
3
];
} else {
let x2$1 = 2;
pm2 = x2$1 !== undefined ? [
x0$1,
"n/a",
x2$1,
3
] : [
x0$1,
"n/a",
0,
3
];
}
} else {
pm2 = [
pm2 = ir2.TAG === "V0" ? [
"v0",
"v1",
2,
3
] : [
"v0",
"n/a",
0,
"v1"
];
}

function inlinedRecord(ir) {
if (ir.TAG !== "V0") {
Expand Down
Loading