Browse Source

emit an error on static @:to without arguments (fixes #8840)

Aleksandr Kuzmenko 6 years ago
parent
commit
b138fca078

+ 11 - 0
src/typing/typeloadFields.ml

@@ -872,6 +872,17 @@ let check_abstract (ctx,cctx,fctx) c cf fd t ret p =
 					a.a_from_field <- (TLazy r,cf) :: a.a_from_field;
 					a.a_from_field <- (TLazy r,cf) :: a.a_from_field;
 				| (Meta.To,_,_) :: _ ->
 				| (Meta.To,_,_) :: _ ->
 					if fctx.is_macro then error (cf.cf_name ^ ": Macro cast functions are not supported") p;
 					if fctx.is_macro then error (cf.cf_name ^ ": Macro cast functions are not supported") p;
+					(match cf.cf_kind, cf.cf_type with
+					| Var _, _ ->
+						error "@:to meta should be used on methods" p
+					| Method _, TFun(args, _) when not fctx.is_abstract_member && List.length args <> 1 ->
+						if not (Meta.has Meta.MultiType a.a_meta) then (* TODO: get rid of this check once multitype is removed *)
+						error ("static @:to method should have one argument") p
+					| Method _, TFun(args, _) when fctx.is_abstract_member && List.length args <> 1 ->
+						if not (Meta.has Meta.MultiType a.a_meta) then (* TODO: get rid of this check once multitype is removed *)
+						error "@:to method should have no arguments" p
+					| _ -> ()
+					);
 					(* TODO: this doesn't seem quite right... *)
 					(* TODO: this doesn't seem quite right... *)
 					if not (Meta.has Meta.Impl cf.cf_meta) then cf.cf_meta <- (Meta.Impl,[],null_pos) :: cf.cf_meta;
 					if not (Meta.has Meta.Impl cf.cf_meta) then cf.cf_meta <- (Meta.Impl,[],null_pos) :: cf.cf_meta;
 					let resolve_m args =
 					let resolve_m args =

+ 11 - 0
tests/misc/projects/Issue8840/Main.hx

@@ -0,0 +1,11 @@
+class Main {
+	static function main() {}
+}
+
+abstract Abstr(String) {
+	@:to public static function staticTo():String
+		return '';
+
+	@:to public function instanceTo(a:Int):String
+		return '';
+}

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

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

+ 2 - 0
tests/misc/projects/Issue8840/compile-fail.hxml.stderr

@@ -0,0 +1,2 @@
+Main.hx:6: lines 6-7 : static @:to method should have one argument
+Main.hx:9: lines 9-10 : @:to method should have no arguments