Bläddra i källkod

add final return again after dealing with field inits

closes #12067
Simon Krajewski 5 månader sedan
förälder
incheckning
9431666126

+ 1 - 0
src/filters/addFieldInits.ml

@@ -56,6 +56,7 @@ let add_field_inits cl_path locals com t =
 				(* This seems a bit expensive, but hopefully constructor expressions aren't that massive. *)
 				let e = RenameVars.run cl_path locals e in
 				let e = Optimizer.sanitize com e in
+				let e = if com.config.pf_add_final_return then AddFinalReturn.add_final_return e else e in
 				cf.cf_expr <- Some e
 			| _ ->
 				());

+ 39 - 0
src/filters/addFinalReturn.ml

@@ -0,0 +1,39 @@
+open Type
+
+(* Adds final returns to functions as required by some platforms *)
+let rec add_final_return e =
+	let rec loop e t =
+		let def_return p =
+			let c = (match follow t with
+				| TAbstract ({ a_path = [],"Int" },_) -> TInt 0l
+				| TAbstract ({ a_path = [],"Float" },_) -> TFloat "0."
+				| TAbstract ({ a_path = [],"Bool" },_) -> TBool false
+				| _ -> TNull
+			) in
+			{ eexpr = TReturn (Some { eexpr = TConst c; epos = p; etype = t }); etype = t_dynamic; epos = p }
+		in
+		match e.eexpr with
+		| TBlock el ->
+			(match List.rev el with
+			| [] -> e
+			| elast :: el ->
+				match loop elast t with
+				| { eexpr = TBlock el2 } -> { e with eexpr = TBlock ((List.rev el) @ el2) }
+				| elast -> { e with eexpr = TBlock (List.rev (elast :: el)) })
+		| TReturn _ ->
+			e
+		| _ ->
+			{ e with eexpr = TBlock [e;def_return e.epos] }
+	in
+
+	let e = Type.map_expr add_final_return e in
+
+	match e.eexpr with
+		| TFunction f ->
+			let f = (match follow f.tf_type with
+				| TAbstract ({ a_path = [],"Void" },[]) -> f
+				| _ -> { f with tf_expr = loop f.tf_expr f.tf_type }
+			) in
+			{ e with eexpr = TFunction f }
+		| _ ->
+			e

+ 1 - 38
src/filters/filters.ml

@@ -29,43 +29,6 @@ let get_native_name = Native.get_native_name
 
 (* PASS 1 begin *)
 
-(* Adds final returns to functions as required by some platforms *)
-let rec add_final_return e =
-	let rec loop e t =
-		let def_return p =
-			let c = (match follow t with
-				| TAbstract ({ a_path = [],"Int" },_) -> TInt 0l
-				| TAbstract ({ a_path = [],"Float" },_) -> TFloat "0."
-				| TAbstract ({ a_path = [],"Bool" },_) -> TBool false
-				| _ -> TNull
-			) in
-			{ eexpr = TReturn (Some { eexpr = TConst c; epos = p; etype = t }); etype = t_dynamic; epos = p }
-		in
-		match e.eexpr with
-		| TBlock el ->
-			(match List.rev el with
-			| [] -> e
-			| elast :: el ->
-				match loop elast t with
-				| { eexpr = TBlock el2 } -> { e with eexpr = TBlock ((List.rev el) @ el2) }
-				| elast -> { e with eexpr = TBlock (List.rev (elast :: el)) })
-		| TReturn _ ->
-			e
-		| _ ->
-			{ e with eexpr = TBlock [e;def_return e.epos] }
-	in
-
-	let e = Type.map_expr add_final_return e in
-
-	match e.eexpr with
-		| TFunction f ->
-			let f = (match follow f.tf_type with
-				| TAbstract ({ a_path = [],"Void" },[]) -> f
-				| _ -> { f with tf_expr = loop f.tf_expr f.tf_type }
-			) in
-			{ e with eexpr = TFunction f }
-		| _ -> e
-
 (* -------------------------------------------------------------------------- *)
 (* CHECK LOCAL VARS INIT *)
 
@@ -721,7 +684,7 @@ let run tctx ectx main before_destruction =
 	let locals = RenameVars.init com in
 	let filters = [
 		"sanitize",(fun _ e -> Optimizer.sanitize com e);
-		"add_final_return",(fun _ -> if com.config.pf_add_final_return then add_final_return else (fun e -> e));
+		"add_final_return",(fun _ -> if com.config.pf_add_final_return then AddFinalReturn.add_final_return else (fun e -> e));
 		"RenameVars",(match com.platform with
 		| Eval -> (fun _ e -> e)
 		| Jvm -> (fun _ e -> e)

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

@@ -13,7 +13,6 @@ class Issue10831 extends Test {
 	function test() {
 		switch (get()) {
 			case EnumTest(p):
-				trace(p);
 		}
 		Assert.pass();
 	}

+ 19 - 0
tests/unit/src/unit/issues/Issue12067.hx

@@ -0,0 +1,19 @@
+package unit.issues;
+
+@:keep
+private class PointerData<T> {
+	public var get:() -> T;
+
+	public function new(?get:Void->T) {
+		if (get == null)
+			get = () -> throw "null pointer dereference";
+	}
+
+	public var hasSet:Bool = false;
+}
+
+class Issue12067 extends Test {
+	function test() {
+		eq(false, new PointerData().hasSet);
+	}
+}