Browse Source

[python] do not recurse into branches when determining initialized fields (closes #5053)

Simon Krajewski 9 years ago
parent
commit
f57fcdbbbe
2 changed files with 23 additions and 0 deletions
  1. 3 0
      src/generators/genpy.ml
  2. 20 0
      tests/unit/src/unit/issues/Issue5053.hx

+ 3 - 0
src/generators/genpy.ml

@@ -1918,6 +1918,9 @@ module Generator = struct
 							assigned_fields := cf :: !assigned_fields
 						| TConst (TSuper | TThis) | TThrow _ | TReturn _ ->
 							raise Exit
+						(* TODO: We could do some branch intersection stunts to make this more accurate. *)
+						| TIf(e1,_,_) | TSwitch(e1,_,_) | TWhile(e1,_,_) ->
+							loop e1
 						| _ ->
 							Type.iter loop e
 					in

+ 20 - 0
tests/unit/src/unit/issues/Issue5053.hx

@@ -0,0 +1,20 @@
+package unit.issues;
+
+private class C {
+	public var field:String;
+
+	public function new(b:Bool) {
+		if (b) {
+			field = "";
+		}
+	}
+}
+
+class Issue5053 extends unit.Test {
+	function test() {
+		var c = new C(true);
+		eq("", c.field);
+		var c2 = new C(false);
+		eq(null, c2.field);
+	}
+}