Browse Source

[typer] track type parameter hosts

Simon Krajewski 1 year ago
parent
commit
6f4a1071d2

+ 1 - 1
src/codegen/gencommon/closuresToClass.ml

@@ -393,7 +393,7 @@ let configure gen ft =
 		in
 
 		(*let cltypes = List.map (fun cl -> (snd cl.cl_path, TInst(map_param cl, []) )) tparams in*)
-		let cltypes = List.map (fun cl -> mk_type_param cl None None) tparams in
+		let cltypes = List.map (fun cl -> mk_type_param cl TPHType None None) tparams in
 
 		(* create a new class that extends abstract function class, with a ctor implementation that will setup all captured variables *)
 		let cfield = match gen.gcurrent_classfield with

+ 1 - 1
src/codegen/gencommon/gencommon.ml

@@ -1142,7 +1142,7 @@ let clone_param ttp =
 	let ret = mk_class cl.cl_module (fst cl.cl_path, snd cl.cl_path ^ "_c") cl.cl_pos null_pos in
 	ret.cl_implements <- cl.cl_implements;
 	ret.cl_kind <- cl.cl_kind;
-	let ttp = mk_type_param ret ttp.ttp_default ttp.ttp_constraints in
+	let ttp = mk_type_param ret ttp.ttp_host ttp.ttp_default ttp.ttp_constraints in
 	ret.cl_kind <- KTypeParameter ttp;
 	ttp
 

+ 2 - 1
src/core/tFunctions.ml

@@ -698,10 +698,11 @@ let lookup_param n l =
 	in
 	loop l
 
-let mk_type_param c def constraints = {
+let mk_type_param c host def constraints = {
 	ttp_name = snd c.cl_path;
 	ttp_type = TInst(c,[]);
 	ttp_class = c;
+	ttp_host = host;
 	ttp_constraints = constraints;
 	ttp_default = def;
 }

+ 9 - 0
src/core/tType.ml

@@ -44,6 +44,14 @@ type module_cache_state =
 	| MSBad of module_skip_reason
 	| MSUnknown
 
+type type_param_host =
+	| TPHType
+	| TPHConstructor
+	| TPHMethod
+	| TPHEnumConstructor
+	| TPHAnonField
+	| TPHLocal
+
 type t =
 	| TMono of tmono
 	| TEnum of tenum * tparams
@@ -93,6 +101,7 @@ and tparams = t list
 and typed_type_param = {
 	ttp_name : string;
 	ttp_class : tclass;
+	ttp_host : type_param_host;
 	mutable ttp_type : t;
 	mutable ttp_constraints : t list Lazy.t option;
 	mutable ttp_default : t option;

+ 1 - 1
src/generators/genhl.ml

@@ -361,7 +361,7 @@ let make_debug ctx arr =
 let fake_tnull =
 	{null_abstract with
 		a_path = [],"Null";
-		a_params = [mk_type_param null_class None None];
+		a_params = [mk_type_param null_class TPHType None None];
 	}
 
 let get_rec_cache ctx t none_callback not_found_callback =

+ 1 - 1
src/macro/macroApi.ml

@@ -2277,7 +2277,7 @@ let macro_api ccom get_api =
 					| TInst(c,_) -> c
 					| _ -> die "" __LOC__
 				in				
-				mk_type_param c default None
+				mk_type_param c TPHType default None
 			) (decode_array tpl) in
 			let rec map t = match t with
 				| TInst({cl_kind = KTypeParameter _},_) ->

+ 1 - 1
src/typing/generic.ml

@@ -315,7 +315,7 @@ let build_generic_class ctx c p tl =
 					| None -> None
 					| Some constraints -> Some (lazy (List.map (generic_substitute_type gctx) (Lazy.force constraints)))
 				in
-				let ttp' = mk_type_param c def constraints in
+				let ttp' = mk_type_param c ttp.ttp_host def constraints in
 				(ttp.ttp_type,ttp')
 			) cf_old.cf_params in
 			let param_subst = List.map (fun (t,ttp) -> t,(ttp.ttp_type,None)) params in

+ 8 - 12
src/typing/typeload.ml

@@ -35,7 +35,7 @@ open Globals
 
 let build_count = ref 0
 
-let type_function_params_rec = ref (fun _ _ _ _ -> die "" __LOC__)
+let type_function_params_ref = ref (fun _ _ _ _ _ -> die "" __LOC__)
 
 let check_field_access ctx cff =
 	let display_access = ref None in
@@ -576,7 +576,7 @@ and load_complex_type' ctx allow_display (t,p) =
 					no_expr e;
 					topt t, Var { v_read = AccNormal; v_write = AccNormal }
 				| FFun fd ->
-					params := (!type_function_params_rec) ctx fd (fst f.cff_name) p;
+					params := (!type_function_params_ref) ctx fd TPHAnonField (fst f.cff_name) p;
 					no_expr fd.f_expr;
 					let old = ctx.type_params in
 					ctx.type_params <- !params @ old;
@@ -673,7 +673,7 @@ and init_meta_overloads ctx co cf =
 						not (List.mem t l) (* TODO: this still looks suspicious *)
 					) ctx.type_params
 			end;
