Skip to content
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

Fix bug where a ref assignment is moved ouside a conditional #7176

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

# 12.0.0-alpha.6 (Unreleased)

#### :bug: Bug fix
- Fix bug where a ref assignment is moved ouside a conditional. https://github.com/rescript-lang/rescript/pull/7176

# 12.0.0-alpha.5

#### :rocket: New Feature
Expand Down
2 changes: 1 addition & 1 deletion compiler/core/js_analyzer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

val free_variables_of_statement : J.statement -> Set_ident.t

val free_variables_of_expression : J.finish_ident_expression -> Set_ident.t
val free_variables_of_expression : J.expression -> Set_ident.t

(* val no_side_effect_expression_desc :
J.expression_desc -> bool *)
Expand Down
10 changes: 10 additions & 0 deletions compiler/core/js_stmt_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ let if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) : t =
aux ?comment (E.or_ e (E.not pred1)) ifso ifso1
| ifso1 :: ifso_rest, ifnot1 :: ifnot_rest
when Js_analyzer.eq_statement ifnot1 ifso1
&& (match ifso1.statement_desc with
| Exp
{
expression_desc =
Bin (Eq, {expression_desc = Var (Id v)}, _);
_;
} ->
let guard_vars = Js_analyzer.free_variables_of_expression e in
not (Set_ident.mem guard_vars v)
| _ -> true)
&& Js_analyzer.no_side_effect_expression e ->
(* here we do agressive optimization, because it can help optimization later,
move code outside of branch is generally helpful later
Expand Down
29 changes: 29 additions & 0 deletions tests/tests/src/move_ref_assignment.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


let j = 0;

let k = 1;

if (j === 0) {
j = j + 2 | 0;
console.log("j.c");
} else {
j = j + 2 | 0;
}

j = j + 2 | 0;

if (k === 0) {
console.log("k.c");
}

let j$1 = 0;

let k$1 = 0;

export {
j$1 as j,
k$1 as k,
}
/* Not a pure module */
21 changes: 21 additions & 0 deletions tests/tests/src/move_ref_assignment.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type t = {mutable c: int}

let j = {c: 0}
let k = {c: 1}

if j.c == 0 {
j.c = j.c + 2
Console.log("j.c")
} else {
j.c = j.c + 2
}

if k.c == 0 {
j.c = j.c + 2
Console.log("k.c")
} else {
j.c = j.c + 2
}

let j = 0
let k = 0