diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index c11794c4b4..f1a33cf973 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1534,10 +1534,13 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { call_expr.is_field = true info := field_type_sym.info as table.FnType call_expr.return_type = info.func.return_type - // TODO: check args (do it once for all of the above) - for arg in call_expr.args { - c.expr(arg.expr) + mut earg_types := []table.Type{} + for mut arg in call_expr.args { + targ := c.expr(arg.expr) + arg.typ = targ + earg_types << targ } + call_expr.expected_arg_types = earg_types return info.func.return_type } } diff --git a/vlib/v/tests/struct_fields_storing_functions_test.v b/vlib/v/tests/struct_fields_storing_functions_test.v new file mode 100644 index 0000000000..5687bad582 --- /dev/null +++ b/vlib/v/tests/struct_fields_storing_functions_test.v @@ -0,0 +1,29 @@ +type Async_cb = fn (x []byte, mut y []byte) int + +fn async_cb(b []byte, mut res []byte) int { + if b.len > 0 { + res << b + } + return 0 +} + +struct Ep_arg { +mut: + sfd int + cb Async_cb +} + +fn test_struct_fn_field_can_be_used_directly() { + buf := [byte(1), 2, 3] + mut res := []byte{} + res << 0x88 + async_cb(buf[0..2], mut res) + data := Ep_arg{ + sfd: 1234 + cb: async_cb + } + data.cb(buf[1..2], mut res) + res << 0x99 + eprintln(res) + assert res == [byte(0x88), 0x01, 0x02, 0x02, 0x99] +}