Browse Source

don't generate TLocal twice for rvalue named functions
fixes #8359

Aleksandr Kuzmenko 6 years ago
parent
commit
410a442d49
2 changed files with 42 additions and 14 deletions
  1. 17 14
      src/typing/typer.ml
  2. 25 0
      tests/display/src/cases/Issue8359.hx

+ 17 - 14
src/typing/typer.ml

@@ -2038,7 +2038,7 @@ and type_local_function ctx name inline f with_type p =
 		tf_expr = e;
 	} in
 	let e = mk (TFunction tf) ft p in
-	(match v with
+	match v with
 	| None -> e
 	| Some v ->
 		Typeload.generate_value_meta ctx.com None (fun m -> v.v_meta <- m :: v.v_meta) f.f_args;
@@ -2050,21 +2050,24 @@ and type_local_function ctx name inline f with_type p =
 			| LocalUsage.Use _ | LocalUsage.Assign _ | LocalUsage.Declare _ -> ()
 		in
 		let is_rec = (try local_usage loop e; false with Exit -> true) in
-		let decl = (if is_rec then begin
-			if inline then display_error ctx "Inline function cannot be recursive" e.epos;
-			let el =
+		let exprs =
+			if with_type <> WithType.NoValue && not inline then [mk (TLocal v) v.v_type p]
+			else []
+		in
+		let exprs =
+			if is_rec then begin
+				if inline then display_error ctx "Inline function cannot be recursive" e.epos;
 				(mk (TVar (v,Some (mk (TConst TNull) ft p))) ctx.t.tvoid p) ::
 				(mk (TBinop (OpAssign,mk (TLocal v) ft p,e)) ft p) ::
-				(if with_type = WithType.NoValue then [] else [mk (TLocal v) ft p])
-			in
-			let e = mk (TBlock el) ft p in
-			{e with eexpr = TMeta((Meta.MergeBlock,[],null_pos),e)}
-		end else if inline && not ctx.com.display.dms_display then
-			mk (TBlock []) ctx.t.tvoid p (* do not add variable since it will be inlined *)
-		else
-			mk (TVar (v,Some e)) ctx.t.tvoid p
-		) in
-		if with_type <> WithType.NoValue && not inline then mk (TBlock [decl;mk (TLocal v) v.v_type p]) v.v_type p else decl)
+				exprs
+			end else if inline && not ctx.com.display.dms_display then
+				(mk (TBlock []) ctx.t.tvoid p) :: exprs (* do not add variable since it will be inlined *)
+			else
+				(mk (TVar (v,Some e)) ctx.t.tvoid p) :: exprs
+		in
+		match exprs with
+		| [e] -> e
+		| _ -> mk (TBlock exprs) v.v_type p
 
 and type_array_decl ctx el with_type p =
 	let tp = (match with_type with

+ 25 - 0
tests/display/src/cases/Issue8359.hx

@@ -0,0 +1,25 @@
+package cases;
+
+class Issue8359 extends DisplayTestCase {
+	/**
+		class Main {
+			static function main() {
+				var active = false;
+				var callback: ()->Void;
+				callback = function original(){
+					if( active ){ return; }
+					active = true;
+					callback = function(){
+						active = false;
+						callback = original;
+					};
+				};
+				callback();
+				callback();
+			}
+		}
+	**/
+	function test() {
+		arrayEq([], diagnostics());
+	}
+}