Преглед на файлове

[typer] fail better when importing nonexistent subtype

closes #10897
Simon Krajewski преди 2 години
родител
ревизия
991df8cf35

+ 17 - 5
src/context/display/importHandling.ml

@@ -86,8 +86,16 @@ let init_import ctx context_init path mode p =
 		let error_private p = typing_error "Importing private declarations from a module is not allowed" p in
 		let chk_private t p = if ctx.m.curmod != (t_infos t).mt_module && (t_infos t).mt_private then error_private p in
 		let has_name name t = snd (t_infos t).mt_path = name in
+		let fail_usefully name =
+			typing_error (StringError.string_error name (List.map (fun mt -> snd (t_infos mt).mt_path) types) ("Module " ^ s_type_path md.m_path ^ " does not define type " ^ name)) p_type
+		in
+		let find_type tname = List.find (has_name tname) types in
 		let get_type tname =
-			let t = (try List.find (has_name tname) types with Not_found -> typing_error (StringError.string_error tname (List.map (fun mt -> snd (t_infos mt).mt_path) types) ("Module " ^ s_type_path md.m_path ^ " does not define type " ^ tname)) p_type) in
+			let t = try
+				find_type tname
+			with Not_found ->
+				fail_usefully tname
+			in
 			chk_private t p_type;
 			t
 		in
@@ -147,12 +155,16 @@ let init_import ctx context_init path mode p =
 				with Not_found ->
 					(* this might be a static property, wait later to check *)
 					let find_main_type_static () =
-						let tmain = get_type tname in
 						try
-							add_static_init tmain name tsub
+							let tmain = find_type tname in
+							begin try
+								add_static_init tmain name tsub
+							with Not_found ->
+								(* TODO: mention module-level declarations in the error message? *)
+								display_error ctx.com (s_type_path (t_infos tmain).mt_path ^ " has no field or subtype " ^ tsub) p
+							end
 						with Not_found ->
-							(* TODO: mention module-level declarations in the error message? *)
-							display_error ctx.com (s_type_path (t_infos tmain).mt_path ^ " has no field or subtype " ^ tsub) p
+							fail_usefully tsub
 					in
 					context_init#add (fun() ->
 						match md.m_statics with

+ 3 - 0
tests/misc/projects/Issue10897/Main.hx

@@ -0,0 +1,3 @@
+import Types.Baz;
+
+function main() {}

+ 1 - 0
tests/misc/projects/Issue10897/Types.hx

@@ -0,0 +1 @@
+enum Bar {}

+ 2 - 0
tests/misc/projects/Issue10897/compile-fail.hxml

@@ -0,0 +1,2 @@
+--main Main
+--interp

+ 1 - 0
tests/misc/projects/Issue10897/compile-fail.hxml.stderr

@@ -0,0 +1 @@
+Main.hx:1: characters 8-13 : Module Types does not define type Baz (Suggestion: Bar)