checker: use a more readable error message for non matching fn args
parent
db4a9d6b59
commit
0637feb382
|
@ -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 == .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`',
|
c.error('cannot use type `$typ_sym.str()` as type `$arg_typ_sym.str()` in argument ${i+1} to `$fn_name`',
|
||||||
call_expr.pos)
|
call_expr.pos)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 | }
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue