Browse Source

use `cast_or_unify` for map literals (closes #4100)

Simon Krajewski 10 years ago
parent
commit
094db4e554
2 changed files with 45 additions and 5 deletions
  1. 40 0
      tests/unit/src/unit/issues/Issue4100.hx
  2. 5 5
      typer.ml

+ 40 - 0
tests/unit/src/unit/issues/Issue4100.hx

@@ -0,0 +1,40 @@
+package unit.issues;
+
+private enum ValueEnum {
+	VString(v : String);
+	VInt(v : Int);
+}
+
+private abstract ValueAbs(ValueEnum) {
+	public inline function new(v : ValueEnum) {
+		this = v;
+	}
+
+	@:from
+	public static function fromString(v : String) : ValueAbs {
+		return new ValueAbs(VString(v));
+	}
+
+	@:from
+	public static function fromInt(v : Int) {
+		return new ValueAbs(VInt(v));
+	}
+
+	public function getString() {
+		return switch (this) {
+			case VString(s): s;
+			case VInt(i): "" + i;
+		}
+	}
+}
+
+class Issue4100 extends Test {
+	function test() {
+		var myMap2 : Map<String, ValueAbs> = [
+		  "one" => "some other string",
+		  "two" => 12
+		];
+		eq("some other string", myMap2["one"].getString());
+		eq("12", myMap2["two"].getString());
+	}
+}

+ 5 - 5
typer.ml

@@ -3010,9 +3010,9 @@ and type_expr ctx (e,p) (with_type:with_type) =
 			| WithTypeResume t -> get_map_params t,true
 			| _ -> (mk_mono(),mk_mono()),false
 		in
-		let unify_with_resume ctx a b p =
-			if resume then try unify_raise ctx a b p with Error (Unify l,p) -> raise (WithTypeError(l,p))
-			else unify ctx a b p
+		let unify_with_resume ctx e t p =
+			if resume then try Codegen.AbstractCast.cast_or_unify_raise ctx t e p with Error (Unify l,p) -> raise (WithTypeError(l,p))
+			else Codegen.AbstractCast.cast_or_unify ctx t e p
 		in
 		let type_arrow e1 e2 =
 			let e1 = type_expr ctx e1 (WithType tkey) in
@@ -3022,9 +3022,9 @@ and type_expr ctx (e,p) (with_type:with_type) =
 				error "Previously defined here" p
 			with Not_found ->
 				Hashtbl.add keys e1.eexpr e1.epos;
-				unify_with_resume ctx e1.etype tkey e1.epos;
+				let e1 = unify_with_resume ctx e1 tkey e1.epos in
 				let e2 = type_expr ctx e2 (WithType tval) in
-				unify_with_resume ctx e2.etype tval e2.epos;
+				let e2 = unify_with_resume ctx e2 tval e2.epos in
 				e1,e2
 		in
 		let m = Typeload.load_module ctx ([],"Map") null_pos in