Explorar o código

bring back explicit positions for `load_complex_type`

We don't always have names with positions and don't want to use `null_pos`  for errors. Closes #5159
Simon Krajewski %!s(int64=9) %!d(string=hai) anos
pai
achega
9fd42901aa
Modificáronse 3 ficheiros con 57 adicións e 52 borrados
  1. 2 2
      src/typing/matcher.ml
  2. 37 32
      src/typing/typeload.ml
  3. 18 18
      src/typing/typer.ml

+ 2 - 2
src/typing/matcher.ml

@@ -54,7 +54,7 @@ let get_general_module_type ctx mt p =
 			end
 		| _ -> error "Cannot use this type as a value" p
 	in
-	Typeload.load_instance ctx ({tname=loop mt;tpackage=[];tsub=None;tparams=[]},p) true
+	Typeload.load_instance ctx ({tname=loop mt;tpackage=[];tsub=None;tparams=[]},null_pos) true p
 
 module Constructor = struct
 	type t =
@@ -1225,7 +1225,7 @@ module TexprConverter = struct
 	let to_texpr ctx t_switch match_debug with_type dt =
 		let com = ctx.com in
 		let p = dt.dt_pos in
-		let c_type = match follow (Typeload.load_instance ctx ({ tpackage = ["std"]; tname="Type"; tparams=[]; tsub = None},p) true) with TInst(c,_) -> c | t -> assert false in
+		let c_type = match follow (Typeload.load_instance ctx ({ tpackage = ["std"]; tname="Type"; tparams=[]; tsub = None},null_pos) true p) with TInst(c,_) -> c | t -> assert false in
 		let mk_index_call e =
 			if ctx.com.display <> DMNone then
 				(* If we are in display mode there's a chance that these fields don't exist. Let's just use a

+ 37 - 32
src/typing/typeload.ml

@@ -398,8 +398,12 @@ let generate_value_meta com co cf args =
 			| _ -> cf.cf_meta <- ((Meta.Value,[EObjectDecl values,cf.cf_pos],cf.cf_pos) :: cf.cf_meta)
 	end
 
+let pselect p1 p2 =
+	if p1 = null_pos then p2 else p1
+
 (* build an instance from a full type *)
-let rec load_instance ?(allow_display=false) ctx (t,p) allow_no_params =
+let rec load_instance ?(allow_display=false) ctx (t,pn) allow_no_params p =
+	let p = pselect pn p in
 	let t = try
 		if t.tpackage <> [] || t.tsub <> None then raise Not_found;
 		let pt = List.assoc t.tname ctx.type_params in
@@ -428,7 +432,7 @@ let rec load_instance ?(allow_display=false) ctx (t,p) allow_no_params =
 		end else if path = ([],"Dynamic") then
 			match t.tparams with
 			| [] -> t_dynamic
-			| [TPType t] -> TDynamic (load_complex_type ctx false t)
+			| [TPType t] -> TDynamic (load_complex_type ctx false p t)
 			| _ -> error "Too many parameters for Dynamic" p
 		else begin
 			if not is_rest && List.length types <> List.length t.tparams then error ("Invalid number of type parameters for " ^ s_type_path path) p;
@@ -444,7 +448,7 @@ let rec load_instance ?(allow_display=false) ctx (t,p) allow_no_params =
 					let c = mk_class null_module ([],name) p in
 					c.cl_kind <- KExpr e;
 					TInst (c,[])
-				| TPType t -> load_complex_type ctx true t
+				| TPType t -> load_complex_type ctx true p t
 			) t.tparams in
 			let rec loop tl1 tl2 is_rest = match tl1,tl2 with
 				| t :: tl1,(name,t2) :: tl2 ->
@@ -491,20 +495,21 @@ let rec load_instance ?(allow_display=false) ctx (t,p) allow_no_params =
 			f params
 		end
 	in
