Browse Source

[hl] restore unsafe cast optimization on Map.get (#12396)

Yuxiao Mao 1 month ago
parent
commit
a548c5ab9c
1 changed files with 15 additions and 1 deletions
  1. 15 1
      src/generators/genhl.ml

+ 15 - 1
src/generators/genhl.ml

@@ -2328,8 +2328,22 @@ and eval_expr ctx e =
 		);
 		);
 		(match !def_ret with
 		(match !def_ret with
 		| None ->
 		| None ->
+			let is_map_get_method t =
+				match follow t with
+				| TFun (_,rt) ->
+					(match follow rt with
+					| TInst({ cl_kind = KTypeParameter ttp; cl_path=["haxe";"ds";("StringMap"|"ObjectMap"|"IntMap"|"Int64Map")],_ }, _) -> true
+					| _ -> false)
+				| _ ->
+					false
+			in
 			let rt = to_type ctx e.etype in
 			let rt = to_type ctx e.etype in
-			cast_to ~force:true ctx ret rt e.epos
+			(match ec.eexpr with
+			| TField (_, FInstance(_,_,{ cf_kind = Method (MethNormal|MethInline); cf_type = t })) when is_map_get_method t ->
+				(* let's trust the compiler on map.get type *)
+				unsafe_cast_to ctx ret rt e.epos
+			| _ ->
+				cast_to ~force:true ctx ret rt e.epos)
 		| Some r -> r
 		| Some r -> r
 		)
 		)
 	| TField (ec,FInstance({ cl_path = [],"Array" },[t],{ cf_name = "length" })) when to_type ctx t = HDyn ->
 	| TField (ec,FInstance({ cl_path = [],"Array" },[t],{ cf_name = "length" })) when to_type ctx t = HDyn ->