瀏覽代碼

remove redundant final return in void functions (closes #6420)

Aleksandr Kuzmenko 5 年之前
父節點
當前提交
3352cab1d3
共有 2 個文件被更改,包括 18 次插入1 次删除
  1. 7 1
      src/optimization/optimizer.ml
  2. 11 0
      tests/optimization/src/TestJs.hx

+ 7 - 1
src/optimization/optimizer.ml

@@ -160,7 +160,13 @@ let sanitize_expr com e =
 		{ e with eexpr = TFor (v,e1,e2) }
 		{ e with eexpr = TFor (v,e1,e2) }
 	| TFunction f ->
 	| TFunction f ->
 		let f = (match f.tf_expr.eexpr with
 		let f = (match f.tf_expr.eexpr with
-			| TBlock _ -> f
+			| TBlock exprs ->
+				if ExtType.is_void (follow f.tf_type) then
+					match List.rev exprs with
+					| { eexpr = TReturn None } :: rest -> { f with tf_expr = { f.tf_expr with eexpr = TBlock (List.rev rest) } }
+					| _ -> f
+				else
+					f
 			| _ -> { f with tf_expr = block f.tf_expr }
 			| _ -> { f with tf_expr = block f.tf_expr }
 		) in
 		) in
 		{ e with eexpr = TFunction f }
 		{ e with eexpr = TFunction f }

+ 11 - 0
tests/optimization/src/TestJs.hx

@@ -624,6 +624,17 @@ class TestJs {
 			obj.id = 2;
 			obj.id = 2;
 		});
 		});
 	}
 	}
+
+	@:js('
+		var voidFunc = function() {};
+		TestJs.use(function() {voidFunc();});
+		TestJs.use(function() {voidFunc();});
+	')
+	static function testIssue6420_voidFunction_noRedundantReturn() {
+		function voidFunc() {}
+		TestJs.use(function() return voidFunc());
+		TestJs.use(() -> voidFunc());
+	}
 }
 }
 
 
 extern class Extern {
 extern class Extern {