浏览代码

[typer] allow assigning `[]` when Map is expected

closes #7426
Simon Krajewski 7 年之前
父节点
当前提交
dda4521002
共有 2 个文件被更改,包括 17 次插入2 次删除
  1. 12 1
      src/typing/typer.ml
  2. 5 1
      tests/unit/src/unitstd/Map.unit.hx

+ 12 - 1
src/typing/typer.ml

@@ -2393,7 +2393,18 @@ and type_expr ctx (e,p) (with_type:WithType.t) =
 	| EArrayDecl ((EBinop(OpArrow,_,_),_) as e1 :: el) ->
 		type_map_declaration ctx e1 el with_type p
 	| EArrayDecl el ->
-		type_array_decl ctx el with_type p
+		begin match el,with_type with
+			| [],WithType(t,_) ->
+				let rec loop t = match follow t with
+					| TAbstract({a_path = (["haxe";"ds"],"Map")},_) ->
+						type_expr ctx (ENew(({tpackage=["haxe";"ds"];tname="Map";tparams=[];tsub=None},null_pos),[]),p) with_type
+					| _ ->
+						type_array_decl ctx el with_type p
+				in
+				loop t
+			| _ ->
+				type_array_decl ctx el with_type p
+		end
 	| EVars vl ->
 		type_vars ctx vl p
 	| EFor (it,e2) ->

+ 5 - 1
tests/unit/src/unitstd/Map.unit.hx

@@ -228,4 +228,8 @@ map["foo"] == 9;
 [2 => 3].iterator().next() == 3;
 //[a => b].keys().next() == a;
 //[a => b].iterator().next() == b;
-#end
+#end
+
+var map:Map<String, Int>;
+HelperMacros.typedAs((null : Map<String, Int>), map = []);
+HelperMacros.typeError(map[1] = 1) == true;