Explorar o código

[js][es6] fix catch var scoping in renameVars filter (closes #9413)

Aleksandr Kuzmenko %!s(int64=5) %!d(string=hai) anos
pai
achega
212bae7e95
Modificáronse 2 ficheiros con 25 adicións e 2 borrados
  1. 7 2
      src/filters/renameVars.ml
  2. 18 0
      tests/unit/src/unit/issues/Issue9413.hx

+ 7 - 2
src/filters/renameVars.ml

@@ -217,8 +217,13 @@ let rec collect_vars ?(in_block=false) rc scope e =
 	| TTry (try_expr, catches) ->
 		collect_vars scope try_expr;
 		List.iter (fun (v, catch_expr) ->
-			declare_var rc scope v;
-			collect_vars scope catch_expr
+			let v_expr = mk (TVar (v,None)) t_dynamic v.v_pos in
+			let e =
+				match catch_expr.eexpr with
+				| TBlock exprs -> { catch_expr with eexpr = TBlock (v_expr :: exprs) }
+				| _ -> { catch_expr with eexpr = TBlock [v_expr; catch_expr] }
+			in
+			collect_vars scope e
 		) catches
 	| TSwitch (target, cases, default_opt) when rc.rc_switch_cases_no_blocks ->
 		collect_vars scope target;

+ 18 - 0
tests/unit/src/unit/issues/Issue9413.hx

@@ -0,0 +1,18 @@
+package unit.issues;
+
+class Issue9413 extends unit.Test {
+	function test() {
+		var size = 100;
+
+		try {
+			noop();
+		} catch (e) {
+			for (i in 0...size) {
+				noop();
+			}
+		}
+		noAssert();
+	}
+
+	@:pure(false) static function noop() {}
+}