Browse Source

check casts for individual case expressions (fixed issue #1574)

Simon Krajewski 12 năm trước cách đây
mục cha
commit
3a5f735583
2 tập tin đã thay đổi với 19 bổ sung5 xóa
  1. 8 5
      matcher.ml
  2. 11 0
      tests/unit/TestBasetypes.hx

+ 8 - 5
matcher.ml

@@ -1165,11 +1165,14 @@ let match_expr ctx e cases def with_type p =
 			| None -> mk (TBlock []) ctx.com.basic.tvoid (punion_el el)
 			| Some e ->
 				let e = type_expr ctx e with_type in
-				(match with_type with
-				| WithType t -> unify ctx e.etype t e.epos
-				| WithTypeResume t -> (try unify_raise ctx e.etype t e.epos with Error (Unify l,p) -> raise (Typer.WithTypeError (l,p)) )
-				| _ -> ());
-				e
+				match with_type with
+				| WithType t ->
+					unify ctx e.etype t e.epos;
+					Codegen.Abstract.check_cast ctx t e e.epos;
+				| WithTypeResume t ->
+					(try unify_raise ctx e.etype t e.epos with Error (Unify l,p) -> raise (Typer.WithTypeError (l,p)));
+					Codegen.Abstract.check_cast ctx t e e.epos
+				| _ -> e
 		in
 		let eg = match eg with
 			| None -> None

+ 11 - 0
tests/unit/TestBasetypes.hx

@@ -383,6 +383,17 @@ class TestBasetypes extends Test {
 		}
 		
 		eq(returnAbstractCast(), "12.2m");
+		
+		// switch
+		function switchMe(b):String {
+			return switch(b) {
+				case true: new unit.MyAbstract.Meter(12.2);
+				default: new unit.MyAbstract.Meter(2.4);
+			}
+		}
+		
+		eq(switchMe(true), "12.2m");
+		eq(switchMe(false), "2.4m");
 	}
 	
 	function testAbstractToAbstractCast() {