Selaa lähdekoodia

make Map literals aware of IntMap, StringMap, ObjectMap and EnumValueMap (closes #3935)

Simon Krajewski 10 vuotta sitten
vanhempi
commit
baaa83292a
2 muutettua tiedostoa jossa 37 lisäystä ja 0 poistoa
  1. 34 0
      tests/unit/src/unit/issues/Issue3935.hx
  2. 3 0
      typer.ml

+ 34 - 0
tests/unit/src/unit/issues/Issue3935.hx

@@ -0,0 +1,34 @@
+package unit.issues;
+
+private class MyClass {
+	public function new() { }
+}
+
+class Issue3935 extends Test {
+	function test() {
+		var c:haxe.ds.IntMap<Dynamic> = [1=>2, 3=>"4"];
+		var c:haxe.ds.StringMap<Dynamic> = ["1"=>2, "3"=>"4"];
+		var m = new MyClass();
+		var m2 = new MyClass();
+		var c:haxe.ds.ObjectMap<MyClass, Dynamic> = [m => 1, m2 => "1"];
+		var c:haxe.ds.EnumValueMap<haxe.macro.Expr.ExprDef, Dynamic> = [EBreak => 1, EContinue => "2"];
+	}
+
+	function testMap() {
+		var c:Map<Int, Dynamic> = [1=>2, 3=>"4"];
+		var c:Map<String, Dynamic> = ["1"=>2, "3"=>"4"];
+		var m = new MyClass();
+		var m2 = new MyClass();
+		var c:Map<MyClass, Dynamic> = [m => 1, m2 => "1"];
+		var c:Map<haxe.macro.Expr.ExprDef, Dynamic> = [EBreak => 1, EContinue => "2"];
+	}
+
+	function testFail() {
+		t(unit.TestType.typeError([1=>2, 3=>"4"]));
+		t(unit.TestType.typeError(["1"=>2, "3"=>"4"]));
+		var m = new MyClass();
+		var m2 = new MyClass();
+		t(unit.TestType.typeError([m => 1, m2 => "1"]));
+		t(unit.TestType.typeError([EBreak => 1, EContinue => "2"]));
+	}
+}

+ 3 - 0
typer.ml

@@ -3014,6 +3014,9 @@ and type_expr ctx (e,p) (with_type:with_type) =
 		let (tkey,tval,has_type),resume =
 			let get_map_params t = match follow t with
 				| TAbstract({a_path=[],"Map"},[tk;tv]) -> tk,tv,true
+				| TInst({cl_path=["haxe";"ds"],"IntMap"},[tv]) -> ctx.t.tint,tv,true
+				| TInst({cl_path=["haxe";"ds"],"StringMap"},[tv]) -> ctx.t.tstring,tv,true
+				| TInst({cl_path=["haxe";"ds"],("ObjectMap" | "EnumValueMap")},[tk;tv]) -> tk,tv,true
 				| _ -> mk_mono(),mk_mono(),false
 			in
 			match with_type with