浏览代码

better error message for inaccessible private types (closes #9243)

Aleksandr Kuzmenko 5 年之前
父节点
当前提交
134f69b506

+ 7 - 2
src/core/error.ml

@@ -13,7 +13,7 @@ type call_error =
 
 and error_msg =
 	| Module_not_found of path
-	| Type_not_found of path * string
+	| Type_not_found of path * string * type_not_found_reason
 	| Unify of unify_error list
 	| Custom of string
 	| Unknown_ident of string
@@ -21,6 +21,10 @@ and error_msg =
 	| Call_error of call_error
 	| No_constructor of module_type
 
+and type_not_found_reason =
+	| Private_type
+	| Not_defined
+
 exception Fatal_error of string * Globals.pos
 exception Error of error_msg * Globals.pos
 
@@ -260,7 +264,8 @@ end
 
 let rec error_msg = function
 	| Module_not_found m -> "Type not found : " ^ s_type_path m
-	| Type_not_found (m,t) -> "Module " ^ s_type_path m ^ " does not define type " ^ t
+	| Type_not_found (m,t,Private_type) -> "Cannot access private type " ^ t ^ " in module " ^ s_type_path m
+	| Type_not_found (m,t,Not_defined) -> "Module " ^ s_type_path m ^ " does not define type " ^ t
 	| Unify l -> BetterErrors.better_error_message l
 	| Unknown_ident s -> "Unknown identifier : " ^ s
 	| Custom s -> s

+ 11 - 2
src/typing/typeload.ml

@@ -88,9 +88,18 @@ let find_type_in_module m tname =
 (* raises Type_not_found *)
 let find_type_in_module_raise m tname p =
 	try
-		find_type_in_module m tname
+		List.find (fun mt ->
+			let infos = t_infos mt in
+			if snd infos.mt_path = tname then
+				if infos.mt_private then
+					raise_error (Type_not_found (m.m_path,tname,Private_type)) p
+				else
+					true
+			else
+				false
+		) m.m_types
 	with Not_found ->
-		raise_error (Type_not_found (m.m_path,tname)) p
+		raise_error (Type_not_found (m.m_path,tname,Not_defined)) p
 
 (* raises Module_not_found or Type_not_found *)
 let load_type_raise ctx mpath tname p =

+ 1 - 1
src/typing/typer.ml

@@ -1293,7 +1293,7 @@ and handle_efield ctx e p0 mode =
 							let mpath = (pack,name) in
 							if Hashtbl.mem ctx.g.modules mpath then
 								let tname = Option.default name sub in
-								raise (Error (Type_not_found (mpath,tname),p))
+								raise (Error (Type_not_found (mpath,tname,Not_defined),p))
 							else
 								raise (Error (Module_not_found mpath,p))
 						end

+ 1 - 1
src/typing/typerDisplay.ml

@@ -525,7 +525,7 @@ let handle_display ?resume_typing ctx e_ast dk with_type =
 	with Error (Unknown_ident n,_) when ctx.com.display.dms_kind = DMDefault ->
         if dk = DKDot && is_legacy_completion ctx.com then raise (Parser.TypePath ([n],None,false,p))
 		else raise_toplevel ctx dk with_type (n,p)
-	| Error ((Type_not_found (path,_) | Module_not_found path),_) as err when ctx.com.display.dms_kind = DMDefault ->
+	| Error ((Type_not_found (path,_,_) | Module_not_found path),_) as err when ctx.com.display.dms_kind = DMDefault ->
 		if is_legacy_completion ctx.com then begin try
 			raise_fields (DisplayFields.get_submodule_fields ctx path) (CRField((make_ci_module path),p,None,None)) (make_subject None (pos e_ast))
 		with Not_found ->

+ 5 - 0
tests/misc/projects/Issue9243/Main.hx

@@ -0,0 +1,5 @@
+private class Main {
+	static function main() {
+		trace("Test");
+	}
+}

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

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

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

@@ -0,0 +1 @@
+Cannot access private type Main in module Main