From 05fb0d28e18a0da0708eeda3b575fe907ba73f80 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Fri, 17 Nov 2023 17:01:11 +0100 Subject: [PATCH] [hl] don't optimize vars used inside nested try/catch --- src/generators/genhl.ml | 29 ++++++++++++++++++------- tests/unit/src/unit/issues/Issue9174.hx | 18 +++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 tests/unit/src/unit/issues/Issue9174.hx diff --git a/src/generators/genhl.ml b/src/generators/genhl.ml index 9b7b21e5665..f3daed21584 100644 --- a/src/generators/genhl.ml +++ b/src/generators/genhl.ml @@ -3084,11 +3084,21 @@ and gen_assign_op ctx acc e1 f = and build_capture_vars ctx f = let ignored_vars = ref PMap.empty in let used_vars = ref PMap.empty in + let depth = ref 0 in + (* get all captured vars in scope, ignore vars that are declared *) let decl_var v = - if has_var_flag v VCaptured then ignored_vars := PMap.add v.v_id () !ignored_vars + if has_var_flag v VCaptured then + ignored_vars := PMap.add v.v_id !depth !ignored_vars in let use_var v = + (* vars used in nested try/catch need to act as captured vars #9174 *) + let decl_depth = try PMap.find v.v_id !ignored_vars with Not_found -> !depth in + if decl_depth < !depth then begin + add_var_flag v VCaptured; + ignored_vars := PMap.remove v.v_id !ignored_vars; + end; + if has_var_flag v VCaptured then used_vars := PMap.add v.v_id v !used_vars in let rec loop e = @@ -3096,15 +3106,18 @@ and build_capture_vars ctx f = | TLocal v -> use_var v; | TVar (v,_) -> - decl_var v - | TTry (_,catches) -> - List.iter (fun (v,_) -> decl_var v) catches + decl_var v; | TFunction f -> List.iter (fun (v,_) -> decl_var v) f.tf_args; - | _ -> - () - ); - Type.iter loop e + | _ -> ()); + + (match e.eexpr with + | TTry (_,catches) -> + incr depth; + List.iter (fun (v,_) -> decl_var v) catches; + Type.iter loop e; + decr depth + | _ -> Type.iter loop e) in List.iter (fun (v,_) -> decl_var v) f.tf_args; loop f.tf_expr; diff --git a/tests/unit/src/unit/issues/Issue9174.hx b/tests/unit/src/unit/issues/Issue9174.hx new file mode 100644 index 00000000000..2d72a0c6c96 --- /dev/null +++ b/tests/unit/src/unit/issues/Issue9174.hx @@ -0,0 +1,18 @@ +package unit.issues; + +class Issue9174 extends unit.Test { + function test() { + var result = false; + try { + try { + throw ''; + } catch(e:String) { + result = true; + eq(true, result); + throw e; + } + } catch(e:String) { + eq(true, result); + } + } +}