From 04be2465dc5203e5c72e157130af4b9ccdfd9dfb Mon Sep 17 00:00:00 2001
From: pancake <pancake@nowsecure.com>
Date: Sun, 26 Dec 2021 10:53:56 +0100
Subject: [PATCH] cgen: check call argument on methods (#12965)

---
 vlib/v/checker/fn.v                                        | 7 +++++++
 .../tests/js_with_non_js_backend_too_many_arguments.out    | 5 +++++
 .../tests/js_with_non_js_backend_too_many_arguments.vv     | 3 +++
 3 files changed, 15 insertions(+)
 create mode 100644 vlib/v/checker/tests/js_with_non_js_backend_too_many_arguments.out
 create mode 100644 vlib/v/checker/tests/js_with_non_js_backend_too_many_arguments.vv

diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v
index 1340724600..bc143f8e0e 100644
--- a/vlib/v/checker/fn.v
+++ b/vlib/v/checker/fn.v
@@ -654,7 +654,14 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
 			node.expected_arg_types << param.typ
 		}
 	}
+	if !c.pref.backend.is_js() && node.args.len > 0 && func.params.len == 0 {
+		c.error('too many arguments in call to `$func.name` (non-js backend: $c.pref.backend)',
+			node.pos)
+	}
 	for i, mut call_arg in node.args {
+		if func.params.len == 0 {
+			continue
+		}
 		param := if func.is_variadic && i >= func.params.len - 1 {
 			func.params[func.params.len - 1]
 		} else {
diff --git a/vlib/v/checker/tests/js_with_non_js_backend_too_many_arguments.out b/vlib/v/checker/tests/js_with_non_js_backend_too_many_arguments.out
new file mode 100644
index 0000000000..158dde5e77
--- /dev/null
+++ b/vlib/v/checker/tests/js_with_non_js_backend_too_many_arguments.out
@@ -0,0 +1,5 @@
+vlib/v/checker/tests/js_with_non_js_backend_too_many_arguments.vv:3:4: error: too many arguments in call to `JS.Foo.bar` (non-js backend: c)
+    1 | fn JS.Foo.bar() bool
+    2 | 
+    3 | JS.Foo.bar(123)
+      |    ~~~~~~~~~~~~
diff --git a/vlib/v/checker/tests/js_with_non_js_backend_too_many_arguments.vv b/vlib/v/checker/tests/js_with_non_js_backend_too_many_arguments.vv
new file mode 100644
index 0000000000..22389f04b8
--- /dev/null
+++ b/vlib/v/checker/tests/js_with_non_js_backend_too_many_arguments.vv
@@ -0,0 +1,3 @@
+fn JS.Foo.bar() bool
+
+JS.Foo.bar(123)