Parcourir la source

[typer] add more information to `field_call_candidate`

Simon Krajewski il y a 5 ans
Parent
commit
ed3152dfa9
3 fichiers modifiés avec 28 ajouts et 15 suppressions
  1. 20 7
      src/context/typecore.ml
  2. 5 5
      src/typing/calls.ml
  3. 3 3
      src/typing/overloadResolution.ml

+ 20 - 7
src/context/typecore.ml

@@ -143,11 +143,22 @@ and monomorphs = {
 	mutable perfunction : (tmono * pos) list;
 }
 
+(* This record holds transient information about an (attempted) call on a field. It is created when resolving
+   field calls and is passed to overload filters. *)
 type 'a field_call_candidate = {
-	fc_args : (texpr * bool) list;
-	fc_type : Type.t;
+	(* The argument expressions for this call and whether or not the argument is optional on the
+	   target function. *)
+	fc_args  : (texpr * bool) list;
+	(* The applied return type. *)
+	fc_ret   : Type.t;
+	(* The applied function type. *)
+	fc_type  : Type.t;
+	(* The class field being called. *)
 	fc_field : tclass_field;
-	fc_data : 'a;
+	(* The field monomorphs that were created for this call. *)
+	fc_monos : Type.t list;
+	(* The custom data associated with this call. *)
+	fc_data  : 'a;
 }
 
 exception Forbid_package of (string * path * pos) * pos list * string
@@ -542,11 +553,13 @@ let safe_mono_close ctx m p =
 		Unify_error l ->
 			raise_or_display ctx l p
 
-let make_field_call_candidate args t cf data = {
-	fc_args = args;
-	fc_type = t;
+let make_field_call_candidate args ret monos t cf data = {
+	fc_args  = args;
+	fc_type  = t;
 	fc_field = cf;
-	fc_data = data;
+	fc_data  = data;
+	fc_ret   = ret;
+	fc_monos = monos;
 }
 
 let s_field_call_candidate fcc =

+ 5 - 5
src/typing/calls.ml

@@ -234,26 +234,26 @@ let unify_field_call ctx fa el_typed el p inline =
 		match follow t with
 		| TFun(args,ret) ->
 			let rec loop acc tmap args el_typed = match args,el_typed with
-				| (_,_,t0) :: args,e :: el_typed ->
+				| (_,opt,t0) :: args,e :: el_typed ->
 					begin try
 						unify_raise ctx (tmap e.etype) t0 e.epos;
 					with Error(Unify _ as msg,p) ->
 						let call_error = Call_error(Could_not_unify msg) in
 						raise(Error(call_error,p))
 					end;
-					loop (e :: acc) (fun t -> t) args el_typed
+					loop ((e,opt) :: acc) (fun t -> t) args el_typed
 				| _ ->
 					(fun el -> (List.rev acc) @ el),args
 			in
 			let get_call_args,args = loop [] tmap args el_typed in
 			let el,tf = unify_call_args' ctx el args ret p inline is_forced_inline in
+			let el = get_call_args el in
 			let mk_call () =
 				let ef = mk (TField(fa.fa_on,FieldAccess.apply_fa cf fa.fa_host)) t fa.fa_pos in
 				let el = List.map fst el in
-				let el = get_call_args el in
 				make_call ctx ef el ret ~force_inline:inline p
 			in
-			make_field_call_candidate el tf cf mk_call
+			make_field_call_candidate el ret monos tf cf mk_call
 		| t ->
 			error (s_type (print_context()) t ^ " cannot be called") p
 	in
@@ -301,7 +301,7 @@ let unify_field_call ctx fa el_typed el p inline =
 			let ef = mk (TField(fa.fa_on,FieldAccess.apply_fa fa.fa_field fa.fa_host)) tf fa.fa_pos in
 			mk (TCall(ef,[])) t_dynamic p
 		in
-		make_field_call_candidate [] tf fa.fa_field call
+		make_field_call_candidate [] t_dynamic [] tf fa.fa_field call
 	in
 	let maybe_check_access cf =
 		(* type_field doesn't check access for overloads, so let's check it here *)

+ 3 - 3
src/typing/overloadResolution.ml

@@ -6,7 +6,7 @@ open TFunctions
 let unify_cf map_type c cf el =
 	let monos = List.map (fun _ -> mk_mono()) cf.cf_params in
 	match follow (apply_params cf.cf_params monos (map_type cf.cf_type)) with
-		| TFun(tl'',_) as tf ->
+		| TFun(tl'',ret) as tf ->
 			let rec loop2 acc el tl = match el,tl with
 				| e :: el,(_,o,t) :: tl ->
 					begin try
@@ -17,7 +17,7 @@ let unify_cf map_type c cf el =
 						| TAbstract({a_path=["haxe";"extern"],"Rest"},[t]),[] ->
 							begin try
 								let el = List.map (fun e -> unify t e.etype; e,o) el in
-								let fcc = make_field_call_candidate ((List.rev acc) @ el) tf cf (c,cf,monos) in
+								let fcc = make_field_call_candidate ((List.rev acc) @ el) ret monos tf cf (c,cf,monos) in
 								Some fcc
 							with _ ->
 								None
@@ -26,7 +26,7 @@ let unify_cf map_type c cf el =
 							None
 					end
 				| [],[] ->
-					let fcc = make_field_call_candidate (List.rev acc) tf cf (c,cf,monos) in
+					let fcc = make_field_call_candidate (List.rev acc) ret monos tf cf (c,cf,monos) in
 					Some fcc
 				| _ ->
 					None