浏览代码

Don't add `Null` to array access too early (fixes #6846)

Alexander Kuzmenko 7 年之前
父节点
当前提交
0f3c780c8b
共有 1 个文件被更改,包括 5 次插入1 次删除
  1. 5 1
      src/typing/typer.ml

+ 5 - 1
src/typing/typer.ml

@@ -2614,6 +2614,7 @@ and type_array_access ctx e1 e2 p mode =
 		| _ -> raise Not_found)
 	with Not_found ->
 		unify ctx e2.etype ctx.t.tint e2.epos;
+		let is_nullable = ref false in
 		let rec loop et =
 			match follow et with
 			| TInst ({ cl_array_access = Some t; cl_params = pl },tl) ->
@@ -2627,7 +2628,9 @@ and type_array_access ctx e1 e2 p mode =
 			| TAbstract(a,tl) when Meta.has Meta.ArrayAccess a.a_meta ->
 				loop (apply_params a.a_params tl a.a_this)
 			| t ->
-				let pt = if should_be_nullable_array_access ctx t mode then ctx.t.tnull (mk_mono()) else mk_mono() in
+				if should_be_nullable_array_access ctx t mode then
+					is_nullable := true;
+				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
@@ -2637,6 +2640,7 @@ and type_array_access ctx e1 e2 p mode =
 				pt
 		in
 		let pt = loop e1.etype in
+		let pt = if !is_nullable then ctx.t.tnull pt else pt in
 		AKExpr (mk (TArray (e1,e2)) pt p)
 
 and type_vars ctx vl p =