Browse Source

handle return flow of while(true) (required for Java)

Simon Krajewski 13 years ago
parent
commit
8262fab5e6
3 changed files with 22 additions and 1 deletions
  1. 0 1
      std/haxe/Json.hx
  2. 13 0
      tests/unit/TestType.hx
  3. 9 0
      typeload.ml

+ 0 - 1
std/haxe/Json.hx

@@ -261,7 +261,6 @@ class Json {
 				invalidChar();
 				invalidChar();
 			}
 			}
 		}
 		}
-		return null;
 	}
 	}
 
 
 	function parseString() {
 	function parseString() {

+ 13 - 0
tests/unit/TestType.hx

@@ -368,4 +368,17 @@ class TestType extends Test {
 		eq(c.accDynamic, 3);
 		eq(c.accDynamic, 3);
 		exc(function() c.accFunc = 4);
 		exc(function() c.accFunc = 4);
 	}
 	}
+	
+	function testReturnFlow()
+	{
+		var l = function():String
+		{
+			while (true)
+			{
+				return "foo";
+			}
+			// some platforms may have to add an implicit return null here
+		}
+		eq(l(), "foo");
+	}
 }
 }

+ 9 - 0
typeload.ml

@@ -495,6 +495,15 @@ let rec return_flow ctx e =
 	| TTry (e,cases) ->
 	| TTry (e,cases) ->
 		return_flow e;
 		return_flow e;
 		List.iter (fun (_,e) -> return_flow e) cases;
 		List.iter (fun (_,e) -> return_flow e) cases;
+	| TWhile({eexpr = (TConst (TBool true))},e,_) ->
+		(* a special case for "inifite" while loops that have no break *)
+		let rec loop e = match e.eexpr with
+			(* ignore nested loops to not accidentally get one of its breaks *)
+			| TWhile _ | TFor _ -> ()
+			| TBreak -> error()
+			| _ -> Type.iter loop e
+		in
+		loop e
 	| _ ->
 	| _ ->
 		error()
 		error()