Selaa lähdekoodia

[typer] allow overload for module statics

closes #9791
Simon Krajewski 5 vuotta sitten
vanhempi
commit
c4e6f1335e

+ 1 - 0
src/typing/typeloadFields.ml

@@ -1621,6 +1621,7 @@ let init_class ctx c p context_init herits fields =
 					else
 						let type_kind,path = match c.cl_kind with
 							| KAbstractImpl a -> "abstract",a.a_path
+							| KModuleFields m -> "module",m.m_path
 							| _ -> "class",c.cl_path
 						in
 						display_error ctx ("Duplicate " ^ type_kind ^ " field declaration : " ^ s_type_path path ^ "." ^ cf.cf_name) cf.cf_name_pos

+ 7 - 6
src/typing/typeloadModule.ml

@@ -194,7 +194,7 @@ let module_pass_1 ctx m tdecls loadp =
 	let com = ctx.com in
 	let decls = ref [] in
 	let statics = ref [] in
-	let check_name name meta p =
+	let check_name name meta also_statics p =
 		DeprecationCheck.check_is com name meta p;
 		let error prev_pos =
 			display_error ctx ("Name " ^ name ^ " is already defined in this module") p;
@@ -203,12 +203,13 @@ let module_pass_1 ctx m tdecls loadp =
 		List.iter (fun (t2,(_,p2)) ->
 			if snd (t_path t2) = name then error (t_infos t2).mt_name_pos
 		) !decls;
-		List.iter (fun (d,_) ->
-			if fst d.d_name = name then error (snd d.d_name)
-		) !statics
+		if also_statics then
+			List.iter (fun (d,_) ->
+				if fst d.d_name = name then error (snd d.d_name)
+			) !statics
 	in
 	let make_path name priv meta p =
-		check_name name meta p;
+		check_name name meta true p;
 		if priv then (fst m.m_path @ ["_" ^ snd m.m_path], name) else (fst m.m_path, name)
 	in
 	let has_declaration = ref false in
@@ -223,7 +224,7 @@ let module_pass_1 ctx m tdecls loadp =
 			if !has_declaration then error "import and using may not appear after a declaration" p;
 			acc
 		| EStatic d ->
-			check_name (fst d.d_name) d.d_meta (snd d.d_name);
+			check_name (fst d.d_name) d.d_meta false (snd d.d_name);
 			has_declaration := true;
 			statics := (d,p) :: !statics;
 			acc;

+ 1 - 2
tests/misc/projects/Issue9631/compile-fields-fail.hxml.stderr

@@ -1,2 +1 @@
-Fields.hx:5: characters 10-14 : Name main is already defined in this module
-Fields.hx:1: characters 10-14 : ... Previous declaration here
+Fields.hx:5: characters 10-14 : Duplicate module field declaration : Fields.main

+ 2 - 2
tests/unit/src/unit/issues/Issue9791.hx

@@ -1,6 +1,6 @@
 package unit.issues;
 
-#if false
+#if java
 
 overload function moduleOverload(i:Int) {
 	return "Int: " + i;
@@ -13,7 +13,7 @@ overload function moduleOverload(s:String) {
 #end
 
 class Issue9791 extends unit.Test {
-	#if false
+	#if java
 	function test() {
 		eq("Int: 12", moduleOverload(12));
 		eq("String: foo", moduleOverload("foo"));