-	if allow_display && ctx.com.display <> DMNone && Display.is_display_position p then
-		Display.display_type ctx.com.display t p;
+	if allow_display && ctx.com.display <> DMNone && Display.is_display_position pn then
+		Display.display_type ctx.com.display t pn;
 	t
 
 (*
 	build an instance from a complex type
 *)
-and load_complex_type ctx allow_display (t,p) =
+and load_complex_type ctx allow_display p (t,pn) =
+	let p = pselect pn p in
 	match t with
-	| CTParent t -> load_complex_type ctx allow_display t
-	| CTPath t -> load_instance ~allow_display ctx (t,p) false
+	| CTParent t -> load_complex_type ctx allow_display p t
+	| CTPath t -> load_instance ~allow_display ctx (t,pn) false p
 	| CTOptional _ -> error "Optional type not allowed here" p
 	| CTExtend (tl,l) ->
-		(match load_complex_type ctx allow_display (CTAnonymous l,p) with
+		(match load_complex_type ctx allow_display p (CTAnonymous l,p) with
 		| TAnon a as ta ->
 			let is_redefined cf1 a2 =
 				try
@@ -555,7 +560,7 @@ and load_complex_type ctx allow_display (t,p) =
 				| _ ->
 					error "Multiple structural extension is only allowed for structures" p
 			in
-			let il = List.map (fun (t,_) -> load_instance ctx ~allow_display (t,p) false) tl in
+			let il = List.map (fun (t,_) -> load_instance ctx ~allow_display (t,pn) false p) tl in
 			let tr = ref None in
 			let t = TMono tr in
 			let r = exc_protect ctx (fun r ->
@@ -579,7 +584,7 @@ and load_complex_type ctx allow_display (t,p) =
 			if PMap.mem n acc then error ("Duplicate field declaration : " ^ n) p;
 			let topt = function
 				| None -> error ("Explicit type required for field " ^ n) p
-				| Some t -> load_complex_type ctx allow_display t
+				| Some t -> load_complex_type ctx allow_display p t
 			in
 			if n = "new" then ctx.com.warning "Structures with new are deprecated, use haxe.Constraints.Constructible instead" p;
 			let no_expr = function
@@ -627,7 +632,7 @@ and load_complex_type ctx allow_display (t,p) =
 							error "Custom property access is no longer supported in Haxe 3" f.cff_pos;
 					in
 					let t = (match t with None -> error "Type required for structure property" p | Some t -> t) in
-					load_complex_type ctx allow_display t, Var { v_read = access i1 true; v_write = access i2 false }
+					load_complex_type ctx allow_display p t, Var { v_read = access i1 true; v_write = access i2 false }
 			) in
 			let t = if Meta.has Meta.Optional f.cff_meta then ctx.t.tnull t else t in
 			let cf = {
@@ -649,12 +654,12 @@ and load_complex_type ctx allow_display (t,p) =
 	| CTFunction (args,r) ->
 		match args with
 		| [CTPath { tpackage = []; tparams = []; tname = "Void" },_] ->
-			TFun ([],load_complex_type ctx allow_display r)
+			TFun ([],load_complex_type ctx allow_display p r)
 		| _ ->
 			TFun (List.map (fun t ->
 				let t, opt = (match fst t with CTOptional t -> t, true | _ -> t,false) in
-				"",opt,load_complex_type ctx allow_display t
-			) args,load_complex_type ctx allow_display r)
+				"",opt,load_complex_type ctx allow_display p t
+			) args,load_complex_type ctx allow_display p r)
 
 and init_meta_overloads ctx co cf =
 	let overloads = ref [] in
@@ -674,7 +679,7 @@ and init_meta_overloads ctx co cf =
 			| l -> ctx.type_params <- List.filter (fun t -> not (List.mem t l)) ctx.type_params);
 			let params = (!type_function_params_rec) ctx f cf.cf_name p in
 			ctx.type_params <- params @ ctx.type_params;
