Pārlūkot izejas kodu

fixed checks for uninitialized vars with return/continue/throw/break (closes #10219)

Aleksandr Kuzmenko 4 gadi atpakaļ
vecāks
revīzija
195d0453e8

+ 6 - 2
src/filters/filters.ml

@@ -84,6 +84,10 @@ let check_local_vars_init com e =
 	in
 	let declared = ref [] in
 	let outside_vars = ref IntMap.empty in
+	(* Set variables which belong to current function *)
+	let set_all_vars vars =
+		vars := PMap.mapi (fun id is_set -> if IntMap.mem id !outside_vars then is_set else true) !vars
+	in
 	let rec loop vars e =
 		match e.eexpr with
 		| TLocal v ->
@@ -174,10 +178,10 @@ let check_local_vars_init com e =
 				join vars cvars)
 		(* mark all reachable vars as initialized, since we don't exit the block  *)
 		| TBreak | TContinue | TReturn None ->
-			vars := PMap.map (fun _ -> true) !vars
+			set_all_vars vars
 		| TThrow e | TReturn (Some e) ->
 			loop vars e;
-			vars := PMap.map (fun _ -> true) !vars
+			set_all_vars vars
 		| TFunction tf ->
 			let old = !outside_vars in
 			(* Mark all known variables as "outside" so we can ignore their initialization state within the function.

+ 9 - 0
tests/misc/projects/Issue10219/Main.hx

@@ -0,0 +1,9 @@
+function main() {
+	var x:Int;
+	if (Math.random() < 0.5) {
+		x = 3;
+	} else {
+		var b = () -> 3;
+	}
+	trace(x);
+}

+ 1 - 0
tests/misc/projects/Issue10219/compile-fail.hxml

@@ -0,0 +1 @@
+--main Main

+ 1 - 0
tests/misc/projects/Issue10219/compile-fail.hxml.stderr

@@ -0,0 +1 @@
+Main.hx:8: characters 8-9 : Local variable x used without being initialized

+ 1 - 0
tests/misc/projects/Issue7447/Main.hx

@@ -8,5 +8,6 @@ class Main {
 abstract Abstr(Int) {
 	public inline function new() {
 		trace(() -> this);
+		this = 0;
 	}
 }

+ 1 - 0
tests/unit/src/unit/issues/Issue4327.hx

@@ -3,6 +3,7 @@ package unit.issues;
 @:callable
 abstract Example(()->String) {
 	public function new() {
+		this = null;
 		this = fun;
 	}