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 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
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
17 changes: 2 additions & 15 deletions compiler/core/js_stmt_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,6 @@ let rec block_last_is_return_throw_or_continue (x : J.block) =
*)
let if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) : t =
let declared = ref false in
let common_prefix_blocks = ref [] in
let add_prefix b = common_prefix_blocks := b :: !common_prefix_blocks in
let rec aux ?comment (e : J.expression) (ifso : J.block) (ifnot : J.block) : t
=
match (e.expression_desc, ifnot) with
Expand Down Expand Up @@ -301,14 +299,6 @@ let if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) : t =
| _, [{statement_desc = If (pred1, ifso1, ifnot1)}]
when Js_analyzer.eq_block ifso ifnot1 ->
aux ?comment (E.or_ e (E.not pred1)) ifso ifso1
| ifso1 :: ifso_rest, ifnot1 :: ifnot_rest
when Js_analyzer.eq_statement ifnot1 ifso1
&& 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
*)
add_prefix ifso1;
aux ?comment e ifso_rest ifnot_rest
| _ -> {statement_desc = If (e, ifso, ifnot); comment})
in
let if_block =
Expand All @@ -317,12 +307,9 @@ let if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) : t =
| None -> []
| Some v -> v)
in
let prefix = !common_prefix_blocks in
match (!declared, declaration) with
| true, _ | _, None ->
if prefix = [] then if_block else block (List.rev_append prefix [if_block])
| false, Some (kind, id) ->
block (declare_variable ~kind id :: List.rev_append prefix [if_block])
| true, _ | _, None -> if_block
| false, Some (kind, id) -> block (declare_variable ~kind id :: [if_block])

let assign ?comment id e : t =
{statement_desc = J.Exp (E.assign (E.var id) e); comment}
Expand Down
33 changes: 33 additions & 0 deletions tests/tests/src/move_ref_assignment.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


let j = 1;

let k = {
c: 1
};

function upd() {
k.c = 3;
}

if (k.c === 1) {
upd();
j = j + 2 | 0;
console.log("correct");
} else {
upd();
j = j + 2 | 0;
console.log("incorrect");
}

let j$1 = 0;

let k$1 = 0;

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

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

let upd = () => k.c = 3

if k.c == 1 {
upd()
j.c = j.c + 2
Console.log("correct")
} else {
upd()
j.c = j.c + 2
Console.log("incorrect")
}

let j = 0
let k = 0
12 changes: 9 additions & 3 deletions tests/tests/src/test_ramification.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ function f(x) {
function f2(x) {
let v = 0;
let y;
v = 1;
if (x.TAG === "A") {
v = 1;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the only case where the optimisation was kicking.
It's pretty unlikely to fire to begin with, and when it fires the cases where one can legitimately move the common instruction up are pretty rare.
In this case, it would be OK because assigning to v does not change the value of x.TAG, because the two values cannot be aliases.

let z = 33;
y = z + 3 | 0;
} else {
v = 1;
let z$1 = 33;
y = z$1 + 4 | 0;
}
Expand All @@ -47,8 +48,13 @@ function f2(x) {
function f3(x) {
let v = 0;
let y;
v = 1;
y = x.TAG === "A" ? 3 : 4;
if (x.TAG === "A") {
v = 1;
y = 3;
} else {
v = 1;
y = 4;
}
return y + 32 | 0;
}

Expand Down