diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index badcde628e..dd525cfd6b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1194,6 +1194,11 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { } if typ_sym.kind == .array_fixed { } + if typ_sym.kind == .function && arg_typ_sym.kind == .function { + candidate_fn_name := if typ_sym.name.starts_with('anon_') { 'anonymous function' } else { 'fn `$typ_sym.name`' } + c.error('cannot use $candidate_fn_name as function type `$arg_typ_sym.str()` in argument ${i+1} to `$fn_name`', + call_expr.pos) + } c.error('cannot use type `$typ_sym.str()` as type `$arg_typ_sym.str()` in argument ${i+1} to `$fn_name`', call_expr.pos) } diff --git a/vlib/v/checker/tests/non_matching_functional_args.out b/vlib/v/checker/tests/non_matching_functional_args.out new file mode 100644 index 0000000000..8c823c43e9 --- /dev/null +++ b/vlib/v/checker/tests/non_matching_functional_args.out @@ -0,0 +1,14 @@ +vlib/v/checker/tests/non_matching_functional_args.v:27:2: error: cannot use anonymous function as function type `MyFn` in argument 1 to `sum` + 25 | + 26 | fn main() { + 27 | sum(fn (mut t Table) { + | ~~~~~~~~~~~~~~~~~~~~~~ + 28 | t.rename() + 29 | println(t.name) +vlib/v/checker/tests/non_matching_functional_args.v:31:2: error: cannot use fn `xxx` as function type `MyFn` in argument 1 to `sum` + 29 | println(t.name) + 30 | }) + 31 | sum(xxx) + | ~~~~~~~~ + 32 | sum(yyy) + 33 | } diff --git a/vlib/v/checker/tests/non_matching_functional_args.vv b/vlib/v/checker/tests/non_matching_functional_args.vv new file mode 100644 index 0000000000..59ac7d05ee --- /dev/null +++ b/vlib/v/checker/tests/non_matching_functional_args.vv @@ -0,0 +1,33 @@ +struct Table { +pub mut: + name string +} + +type MyFn = fn (zzzz Table) + +fn (mut t Table) rename() { + t.name = 'abc' +} + +fn yyy(t Table) { + println(t.name) +} + +fn xxx(mut t Table) { + t.rename() + println(t.name) +} + +fn sum(myfn MyFn) { + mut t := Table{} + myfn(t) +} + +fn main() { + sum(fn (mut t Table) { + t.rename() + println(t.name) + }) + sum(xxx) + sum(yyy) +}