Skip to content

Commit

Permalink
fix fn type call
Browse files Browse the repository at this point in the history
  • Loading branch information
lcddh authored and medvednikov committed Dec 18, 2019
1 parent 4b7aa4e commit 2f218b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 12 additions & 4 deletions vlib/compiler/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -1810,8 +1810,8 @@ fn (p mut Parser) var_expr(v Var) string {
p.next()
mut typ := v.typ
// Function pointer?
if typ.starts_with('fn ') && p.tok == .lpar {
T := p.table.find_type(typ)
if p.base_type(typ).starts_with('fn ') && p.tok == .lpar {
T := p.table.find_type(p.base_type(typ))
p.gen('(')
p.fn_call_args(mut T.func)
p.gen(')')
Expand All @@ -1820,6 +1820,13 @@ fn (p mut Parser) var_expr(v Var) string {
// users[0].name
if p.tok == .lsbr {
typ = p.index_expr(typ, fn_ph)
if p.base_type(typ).starts_with('fn ') && p.tok == .lpar {
T := p.table.find_type(p.base_type(typ))
p.gen('(')
p.fn_call_args(mut T.func)
p.gen(')')
typ = T.func.typ
}
}
// a.b.c().d chain
// mut dc := 0
Expand Down Expand Up @@ -1984,8 +1991,9 @@ pub:
}
', fname_tidx)
}
if p.base_type(field.typ).starts_with('fn ') && p.peek() == .lpar {
tmp_typ := p.table.find_type(field.typ)
base := p.base_type(field.typ)
if base.starts_with('fn ') && p.peek() == .lpar {
tmp_typ := p.table.find_type(base)
mut f := tmp_typ.func
p.gen('.$field.name')
p.gen('(')
Expand Down
16 changes: 16 additions & 0 deletions vlib/compiler/tests/fn_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,21 @@ fn test_assert_in_bool_fn() {
assert_in_bool_fn(2)
}

type MyFn fn (int) int
fn test(n int) int {
return n + 1000
}
struct MySt {
f MyFn
}
fn test_fn_type_call() {
mut arr := []MyFn
arr << MyFn(test)
assert arr[0](10) == 1010

st := MySt{f:test}
assert st.f(10) == 1010
}



0 comments on commit 2f218b8

Please sign in to comment.