瀏覽代碼

accept final throw as return (closes #1923)

Simon Krajewski 12 年之前
父節點
當前提交
25030ce23b
共有 2 個文件被更改,包括 13 次插入1 次删除
  1. 8 0
      tests/unit/TestMisc.hx
  2. 5 1
      typeload.ml

+ 8 - 0
tests/unit/TestMisc.hx

@@ -606,4 +606,12 @@ class TestMisc extends Test {
 		eq(b.i, -6);
 		eq(b.b, true);
 	}
+	
+	function testNoReturnThrow() {
+		function test():String {
+			throw "never call me";
+		};
+		var s = try test() catch(e:String) e;
+		eq(s,"never call me");
+	}
 }

+ 5 - 1
typeload.ml

@@ -1062,7 +1062,11 @@ let type_function ctx args ret fmode f do_display p =
 	let have_ret = (try loop e; false with Exit -> true) in
 	if have_ret then
 		(try return_flow ctx e with Exit -> ())
-	else (try type_eq EqStrict ret ctx.t.tvoid with Unify_error _ -> display_error ctx ("Missing return " ^ (s_type (print_context()) ret)) p);
+	else (try type_eq EqStrict ret ctx.t.tvoid with Unify_error _ ->
+		match e.eexpr with
+		(* accept final throw (issue #1923) *)
+		| TBlock el when (match List.rev el with ({eexpr = TThrow _} :: _) -> true | _ -> false) -> ()
+		| _ -> display_error ctx ("Missing return " ^ (s_type (print_context()) ret)) p);
 	let rec loop e =
 		match e.eexpr with
 		| TCall ({ eexpr = TConst TSuper },_) -> raise Exit