Browse Source

[display] support expected type for map key expression

see #9133
Simon Krajewski 5 years ago
parent
commit
a89e6752cf
3 changed files with 53 additions and 10 deletions
  1. 16 9
      src/typing/typer.ml
  2. 2 1
      tests/display/build.hxml
  3. 35 0
      tests/display/src/cases/Issue9133.hx

+ 16 - 9
src/typing/typer.ml

@@ -2549,17 +2549,24 @@ and type_expr ?(mode=MGet) ctx (e,p) (with_type:WithType.t) =
 	| EArrayDecl ((EBinop(OpArrow,_,_),_) as e1 :: el) ->
 	| EArrayDecl ((EBinop(OpArrow,_,_),_) as e1 :: el) ->
 		type_map_declaration ctx e1 el with_type p
 		type_map_declaration ctx e1 el with_type p
 	| EArrayDecl el ->
 	| EArrayDecl el ->
-		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
+		begin match with_type with
+		| WithType(t,_) ->
+			begin match follow t with
+			| TAbstract({a_path = (["haxe";"ds"],"Map")},[tk;tv]) ->
+				begin match el with
+				| [] ->
+					type_expr ctx (ENew(({tpackage=["haxe";"ds"];tname="Map";tparams=[];tsub=None},null_pos),[]),p) with_type
+				| [(EDisplay _,_) as e1] ->
+					(* This must mean we're just typing the first key of a map declaration (issue #9133). *)
+					type_expr ctx e1 (WithType.with_type tk)
+				| _ ->
+					type_array_decl ctx el with_type p
+				end
 			| _ ->
 			| _ ->
 				type_array_decl ctx el with_type p
 				type_array_decl ctx el with_type p
+			end
+		| _ ->
+			type_array_decl ctx el with_type p
 		end
 		end
 	| EVars vl ->
 	| EVars vl ->
 		type_vars ctx vl p
 		type_vars ctx vl p

+ 2 - 1
tests/display/build.hxml

@@ -4,4 +4,5 @@
 -lib utest
 -lib utest
 -lib haxeserver
 -lib haxeserver
 --interp
 --interp
--D use-rtti-doc
+-D use-rtti-doc
+#-D test=9133

+ 35 - 0
tests/display/src/cases/Issue9133.hx

@@ -0,0 +1,35 @@
+package cases;
+
+import utest.Assert;
+
+using Lambda;
+
+class Issue9133 extends DisplayTestCase {
+	/**
+		class Main {
+		static function main() {
+			var i = 1;
+			var s = "";
+
+			var map:Map<Int, Int> = [
+				{-1-}
+	**/
+	function test1() {
+		var fields = toplevel(pos(1));
+		var i1 = fields.findIndex(item -> item.kind == "local" && item.name == "i");
+		var i2 = fields.findIndex(item -> item.kind == "local" && item.name == "s");
+		Assert.isTrue(i1 < i2);
+		Assert.isTrue(i1 != -1);
+	}
+
+	/**
+		class Main {
+		static function main() {
+			var i = 1;
+			var s = "";
+
+			var map:Map<Int, Int> = [
+				{-1-}
+	**/
+	function test2() {}
+}