checker: fix `instance.cb_field(mut arg)`

pull/8142/head
Delyan Angelov 2021-01-15 19:16:34 +02:00
parent 1c6fe83408
commit 263fb7d7a8
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 35 additions and 3 deletions

View File

@ -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
}
}

View File

@ -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]
}