Browse Source

error if child method has different @:nativeName (fixes #5949)

Aleksandr Kuzmenko 6 years ago
parent
commit
15f2e92441

+ 1 - 17
src/filters/filters.ml

@@ -25,23 +25,7 @@ open Error
 open Globals
 open FiltersCommon
 
-(** retrieve string from @:native metadata or raise Not_found *)
-let get_native_name meta =
-	let rec get_native meta = match meta with
-		| [] -> raise Not_found
-		| (Meta.Native,[v],p as meta) :: _ ->
-			meta
-		| _ :: meta ->
-			get_native meta
-	in
-	let (_,e,mp) = get_native meta in
-	match e with
-	| [Ast.EConst (Ast.String name),p] ->
-		name,p
-	| [] ->
-		raise Not_found
-	| _ ->
-		error "String expected" mp
+let get_native_name = TypeloadCheck.get_native_name
 
 (* PASS 1 begin *)
 

+ 30 - 0
src/typing/typeloadCheck.ml

@@ -130,6 +130,35 @@ let copy_meta meta_src meta_target sl =
 	) meta_src;
 	!meta
 
+(** retrieve string from @:native metadata or raise Not_found *)
+let get_native_name meta =
+	let rec get_native meta = match meta with
+		| [] -> raise Not_found
+		| (Meta.Native,[v],p as meta) :: _ ->
+			meta
+		| _ :: meta ->
+			get_native meta
+	in
+	let (_,e,mp) = get_native meta in
+	match e with
+	| [Ast.EConst (Ast.String name),p] ->
+		name,p
+	| [] ->
+		raise Not_found
+	| _ ->
+		error "String expected" mp
+
+let check_native_name_override ctx child base =
+	let error() =
+		display_error ctx ("Field " ^ child.cf_name ^ " has different @:nativeName value than in superclass") child.cf_pos;
+		display_error ctx ("Base field is defined here") base.cf_pos
+	in
+	try
+		let native_name = fst (get_native_name child.cf_meta) in
+		try if fst (get_native_name base.cf_meta) <> native_name then error()
+		with Not_found -> error()
+	with Not_found -> ()
+
 let check_overriding ctx c f =
 	match c.cl_super with
 	| None ->
@@ -142,6 +171,7 @@ let check_overriding ctx c f =
 			(if is_overload && not (Meta.has Meta.Overload f.cf_meta) then
 				display_error ctx ("Missing @:overload declaration for field " ^ i) p);
 			let t, f2 = get_super_field csup i in
+			check_native_name_override ctx f f2;
 			(* allow to define fields that are not defined for this platform version in superclass *)
 			(match f2.cf_kind with
 			| Var { v_read = AccRequire _ } -> raise Not_found;

+ 12 - 0
tests/misc/projects/Issue5949/Main.hx

@@ -0,0 +1,12 @@
+class Main {
+	static function main() {}
+}
+
+class C1 {
+	public function a() {}
+}
+
+@:keep
+class C2 extends C1 {
+	@:native("z") override public function a() {}
+}

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

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

+ 3 - 0
tests/misc/projects/Issue5949/compile-fail.hxml.stderr

@@ -0,0 +1,3 @@
+Main.hx:11: characters 16-47 : Field a has different @:nativeName value than in superclass
+Main.hx:6: characters 2-24 : Base field is defined here
+Main.hx:10: lines 10-12 : Defined in this class