From ccf3e83f327fe3e78d08a3fd82abbd16da397dd9 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Mon, 28 Nov 2022 11:46:54 +0100 Subject: [PATCH] Fix two more cases of async inline. Fixes https://github.com/rescript-lang/rescript-compiler/issues/5857 --- jscomp/core/js_pass_tailcall_inline.ml | 2 +- jscomp/core/lam_pass_remove_alias.ml | 8 ++++++-- jscomp/test/async_inline.js | 16 ++++++++-------- jscomp/test/async_inline.res | 12 +++++++++++- lib/4.06.1/unstable/js_compiler.ml | 10 +++++++--- lib/4.06.1/unstable/js_playground_compiler.ml | 10 +++++++--- lib/4.06.1/whole_compiler.ml | 10 +++++++--- 7 files changed, 47 insertions(+), 21 deletions(-) diff --git a/jscomp/core/js_pass_tailcall_inline.ml b/jscomp/core/js_pass_tailcall_inline.ml index 3ae42a5081..d3e003c109 100644 --- a/jscomp/core/js_pass_tailcall_inline.ml +++ b/jscomp/core/js_pass_tailcall_inline.ml @@ -200,7 +200,7 @@ let subst (export_set : Set_ident.t) stats = Call ( { expression_desc = - Fun {is_method=false; params; body; env}; + Fun {is_method=false; params; body; env; async=false}; }, args, _info ); diff --git a/jscomp/core/lam_pass_remove_alias.ml b/jscomp/core/lam_pass_remove_alias.ml index fe459f6bfc..33bceda1ff 100644 --- a/jscomp/core/lam_pass_remove_alias.ml +++ b/jscomp/core/lam_pass_remove_alias.ml @@ -171,7 +171,10 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t = ap_info; } -> ( match Lam_compile_env.query_external_id_info ident fld_name with - | { persistent_closed_lambda = Some (Lfunction { params; body; _ }) } + | { + persistent_closed_lambda = + Some (Lfunction ({ params; body } as lfunction)); + } (* be more cautious when do cross module inlining *) when Ext_list.same_length params args && Ext_list.for_all args (fun arg -> @@ -180,7 +183,8 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t = match Hash_ident.find_opt meta.ident_tbl p with | Some v -> v <> Parameter | None -> true) - | _ -> true) -> + | _ -> true) + && Lam_analysis.lfunction_can_be_beta_reduced lfunction -> simpl (Lam_beta_reduce.propagate_beta_reduce meta params body args) | _ -> Lam.apply (simpl l1) (Ext_list.map args simpl) ap_info) (* Function inlining interact with other optimizations... diff --git a/jscomp/test/async_inline.js b/jscomp/test/async_inline.js index 150cc10a3f..1b9ede4d21 100644 --- a/jscomp/test/async_inline.js +++ b/jscomp/test/async_inline.js @@ -8,13 +8,6 @@ async function willBeInlined(param) { var inlined = willBeInlined(undefined); -function wrapSomethingAsync(param) { - ((async function (param) { - var test = await Promise.resolve("Test"); - console.log(test); - })(777)); -} - async function doSomethingAsync(someAsyncFunction) { return await Curry._1(someAsyncFunction, undefined); } @@ -53,11 +46,17 @@ var tui = 3; var tuia = uncurriedIdAsync(3); +function wrapSomethingAsync(param) { + ((async function (param) { + var test = await Promise.resolve("Test"); + console.log(test); + })(undefined)); +} + var tci = 3; exports.willBeInlined = willBeInlined; exports.inlined = inlined; -exports.wrapSomethingAsync = wrapSomethingAsync; exports.M = M; exports.broken = broken$2; exports.curriedId = curriedId; @@ -68,4 +67,5 @@ exports.tci = tci; exports.tcia = tcia; exports.tui = tui; exports.tuia = tuia; +exports.wrapSomethingAsync = wrapSomethingAsync; /* inlined Not a pure module */ diff --git a/jscomp/test/async_inline.res b/jscomp/test/async_inline.res index 98c6ba14d3..915fde0787 100644 --- a/jscomp/test/async_inline.res +++ b/jscomp/test/async_inline.res @@ -35,4 +35,14 @@ let uncurriedIdAsync = async (.x ) => x let tci = curriedId(3) let tcia = curriedIdAsync(3) let tui = uncurriedId(. 3) -let tuia = uncurriedIdAsync(. 3) \ No newline at end of file +let tuia = uncurriedIdAsync(. 3) + +external ignorePromise: promise<'a> => unit = "%identity" + +let wrapSomethingAsync = () => + ( + async () => { + let test = await Js.Promise.resolve("Test") + Js.log(test) + } + )()->ignorePromise diff --git a/lib/4.06.1/unstable/js_compiler.ml b/lib/4.06.1/unstable/js_compiler.ml index ff07ed091d..f5d3a90a8c 100644 --- a/lib/4.06.1/unstable/js_compiler.ml +++ b/lib/4.06.1/unstable/js_compiler.ml @@ -92837,7 +92837,7 @@ let subst (export_set : Set_ident.t) stats = Call ( { expression_desc = - Fun {is_method=false; params; body; env}; + Fun {is_method=false; params; body; env; async=false}; }, args, _info ); @@ -139496,7 +139496,10 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t = ap_info; } -> ( match Lam_compile_env.query_external_id_info ident fld_name with - | { persistent_closed_lambda = Some (Lfunction { params; body; _ }) } + | { + persistent_closed_lambda = + Some (Lfunction ({ params; body } as lfunction)); + } (* be more cautious when do cross module inlining *) when Ext_list.same_length params args && Ext_list.for_all args (fun arg -> @@ -139505,7 +139508,8 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t = match Hash_ident.find_opt meta.ident_tbl p with | Some v -> v <> Parameter | None -> true) - | _ -> true) -> + | _ -> true) + && Lam_analysis.lfunction_can_be_beta_reduced lfunction -> simpl (Lam_beta_reduce.propagate_beta_reduce meta params body args) | _ -> Lam.apply (simpl l1) (Ext_list.map args simpl) ap_info) (* Function inlining interact with other optimizations... diff --git a/lib/4.06.1/unstable/js_playground_compiler.ml b/lib/4.06.1/unstable/js_playground_compiler.ml index a063381456..9a5cc506de 100644 --- a/lib/4.06.1/unstable/js_playground_compiler.ml +++ b/lib/4.06.1/unstable/js_playground_compiler.ml @@ -92837,7 +92837,7 @@ let subst (export_set : Set_ident.t) stats = Call ( { expression_desc = - Fun {is_method=false; params; body; env}; + Fun {is_method=false; params; body; env; async=false}; }, args, _info ); @@ -139496,7 +139496,10 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t = ap_info; } -> ( match Lam_compile_env.query_external_id_info ident fld_name with - | { persistent_closed_lambda = Some (Lfunction { params; body; _ }) } + | { + persistent_closed_lambda = + Some (Lfunction ({ params; body } as lfunction)); + } (* be more cautious when do cross module inlining *) when Ext_list.same_length params args && Ext_list.for_all args (fun arg -> @@ -139505,7 +139508,8 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t = match Hash_ident.find_opt meta.ident_tbl p with | Some v -> v <> Parameter | None -> true) - | _ -> true) -> + | _ -> true) + && Lam_analysis.lfunction_can_be_beta_reduced lfunction -> simpl (Lam_beta_reduce.propagate_beta_reduce meta params body args) | _ -> Lam.apply (simpl l1) (Ext_list.map args simpl) ap_info) (* Function inlining interact with other optimizations... diff --git a/lib/4.06.1/whole_compiler.ml b/lib/4.06.1/whole_compiler.ml index 80bcd4b2dd..b1955c230d 100644 --- a/lib/4.06.1/whole_compiler.ml +++ b/lib/4.06.1/whole_compiler.ml @@ -141231,7 +141231,7 @@ let subst (export_set : Set_ident.t) stats = Call ( { expression_desc = - Fun {is_method=false; params; body; env}; + Fun {is_method=false; params; body; env; async=false}; }, args, _info ); @@ -154666,7 +154666,10 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t = ap_info; } -> ( match Lam_compile_env.query_external_id_info ident fld_name with - | { persistent_closed_lambda = Some (Lfunction { params; body; _ }) } + | { + persistent_closed_lambda = + Some (Lfunction ({ params; body } as lfunction)); + } (* be more cautious when do cross module inlining *) when Ext_list.same_length params args && Ext_list.for_all args (fun arg -> @@ -154675,7 +154678,8 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t = match Hash_ident.find_opt meta.ident_tbl p with | Some v -> v <> Parameter | None -> true) - | _ -> true) -> + | _ -> true) + && Lam_analysis.lfunction_can_be_beta_reduced lfunction -> simpl (Lam_beta_reduce.propagate_beta_reduce meta params body args) | _ -> Lam.apply (simpl l1) (Ext_list.map args simpl) ap_info) (* Function inlining interact with other optimizations...