Browse Source

extract find_type_in_current_module_context from load_type_def

and use it in resolve_dot_path instead of duplicating code
Dan Korostelev 5 years ago
parent
commit
2687cdf32c
2 changed files with 21 additions and 25 deletions
  1. 18 14
      src/typing/typeload.ml
  2. 3 11
      src/typing/typerDotPath.ml

+ 18 - 14
src/typing/typeload.ml

@@ -101,31 +101,35 @@ with Error((Module_not_found _ | Type_not_found _),p2) when p = p2 ->
 
 (** since load_type_def and load_instance are used in PASS2, they should not access the structure of a type **)
 
+let find_type_in_current_module_context ctx pack name =
+	let no_pack = pack = [] in
+	let path_matches t2 =
+		let tp = t_path t2 in
+		(* see also https://github.com/HaxeFoundation/haxe/issues/9150 *)
+		tp = (pack,name) || (no_pack && snd tp = name)
+	in
+	try
+		(* Check the types in our own module *)
+		List.find path_matches ctx.m.curmod.m_types
+	with Not_found ->
+		(* Check the local imports *)
+		let t,pi = List.find (fun (t2,pi) -> path_matches t2) ctx.m.module_types in
+		ImportHandling.mark_import_position ctx pi;
+		t
+
 (*
 	load a type or a subtype definition
 *)
 let load_type_def ctx p t =
-	let no_pack = t.tpackage = [] in
 	if t = Parser.magic_type_path then raise_fields (DisplayToplevel.collect ctx TKType NoValue true) CRTypeHint (DisplayTypes.make_subject None p);
 	(* The type name is the module name or the module sub-type name *)
 	let tname = (match t.tsub with None -> t.tname | Some n -> n) in
 	try
 		(* If there's a sub-type, there's no reason to look in our module or its imports *)
 		if t.tsub <> None then raise Not_found;
-		let path_matches t2 =
-			let tp = t_path t2 in
-			tp = (t.tpackage,tname) || (no_pack && snd tp = tname)
-		in
-		try
-			(* Check the types in our own module *)
-			List.find path_matches ctx.m.curmod.m_types
-		with Not_found ->
-			(* Check the local imports *)
-			let t,pi = List.find (fun (t2,pi) -> path_matches t2) ctx.m.module_types in
-			ImportHandling.mark_import_position ctx pi;
-			t
+		find_type_in_current_module_context ctx t.tpackage tname
 	with
-	| Not_found when no_pack ->
+	| Not_found when t.tpackage = [] ->
 		(* Unqualified *)
 		begin try
 			let rec loop l = match l with

+ 3 - 11
src/typing/typerDotPath.ml

@@ -100,17 +100,9 @@ let resolve_dot_path ctx (path_parts : dot_path_part list) =
 				| [] ->
 					(* no package was specified - Unqualified access *)
 					(try
-						(* first try getting a type by `name` in current module types and current imports
-							and try accessing its static field by `sname` *)
-						let path_match t = snd (t_infos t).mt_path = name in
-						let t =
-							try
-								List.find path_match ctx.m.curmod.m_types (* types in this modules *)
-							with Not_found ->
-								let t,p = List.find (fun (t,_) -> path_match t) ctx.m.module_types in (* imported types *)
-								ImportHandling.mark_import_position ctx p;
-								t
-						in
+						(* first try getting the type from the current module context
+						   and try accessing its static field by `sname` *)
+						let t = Typeload.find_type_in_current_module_context ctx pack name in
 						get_static true t
 					with Not_found ->
 						(* if the static field (or the type) wasn't not found, look for a subtype instead - #1916