Browse Source

[typer] unify catch expression against expected type

closes #5384
Simon Krajewski 7 years ago
parent
commit
944cd1143a
2 changed files with 25 additions and 1 deletions
  1. 5 1
      src/typing/typer.ml
  2. 20 0
      tests/unit/src/unit/issues/Issue5384.hx

+ 5 - 1
src/typing/typer.ml

@@ -1894,7 +1894,11 @@ and type_try ctx e1 catches with_type p =
 		if ctx.is_display_file && DisplayPosition.encloses_display_position pc then ignore(TyperDisplay.display_expr ctx e_ast e DKMarked with_type pc);
 		v.v_type <- t2;
 		locals();
-		if with_type <> NoValue then unify ctx e.etype e1.etype e.epos;
+		begin match with_type with
+			| NoValue -> ()
+			| Value -> unify ctx e.etype e1.etype e.epos
+			| WithType t -> unify ctx e.etype t e.epos
+		end;
 		if PMap.mem name ctx.locals then error ("Local variable " ^ name ^ " is preventing usage of this type here") e.epos;
 		(v , e) :: acc
 	) [] catches in

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

@@ -0,0 +1,20 @@
+package unit.issues;
+
+class Issue5384 extends unit.Test {
+	function test() {
+        var a:Dynamic = true;
+        a = "string";
+
+        var b:Dynamic = if (myFunc()) true else "string";
+
+        var c:Dynamic = myFunc() ? true : "string";
+
+        var d:Dynamic = myFunc() ? myFunc() : "string";
+
+        var e:Dynamic = try myFunc() catch(e:Dynamic) "string";
+	}
+
+    static function myFunc():Bool {
+        return true;
+    }
+}