浏览代码

[inliner] only unify_min if we go from no-type to some type (closes #6406)

Simon Krajewski 8 年之前
父节点
当前提交
eba522ae24
共有 2 个文件被更改,包括 20 次插入3 次删除
  1. 4 3
      src/optimization/optimizer.ml
  2. 16 0
      tests/unit/src/unit/issues/Issue6406.hx

+ 4 - 3
src/optimization/optimizer.ml

@@ -410,13 +410,14 @@ let rec type_inline ctx cf f ethis params tret config p ?(self_calling_closure=f
 				el, map term e
 			) cases in
 			let def = opt (map term) def in
-			{ e with eexpr = TSwitch (map false e1,cases,def); etype = if ret_val then unify_min ctx ((List.map snd cases) @ (match def with None -> [] | Some e -> [e])) else e.etype }
+			let t = if ret_val && ExtType.is_void e.etype then unify_min ctx ((List.map snd cases) @ (match def with None -> [] | Some e -> [e])) else e.etype in
+			{ e with eexpr = TSwitch (map false e1,cases,def); etype = t }
 		| TTry (e1,catches) ->
 			{ e with eexpr = TTry (map term e1,List.map (fun (v,e) ->
 				let lv = (local v).i_subst in
 				let e = map term e in
 				lv,e
-			) catches); etype = if term && ret_val then unify_min ctx (e1::List.map snd catches) else e.etype }
+			) catches); etype = if term && ret_val && ExtType.is_void e.etype then unify_min ctx (e1::List.map snd catches) else e.etype }
 		| TBlock l ->
 			let old = save_locals ctx in
 			let t = ref e.etype in
@@ -458,7 +459,7 @@ let rec type_inline ctx cf f ethis params tret config p ?(self_calling_closure=f
 			let econd = map false econd in
 			let eif = map term eif in
 			let eelse = map term eelse in
-			{ e with eexpr = TIf(econd,eif,Some eelse); etype = if ret_val then unify_min ctx [eif;eelse] else e.etype }
+			{ e with eexpr = TIf(econd,eif,Some eelse); etype = if ret_val && ExtType.is_void e.etype then unify_min ctx [eif;eelse] else e.etype }
 		| TParenthesis e1 ->
 			let e1 = map term e1 in
 			mk (TParenthesis e1) e1.etype e.epos

+ 16 - 0
tests/unit/src/unit/issues/Issue6406.hx

@@ -0,0 +1,16 @@
+package unit.issues;
+
+class Issue6406 extends unit.Test {
+	function test() {
+		eq(42, fIf(1));
+		eq(42, fSwitch(1));
+		// eq(42, fTry(1));
+	}
+
+	inline static function fIf(i:Int):Dynamic return if (i == 0) true else 42;
+
+	inline static function fSwitch(i:Int):Dynamic return switch (i) { case 0: true; case _: 42; }
+
+	// not sure if this is supposed to work
+	// inline static function fTry(i:Int):Dynamic return try 42 catch(e:Dynamic) true;
+}