Explorar o código

[typer] don't unify array index with int if access isn't allowed

closes #6796
Simon Krajewski %!s(int64=5) %!d(string=hai) anos
pai
achega
65791a73aa

+ 15 - 6
src/typing/calls.ml

@@ -838,7 +838,7 @@ let array_access ctx e1 e2 mode p =
 			end
 		| _ -> raise Not_found)
 	with Not_found ->
-		unify ctx e2.etype ctx.t.tint e2.epos;
+		let base_ok = ref true in
 		let rec loop ?(skip_abstract=false) et =
 			match skip_abstract,follow et with
 			| _, TInst ({ cl_array_access = Some t; cl_params = pl },tl) ->
@@ -856,14 +856,23 @@ let array_access ctx e1 e2 mode p =
 			| _, _ ->
 				let pt = mk_mono() in
 				let t = ctx.t.tarray pt in
-				(try unify_raise ctx et t p
-				with Error(Unify _,_) -> if not ctx.untyped then begin
-					if !has_abstract_array_access then error ("No @:arrayAccess function accepts an argument of " ^ (s_type (print_context()) e2.etype)) e1.epos
-					else error ("Array access is not allowed on " ^ (s_type (print_context()) e1.etype)) e1.epos
-				end);
+				begin try
+					unify_raise ctx et t p
+				with Error(Unify _,_) ->
+					if not ctx.untyped then begin
+						let msg = if !has_abstract_array_access then
+							"No @:arrayAccess function accepts an argument of " ^ (s_type (print_context()) e2.etype)
+						else
+							"Array access is not allowed on " ^ (s_type (print_context()) e1.etype)
+						in
+						base_ok := false;
+						raise_or_display_message ctx msg e1.epos;
+					end
+				end;
 				pt
 		in
 		let pt = loop e1.etype in
+		if !base_ok then unify ctx e2.etype ctx.t.tint e2.epos;
 		AKExpr (mk (TArray (e1,e2)) pt p)
 
 (*

+ 5 - 0
tests/misc/projects/Issue6796/Main.hx

@@ -0,0 +1,5 @@
+class Main {
+    public static function main() {
+        Sys.println(main["foo"]);
+    }
+}

+ 1 - 0
tests/misc/projects/Issue6796/compile-fail.hxml

@@ -0,0 +1 @@
+-main Main

+ 2 - 0
tests/misc/projects/Issue6796/compile-fail.hxml.stderr

@@ -0,0 +1,2 @@
+Main.hx:3: characters 21-25 : Array access is not allowed on Void -> Unknown<0>
+Main.hx:3: characters 21-25 : For function argument 'v'