-			let params : type_params = (!type_function_params_rec) ctx f cf.cf_name p in
+			let params : type_params = (!type_function_params_ref) ctx f TPHMethod cf.cf_name p in
 			ctx.type_params <- params @ ctx.type_params;
 			let topt = function None -> raise_typing_error "Explicit type required" p | Some t -> load_complex_type ctx true t in
 			let args =
@@ -728,12 +728,6 @@ let load_type_hint ?(opt=false) ctx pcur t =
 (* ---------------------------------------------------------------------- *)
 (* PASS 1 & 2 : Module and Class Structure *)
 
-type type_param_host =
-	| TPHType
-	| TPHConstructor
-	| TPHMethod
-	| TPHEnumConstructor
-
 let rec type_type_param ctx host path get_params p tp =
 	let n = fst tp.tp_name in
 	let c = mk_class ctx.m.curmod (fst path @ [snd path],n) (pos tp.tp_name) (pos tp.tp_name) in
@@ -754,7 +748,9 @@ let rec type_type_param ctx host path get_params p tp =
 					()
 				| TPHConstructor
 				| TPHMethod
-				| TPHEnumConstructor ->
+				| TPHEnumConstructor
+				| TPHAnonField
+				| TPHLocal ->
 					display_error ctx.com "Default type parameters are only supported on types" (pos ct)
 				end;
 				t
@@ -763,7 +759,7 @@ let rec type_type_param ctx host path get_params p tp =
 	in
 	let ttp = match tp.tp_constraints with
 		| None ->
-			mk_type_param c default None
+			mk_type_param c host default None
 		| Some th ->
 			let constraints = lazy (
 				let ctx = { ctx with type_params = ctx.type_params @ get_params() } in
@@ -787,7 +783,7 @@ let rec type_type_param ctx host path get_params p tp =
 				constr
 			) in
 			delay ctx PConnectField (fun () -> ignore (Lazy.force constraints));
-			mk_type_param c default (Some constraints)
+			mk_type_param c host default (Some constraints)
 	in
 	c.cl_kind <- KTypeParameter ttp;
 	ttp

+ 1 - 1
src/typing/typeloadFields.ml

@@ -1295,7 +1295,7 @@ let setup_args_ret ctx cctx fctx name fd p =
 
 let create_method (ctx,cctx,fctx) c f fd p =
 	let name = fst f.cff_name in
-	let params = TypeloadFunction.type_function_params ctx fd name p in
+	let params = TypeloadFunction.type_function_params ctx fd TPHMethod name p in
 	if fctx.is_generic then begin
 		if params = [] then raise_typing_error "Generic functions must have type parameters" p;
 	end;

+ 3 - 3
src/typing/typeloadFunction.ml

@@ -43,9 +43,9 @@ let save_field_state ctx =
 		ctx.in_function <- old_in_function;
 	)
 
-let type_function_params ctx fd fname p =
+let type_function_params ctx fd host fname p =
 	let params = ref [] in
-	params := Typeload.type_type_params ctx TPHMethod ([],fname) (fun() -> !params) p fd.f_params;
+	params := Typeload.type_type_params ctx host ([],fname) (fun() -> !params) p fd.f_params;
 	!params
 
 let type_function ctx (args : function_arguments) ret fmode e do_display p =
@@ -257,4 +257,4 @@ let add_constructor ctx c force_constructor p =
 		(* nothing to do *)
 		()
 ;;
-Typeload.type_function_params_rec := type_function_params
+Typeload.type_function_params_ref := type_function_params

+ 1 - 1
src/typing/typer.ml

@@ -1218,7 +1218,7 @@ and type_map_declaration ctx e1 el with_type p =
 
 and type_local_function ctx kind f with_type p =
 	let name,inline = match kind with FKNamed (name,inline) -> Some name,inline | _ -> None,false in
-	let params = TypeloadFunction.type_function_params ctx f (match name with None -> "localfun" | Some (n,_) -> n) p in
+	let params = TypeloadFunction.type_function_params ctx f TPHLocal (match name with None -> "localfun" | Some (n,_) -> n) p in
 	if params <> [] then begin
 		if name = None then display_error ctx.com "Type parameters not supported in unnamed local functions" p;
 		if with_type <> WithType.NoValue then raise_typing_error "Type parameters are not supported for rvalue functions" p