Browse Source

check for duplicated __init__ methods (closes #9368)

Aleksandr Kuzmenko 5 years ago
parent
commit
aa30295cfc

+ 7 - 0
src/typing/typeloadFields.ml

@@ -1488,12 +1488,19 @@ let init_class ctx c p context_init herits fields =
 	in
 	let cl_if_feature = check_if_feature c.cl_meta in
 	let cl_req = check_require c.cl_meta in
+	let has_init = ref false in
 	List.iter (fun f ->
 		let p = f.cff_pos in
 		try
 			let ctx,fctx = create_field_context (ctx,cctx) c f in
 			if fctx.is_field_debug then print_endline ("Created field context: " ^ dump_field_context fctx);
 			let cf = init_field (ctx,cctx,fctx) f in
+			if fctx.field_kind = FKInit then begin
+				if !has_init then
+					display_error ctx ("Duplicate class field declaration : " ^ (s_type_path c.cl_path) ^ "." ^ cf.cf_name) cf.cf_name_pos
+				else
+					has_init := true
+			end;
 			if fctx.is_field_debug then print_endline ("Created field: " ^ Printer.s_tclass_field "" cf);
 			if fctx.is_static && c.cl_interface && fctx.field_kind <> FKInit && not cctx.is_lib && not (c.cl_extern) then
 				error "You can't declare static fields in interfaces" p;

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

@@ -0,0 +1,5 @@
+class Main {
+	static function __init__() {}
+	static function __init__() {}
+	static function main() {}
+}

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

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

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

@@ -0,0 +1 @@
+Main.hx:3: characters 18-26 : Duplicate class field declaration : Main.__init__