-			let topt = function None -> error "Explicit type required" p | Some t -> load_complex_type ctx true t in
+			let topt = function None -> error "Explicit type required" p | Some t -> load_complex_type ctx true p t in
 			let args = List.map (fun ((a,_),opt,_,t,_) -> a,opt,topt t) f.f_args in
 			let cf = { cf with cf_type = TFun (args,topt f.f_type); cf_params = params; cf_meta = cf_meta} in
 			generate_value_meta ctx.com co cf f.f_args;
@@ -721,7 +726,7 @@ let hide_params ctx =
 *)
 let load_core_type ctx name =
 	let show = hide_params ctx in
-	let t = load_instance ctx ({ tpackage = []; tname = name; tparams = []; tsub = None; },null_pos) false in
+	let t = load_instance ctx ({ tpackage = []; tname = name; tparams = []; tsub = None; },null_pos) false null_pos in
 	show();
 	add_dependency ctx.m.curmod (match t with
 	| TInst (c,_) -> c.cl_module
@@ -746,10 +751,10 @@ let t_iterator ctx =
 (*
 	load either a type t or Null<Unknown> if not defined
 *)
-let load_type_hint ?(opt=false) ctx t =
+let load_type_hint ?(opt=false) ctx pcur t =
 	let t = match t with
 		| None -> mk_mono()
-		| Some (t,p) -> load_complex_type ctx true (t,p)
+		| Some (t,p) -> load_complex_type ctx true pcur (t,p)
 	in
 	if opt then ctx.t.tnull t else t
 
@@ -1471,7 +1476,7 @@ module Inheritance = struct
 		let herits = List.filter (ctx.g.do_inherit ctx c p) herits in
 		(* Pass 1: Check and set relations *)
 		let fl = List.map (fun (is_extends,t) ->
-			let t = load_instance ~allow_display:true ctx t false in
+			let t = load_instance ~allow_display:true ctx t false p in
 			if is_extends then begin
 				if c.cl_super <> None then error "Cannot extend several classes" p;
 				let csup,params = check_extends ctx c t p in
@@ -1535,7 +1540,7 @@ let rec type_type_param ?(enum_constructor=false) ctx path get_params p tp =
 		let r = exc_protect ctx (fun r ->
 			r := (fun _ -> t);
 			let ctx = { ctx with type_params = ctx.type_params @ get_params() } in
-			let constr = List.map (load_complex_type ctx true) tp.tp_constraints in
+			let constr = List.map (load_complex_type ctx true p) tp.tp_constraints in
 			(* check against direct recursion *)
 			let rec loop t =
 				match follow t with
@@ -1695,7 +1700,7 @@ let load_core_class ctx c =
 		| KAbstractImpl a -> { tpackage = fst a.a_path; tname = snd a.a_path; tparams = []; tsub = None; }
 		| _ -> { tpackage = fst c.cl_path; tname = snd c.cl_path; tparams = []; tsub = None; }
 	in
-	let t = load_instance ctx2 (tpath,c.cl_pos) true in
+	let t = load_instance ctx2 (tpath,c.cl_pos) true c.cl_pos in
 	flush_pass ctx2 PFinal "core_final";
 	match t with
 	| TInst (ccore,_) | TAbstract({a_impl = Some ccore}, _) ->
@@ -2054,7 +2059,7 @@ module ClassInitializer = struct
 			display_error ctx "Type required for core api classes" p;
 			t_dynamic
 		| _ ->
-			load_type_hint ctx t
+			load_type_hint ctx p t
 
 	let build_fields (ctx,cctx) c fields =
 		let fields = ref fields in
@@ -2257,7 +2262,7 @@ module ClassInitializer = struct
 				(* TODO is_lib: only load complex type if needed *)
 				let old = ctx.type_params in
 				if fctx.is_static then ctx.type_params <- [];
-				let t = load_complex_type ctx true t in
+				let t = load_complex_type ctx true p t in
 				if fctx.is_static then ctx.type_params <- old;
 				t
 		) in
@@ -2556,7 +2561,7 @@ module ClassInitializer = struct
 		let ret = (match t, eo with
 			| None, None -> error (name ^ ": Property must either define a type or a default value") p;
 			| None, _ -> mk_mono()
-			| Some t, _ -> load_complex_type ctx true t
+			| Some t, _ -> load_complex_type ctx true p t
 		) in
 		let t_get,t_set = match cctx.abstract with
 			| Some a when fctx.is_abstract_member ->
@@ -3170,7 +3175,7 @@ let init_module_type ctx context_init do_init (decl,p) =
 			let rt = (match c.ec_type with
 				| None -> et
 				| Some t ->
-					let t = load_complex_type ctx true t in
+					let t = load_complex_type ctx true p t in
 					(match follow t with
 					| TEnum (te,_) when te == e ->
 						()
@@ -3187,7 +3192,7 @@ let init_module_type ctx context_init do_init (decl,p) =
 						(match t with CTPath({tpackage=[];tname="Void"}) -> error "Arguments of type Void are not allowed in enum constructors" c.ec_pos | _ -> ());
 						if PMap.mem s (!pnames) then error ("Duplicate parameter '" ^ s ^ "' in enum constructor " ^ fst c.ec_name) p;
 						pnames := PMap.add s () (!pnames);
-						s, opt, load_type_hint ~opt ctx (Some (t,tp))
+						s, opt, load_type_hint ~opt ctx p (Some (t,tp))
 					) l, rt)
 			) in
 			if PMap.mem (fst c.ec_name) e.e_constrs then error ("Duplicate constructor " ^ fst c.ec_name) p;
@@ -3249,7 +3254,7 @@ let init_module_type ctx context_init do_init (decl,p) =
 		if Display.is_display_position (pos d.d_name) then Display.display_module_type ctx.com.display (TTypeDecl t) (pos d.d_name);
 		check_global_metadata ctx (fun m -> t.t_meta <- m :: t.t_meta) t.t_module.m_path t.t_path None;
 		let ctx = { ctx with type_params = t.t_params } in
-		let tt = load_complex_type ctx true d.d_data in
+		let tt = load_complex_type ctx true p d.d_data in
 		let tt = (match fst d.d_data with
 		| CTExtend _ -> tt
 		| CTPath { tpackage = ["haxe";"macro"]; tname = "MacroType" } ->
@@ -3281,7 +3286,7 @@ let init_module_type ctx context_init do_init (decl,p) =
 		let ctx = { ctx with type_params = a.a_params } in
 		let is_type = ref false in
 		let load_type t from =
-			let t = load_complex_type ctx true t in
+			let t = load_complex_type ctx true p t in
 			let t = if not (Meta.has Meta.CoreType a.a_meta) then begin
 				if !is_type then begin
 					let r = exc_protect ctx (fun r ->
@@ -3307,7 +3312,7 @@ let init_module_type ctx context_init do_init (decl,p) =
 			| AIsType t ->
 				if a.a_impl = None then error "Abstracts with underlying type must have an implementation" a.a_pos;
 				if Meta.has Meta.CoreType a.a_meta then error "@:coreType abstracts cannot have an underlying type" p;
-				let at = load_complex_type ctx true t in
+				let at = load_complex_type ctx true p t in
 				delay ctx PForce (fun () ->
 					begin match follow at with
 						| TAbstract(a2,_) when a == a2 -> error "Abstract underlying type cannot be recursive" a.a_pos
@@ -3793,7 +3798,7 @@ let rec build_generic ctx c p tl =
 	let gctx = make_generic ctx c.cl_params tl p in
 	let name = (snd c.cl_path) ^ "_" ^ gctx.name in
 	try
-		load_instance ctx ({ tpackage = pack; tname = name; tparams = []; tsub = None },p) false
+		load_instance ctx ({ tpackage = pack; tname = name; tparams = []; tsub = None },p) false p
 	with Error(Module_not_found path,_) when path = (pack,name) ->
 		let m = (try Hashtbl.find ctx.g.modules (Hashtbl.find ctx.g.types_module c.cl_path) with Not_found -> assert false) in
 		let ctx = { ctx with m = { ctx.m with module_types = m.m_types @ ctx.m.module_types } } in
@@ -3964,7 +3969,7 @@ let rec build_generic ctx c p tl =
 (* HAXE.XML.PROXY *)
 
 let extend_xml_proxy ctx c t file p =
-	let t = load_complex_type ctx false (t,p) in
+	let t = load_complex_type ctx false p (t,p) in
 	let file = (try Common.find_file ctx.com file with Not_found -> file) in
 	add_dependency c.cl_module (create_fake_module ctx file);
 	let used = ref PMap.empty in

+ 18 - 18
src/typing/typer.ml

@@ -2810,7 +2810,7 @@ and type_access ctx e p mode =
 and type_vars ctx vl p =
 	let vl = List.map (fun ((v,pv),t,e) ->
 		try
-			let t = Typeload.load_type_hint ctx t in
+			let t = Typeload.load_type_hint ctx p t in
 			let e = (match e with
 				| None -> None
 				| Some e ->
@@ -3087,10 +3087,10 @@ and type_new ctx path el with_type p =
 			error "Constructor is not a function" p
 	in
 	let t = if (fst path).tparams <> [] then
-		follow (Typeload.load_instance ctx path false)
+		follow (Typeload.load_instance ctx path false p)
 	else try
 		ctx.call_argument_stack <- el :: ctx.call_argument_stack;
-		let t = follow (Typeload.load_instance ctx path true) in
+		let t = follow (Typeload.load_instance ctx path true p) in
 		ctx.call_argument_stack <- List.tl ctx.call_argument_stack;
 		(* Try to properly build @:generic classes here (issue #2016) *)
 		begin match t with
@@ -3199,7 +3199,7 @@ and type_try ctx e1 catches with_type p =
 		| [] , name -> name)
 	in
 	let catches = List.fold_left (fun acc ((v,pv),t,e) ->
-		let t = Typeload.load_complex_type ctx true t in
+		let t = Typeload.load_complex_type ctx true p t in
 		let rec loop t = match follow t with
 			| TInst ({ cl_kind = KTypeParameter _} as c,_) when not (Typeload.is_generic_parameter ctx c) ->
 				error "Cannot catch non-generic type parameter" p
@@ -3313,11 +3313,11 @@ and type_local_function ctx name f with_type p =
 	let old_tp,old_in_loop = ctx.type_params,ctx.in_loop in
 	ctx.type_params <- params @ ctx.type_params;
 	if not inline then ctx.in_loop <- false;
-	let rt = Typeload.load_type_hint ctx f.f_type in
+	let rt = Typeload.load_type_hint ctx p f.f_type in
 	let args = List.map (fun ((s,_),opt,_,t,c) ->
-		let t = Typeload.load_type_hint ctx t in
+		let t = Typeload.load_type_hint ctx p t in
 		let t, c = Typeload.type_function_arg ctx t c opt p in
-		s , c, t
+		s, c, t
 	) f.f_args in
 	(match with_type with
 	| WithType t ->
@@ -3658,7 +3658,7 @@ and type_expr ctx (e,p) (with_type:with_type) =
 		let e = type_expr ctx e Value in
 		mk (TCast (e,None)) (mk_mono()) p
 	| ECast (e, Some t) ->
-		let t = Typeload.load_complex_type ctx true t in
+		let t = Typeload.load_complex_type ctx true p t in
 		let check_param pt = match follow pt with
 			| TMono _ -> () (* This probably means that Dynamic wasn't bound (issue #4675). *)
 			| t when t == t_dynamic -> ()
@@ -3685,8 +3685,8 @@ and type_expr ctx (e,p) (with_type:with_type) =
 		mk (TCast (type_expr ctx e Value,Some texpr)) t p
 	| EDisplay (e,iscall) ->
 		handle_display ctx e iscall with_type
-	| EDisplayNew (t,_) ->
-		let t = Typeload.load_instance ctx (t,p) true in
+	| EDisplayNew t ->
+		let t = Typeload.load_instance ctx t true p in
 		(match follow t with
 		| TInst (c,params) | TAbstract({a_impl = Some c},params) ->
 			let ct, f = get_constructor ctx c params p in
@@ -3694,7 +3694,7 @@ and type_expr ctx (e,p) (with_type:with_type) =
 		| _ ->
 			error "Not a class" p)
 	| ECheckType (e,t) ->
-		let t = Typeload.load_complex_type ctx true t in
+		let t = Typeload.load_complex_type ctx true p t in
 		let e = type_expr ctx e (WithType t) in
 		let e = Codegen.AbstractCast.cast_or_unify ctx t e p in
 		if e.etype == t then e else mk (TCast (e,None)) t p
@@ -4560,14 +4560,14 @@ let make_macro_api ctx p =
 						{ tpackage = fst path; tname = snd path; tparams = []; tsub = None }
 				in
 				try
-					let m = Some (Typeload.load_instance ctx (tp,p) true) in
+					let m = Some (Typeload.load_instance ctx (tp,null_pos) true p) in
 					m
 				with Error (Module_not_found _,p2) when p == p2 ->
 					None
 			)
 		);
 		Interp.resolve_type = (fun t p ->
-			typing_timer ctx true (fun() -> Typeload.load_complex_type ctx false (t,p))
+			typing_timer ctx true (fun() -> Typeload.load_complex_type ctx false p (t,null_pos))
 		);
 		Interp.get_module = (fun s ->
 			typing_timer ctx true (fun() ->
@@ -4977,7 +4977,7 @@ let type_macro ctx mode cpath f (el:Ast.expr list) p =
 	let mctx, (margs,mret,mclass,mfield), call_macro = load_macro ctx (mode = MDisplay) cpath f p in
 	let mpos = mfield.cf_pos in
 	let ctexpr = { tpackage = ["haxe";"macro"]; tname = "Expr"; tparams = []; tsub = None } in
-	let expr = Typeload.load_instance mctx (ctexpr,p) false in
+	let expr = Typeload.load_instance mctx (ctexpr,null_pos) false p in
 	(match mode with
 	| MDisplay ->
 		()
@@ -4985,18 +4985,18 @@ let type_macro ctx mode cpath f (el:Ast.expr list) p =
 		unify mctx mret expr mpos;
 	| MBuild ->
 		let ctfields = { tpackage = []; tname = "Array"; tparams = [TPType (CTPath { tpackage = ["haxe";"macro"]; tname = "Expr"; tparams = []; tsub = Some "Field" },null_pos)]; tsub = None } in
-		let tfields = Typeload.load_instance mctx (ctfields,p) false in
+		let tfields = Typeload.load_instance mctx (ctfields,null_pos) false p in
 		unify mctx mret tfields mpos
 	| MMacroType ->
 		let cttype = { tpackage = ["haxe";"macro"]; tname = "Type"; tparams = []; tsub = None } in
-		let ttype = Typeload.load_instance mctx (cttype,p) false in
+		let ttype = Typeload.load_instance mctx (cttype,null_pos) false p in
 		try
 			unify_raise mctx mret ttype mpos;
 			(* TODO: enable this again in the future *)
 			(* ctx.com.warning "Returning Type from @:genericBuild macros is deprecated, consider returning ComplexType instead" p; *)
 		with Error (Unify _,_) ->
 			let cttype = { tpackage = ["haxe";"macro"]; tname = "Expr"; tparams = []; tsub = Some ("ComplexType") } in
-			let ttype = Typeload.load_instance mctx (cttype,p) false in
+			let ttype = Typeload.load_instance mctx (cttype,null_pos) false p in
 			unify_raise mctx mret ttype mpos;
 	);
 	(*
@@ -5112,7 +5112,7 @@ let type_macro ctx mode cpath f (el:Ast.expr list) p =
 						mk_mono()
 					else try
 						let ct = Interp.decode_ctype v in
-						Typeload.load_complex_type ctx false ct;
+						Typeload.load_complex_type ctx false p ct;
 					with Interp.Invalid_expr ->
 						Interp.decode_type v
 					in