From 880223c2dd35f9927dbdd6e494750a5aa97b1074 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 23 May 2022 03:16:46 +0800 Subject: [PATCH] checker: check method call argument type mismatch (#14496) --- vlib/v/checker/fn.v | 6 ++---- vlib/v/checker/tests/method_call_arg_mismatch.out | 6 ++++++ vlib/v/checker/tests/method_call_arg_mismatch.vv | 10 ++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 vlib/v/checker/tests/method_call_arg_mismatch.out create mode 100644 vlib/v/checker/tests/method_call_arg_mismatch.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 78227bda70..967ae75250 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1461,10 +1461,8 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { continue } } - if got_arg_typ != ast.void_type { - c.error('$err.msg() in argument ${i + 1} to `${left_sym.name}.$method_name`', - arg.pos) - } + c.error('$err.msg() in argument ${i + 1} to `${left_sym.name}.$method_name`', + arg.pos) } } if method.is_unsafe && !c.inside_unsafe { diff --git a/vlib/v/checker/tests/method_call_arg_mismatch.out b/vlib/v/checker/tests/method_call_arg_mismatch.out new file mode 100644 index 0000000000..d8a781261d --- /dev/null +++ b/vlib/v/checker/tests/method_call_arg_mismatch.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/method_call_arg_mismatch.vv:9:10: error: `baz()` (no value) used as value in argument 1 to `Foo.bar` + 7 | fn main() { + 8 | foo := Foo{} + 9 | foo.bar(baz()) + | ~~~~~ + 10 | } diff --git a/vlib/v/checker/tests/method_call_arg_mismatch.vv b/vlib/v/checker/tests/method_call_arg_mismatch.vv new file mode 100644 index 0000000000..eb3a5f8486 --- /dev/null +++ b/vlib/v/checker/tests/method_call_arg_mismatch.vv @@ -0,0 +1,10 @@ +struct Foo {} + +fn (f Foo) bar(baz fn ()) {} + +fn baz() {} + +fn main() { + foo := Foo{} + foo.bar(baz()) +}