소스 검색

[typer] clean up import error handling

closes #9142
Simon Krajewski 5 년 전
부모
커밋
3124284b9c
4개의 변경된 파일162개의 추가작업 그리고 8개의 파일을 삭제
  1. 9 6
      src/typing/typeloadModule.ml
  2. 3 1
      tests/display/.vscode/launch.json
  3. 7 1
      tests/display/src/cases/Issue5306.hx
  4. 143 0
      tests/display/src/cases/Issue9142.hx

+ 9 - 6
src/typing/typeloadModule.ml

@@ -405,12 +405,14 @@ let init_module_type ctx context_init (decl,p) =
 	let get_type name =
 		try List.find (fun t -> snd (t_infos t).mt_path = name) ctx.m.curmod.m_types with Not_found -> assert false
 	in
-	let check_path_display path p =
+	let commit_import path mode p =
+		ctx.m.module_imports <- (path,mode) :: ctx.m.module_imports;
 		if Filename.basename p.pfile <> "import.hx" then ImportHandling.add_import_position ctx p path;
+	in
+	let check_path_display path p =
 		if DisplayPosition.display_position#is_in_file p.pfile then DisplayPath.handle_path_display ctx path p
 	in
 	let init_import path mode =
-		ctx.m.module_imports <- (path,mode) :: ctx.m.module_imports;
 		check_path_display path p;
 		let rec loop acc = function
 			| x :: l when is_lower_ident (fst x) -> loop (x::acc) l
@@ -497,7 +499,7 @@ let init_module_type ctx context_init (decl,p) =
 							try
 								add_static_init tmain name tsub
 							with Not_found ->
-								error (s_type_path (t_infos tmain).mt_path ^ " has no field or subtype " ^ tsub) p
+								display_error ctx (s_type_path (t_infos tmain).mt_path ^ " has no field or subtype " ^ tsub) p
 						))
 				| (tsub,p2) :: (fname,p3) :: rest ->
 					(match rest with
@@ -508,7 +510,7 @@ let init_module_type ctx context_init (decl,p) =
 						try
 							add_static_init tsub name fname
 						with Not_found ->
-							error (s_type_path (t_infos tsub).mt_path ^ " has no field " ^ fname) (punion p p3)
+							display_error ctx (s_type_path (t_infos tsub).mt_path ^ " has no field " ^ fname) (punion p p3)
 					);
 				)
 			| IAll ->
@@ -533,8 +535,9 @@ let init_module_type ctx context_init (decl,p) =
 	match decl with
 	| EImport (path,mode) ->
 		begin try
-			init_import path mode
-		with Error(Module_not_found _ as err,p) ->
+			init_import path mode;
+			commit_import path mode p;
+		with Error(err,p) ->
 			display_error ctx (Error.error_msg err) p
 		end
 	| EUsing path ->

+ 3 - 1
tests/display/.vscode/launch.json

@@ -5,7 +5,9 @@
 			"name": "Interpreter",
 			"type": "haxe-eval",
 			"request": "launch",
-			"args": ["build.hxml", "-lib", "test-adapter"]
+			"args": [
+				"build.hxml"
+			]
 		}
 	]
 }

+ 7 - 1
tests/display/src/cases/Issue5306.hx

@@ -7,7 +7,7 @@ class Issue5306 extends DisplayTestCase {
 		class Main {
 			static function main() {
 				var ib:Array<Int>;
-				ib[0] = 0; ib[1] = 1; ib[2]
+				ib[0] = 0; ib[1] = 1; {-7-}ib[2]{-8-}
 				{-5-}trace{-6-}("test");
 			}
 		}
@@ -31,6 +31,12 @@ class Issue5306 extends DisplayTestCase {
 				range: diagnosticsRange(pos(5), pos(6)),
 				severity: Error,
 				args: "Missing ;"
+			},
+			{
+				kind: DKCompilerError,
+				range: diagnosticsRange(pos(7), pos(8)),
+				severity: Warning,
+				args: "This code has no effect"
 			}
 		];
 		arrayEq(expected, diagnostics());

+ 143 - 0
tests/display/src/cases/Issue9142.hx

@@ -13,4 +13,147 @@ class Issue9142 extends DisplayTestCase {
 	function testNonExistentImport() {
 		eq("String", type(pos(1)));
 	}
+
+	/**
+		import lowercase;
+
+		class Main {
+			static function main() {
+				"fo{-1-}o"
+			}
+		}
+	**/
+	function testLowercaseImport() {
+		eq("String", type(pos(1)));
+	}
+
+	/**
+		import haxe.Int64.__Int64;
+
+		class Main {
+			static function main() {
+				"fo{-1-}o"
+			}
+		}
+	**/
+	function testPrivateImport() {
+		eq("String", type(pos(1)));
+	}
+
+	/**
+		import StringTools as st;
+
+		class Main {
+			static function main() {
+				"fo{-1-}o"
+			}
+		}
+	**/
+	function testLowercaseAliasImport() {
+		eq("String", type(pos(1)));
+	}
+
+	/**
+		import StringTools.NonExistent;
+
+		class Main {
+			static function main() {
+				"fo{-1-}o"
+			}
+		}
+	**/
+	function testNonExistentSubtypeImport() {
+		eq("String", type(pos(1)));
+	}
+
+	/**
+		import StringTools.StringTools.NonExistent;
+
+		class Main {
+			static function main() {
+				"fo{-1-}o"
+			}
+		}
+	**/
+	function testNonExistentSubtypeFieldImport() {
+		eq("String", type(pos(1)));
+	}
+
+	/**
+		import StringTools.StringTools.nonExistent;
+
+		class Main {
+			static function main() {
+				"fo{-1-}o"
+			}
+		}
+	**/
+	function testNonExistentSubtypeFieldImport2() {
+		eq("String", type(pos(1)));
+	}
+
+	/**
+		import StringTools.StrongTools.NonExistent;
+
+		class Main {
+			static function main() {
+				"fo{-1-}o"
+			}
+		}
+	**/
+	function testNonExistentSubtypeFieldImport3() {
+		eq("String", type(pos(1)));
+	}
+
+	/**
+		import StringTools.StrongTools.nonExistent;
+
+		class Main {
+			static function main() {
+				"fo{-1-}o"
+			}
+		}
+	**/
+	function testNonExistentSubtypeFieldImport4() {
+		eq("String", type(pos(1)));
+	}
+
+	/**
+		import StringTools.StringTools.StringTools.StringTools;
+
+		class Main {
+			static function main() {
+				"fo{-1-}o"
+			}
+		}
+	**/
+	function testTooMuchImport() {
+		eq("String", type(pos(1)));
+	}
+
+	/**
+		import StringTools.StringTools.StringTools.StringTools.*;
+
+		class Main {
+			static function main() {
+				"fo{-1-}o"
+			}
+		}
+	**/
+	function testTooMuchImportAll() {
+		eq("String", type(pos(1)));
+	}
+
+	/**
+		import StringTools.StrongTools.*;
+
+		class Main {
+			static function main() {
+				"fo{-1-}o"
+			}
+		}
+	**/
+	function testNonExistentSubtypeAll() {
+		eq("String", type(pos(1)));
+	}
 }