Browse Source

set rethrow state before rethrowing

Simon Krajewski 1 year ago
parent
commit
d1d01fea4f

+ 1 - 7
src/coro/coroFromTexpr.ml

@@ -262,13 +262,7 @@ let expr_to_coro ctx (vresult,verror) cb_root e =
 			terminate cb (NextTry(cb_try,catches,cb_next)) e.etype e.epos;
 			cb_next,e_no_value
 		| TFunction tf ->
-			let rec f e = match e.eexpr with
-				| TConst TThis ->
-					replace_this e
-				| _ ->
-					Type.map_expr f e
-			in
-			cb,f e
+			cb,e
 	and ordered_loop cb el =
 		let close = start_ordered_value_list () in
 		let rec aux' cb acc el = match el with

+ 5 - 2
src/coro/coroToTexpr.ml

@@ -199,7 +199,10 @@ let block_to_texpr_coroutine ctx cb vcontinuation vresult verror p =
 			let _ = loop bb_next [esetexcstate (* TODO: test propagation after try/catch *)] exc_state_id_getter in
 			let try_state_id = loop bb_try [set_excstate new_exc_state_id] (fun () -> new_exc_state_id) in (* TODO: add test for nested try/catch *)
 			let catch_case =
-				let erethrow = mk (TThrow eerror) t_dynamic null_pos in
+				let erethrow = mk (TBlock [
+					set_state (get_rethrow_state_id ());
+					mk (TThrow eerror) t_dynamic null_pos
+				]) t_dynamic null_pos in
 				let eif =
 					List.fold_left (fun enext (vcatch,bb_catch) ->
 						let ecatchvar = mk (TVar (vcatch, Some eerror)) com.basic.tvoid null_pos in
@@ -212,7 +215,7 @@ let block_to_texpr_coroutine ctx cb vcontinuation vresult verror p =
 						| t ->
 							let etypecheck = std_is (make_local verror null_pos) vcatch.v_type in
 							mk (TIf (etypecheck, set_state catch_state_id, Some enext)) com.basic.tvoid null_pos
-					) erethrow catches
+					) erethrow (List.rev catches)
 				in
 				make_state new_exc_state_id [eif]
 			in

+ 8 - 8
tests/misc/coroutines/src/TestControlFlow.hx

@@ -96,12 +96,12 @@ class TestControlFlow extends utest.Test {
 		});
 	}
 
-	// function testTryCatchFail(async:Async) {
-	// 	tryCatch.start(new E3(), (result,error) -> {
-	// 		Assert.isOfType(error, E3);
-	// 		async.done();
-	// 	});
-	// }
+	function testTryCatchFail(async:Async) {
+		tryCatch.start(new E3(), (result,error) -> {
+			Assert.isOfType(error, E3);
+			async.done();
+		});
+	}
 
 	@:coroutine function tryCatch(e:haxe.Exception) {
 		try {
@@ -125,8 +125,8 @@ private class E1 extends haxe.Exception {
 }
 
 private class E2 extends haxe.Exception {
-	public function new() super("E1");
+	public function new() super("E2");
 }
 private class E3 extends haxe.Exception {
-	public function new() super("E1");
+	public function new() super("E3");
 }

+ 4 - 4
tests/misc/coroutines/src/yield/TestYieldTryCatch.hx

@@ -45,8 +45,6 @@ class TestYieldTryCatch extends BaseCase {
 		dummy += '6';
 	}
 
-	#if broken
-
 	public function testTryCatch_multiCatch() {
 		assert([10], tryCatch_multiCatch('Error'));
 		Assert.equals('12458', dummy);
@@ -74,6 +72,8 @@ class TestYieldTryCatch extends BaseCase {
 		dummy += '8';
 	}
 
+	#if broken
+
 	public function testTryCatch_nested() {
 		assert([10], tryCatch_nested(1));
 		Assert.equals('124569', dummy);
@@ -121,6 +121,8 @@ class TestYieldTryCatch extends BaseCase {
 		@:yield return 10;
 	}
 
+	#end
+
 	public function testTryCatch_exceptionNotCaught_thrownOutOfYieldContext() {
 		try {
 			assert([], tryCatchNotCaught());
@@ -147,8 +149,6 @@ class TestYieldTryCatch extends BaseCase {
 		dummy += '6';
 	}
 
-	#end
-
 	public function testTryCatch_captureVariable() {
 		assert([10], tryCatch_captureVariable());
 		Assert.equals('12456', dummy);

+ 4 - 1
tests/misc/coroutines/src/yield/Yield.hx

@@ -9,7 +9,10 @@ function sequence<T>(f:Coroutine<Yield<T>->Void>):Iterator<T> {
 
 	var nextStep = null;
 
-	function finish(_, _) {
+	function finish(_, err) {
+		if (err != null) {
+			throw err;
+		}
 		finished = true;
 	}