瀏覽代碼

[typer] disallow `return null` from Void-functions

closes #7198
Simon Krajewski 7 年之前
父節點
當前提交
0705b144e3
共有 2 個文件被更改,包括 5 次插入1 次删除
  1. 4 0
      src/typing/typer.ml
  2. 1 1
      tests/unit/src/unit/issues/Issue4312.hx

+ 4 - 0
src/typing/typer.ml

@@ -2152,6 +2152,10 @@ and type_return ctx e p =
 			let e = AbstractCast.cast_or_unify ctx ctx.ret e p in
 			begin match follow e.etype with
 			| TAbstract({a_path=[],"Void"},_) ->
+				begin match (Texpr.skip e).eexpr with
+				| TConst TNull -> error "Cannot return `null` from Void-function" p
+				| _ -> ()
+				end;
 				(* if we get a Void expression (e.g. from inlining) we don't want to return it (issue #4323) *)
 				mk (TBlock [
 					e;

+ 1 - 1
tests/unit/src/unit/issues/Issue4312.hx

@@ -3,7 +3,7 @@ package unit.issues;
 class Issue4312 extends Test {
    function test():Void {
 		var a = getValue();
-		if (!a) return null;
+		t(HelperMacros.typeError(if (!a) return null));
     }
 
 	static function getValue() {