From 2f218b878bfd72de2ae546dc50d8d3dd6eacac15 Mon Sep 17 00:00:00 2001 From: BigBlack <840206@qq.com> Date: Wed, 18 Dec 2019 18:21:49 +0800 Subject: [PATCH] fix fn type call --- vlib/compiler/parser.v | 16 ++++++++++++---- vlib/compiler/tests/fn_test.v | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index 18efbe384c..5ab0ca15bf 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -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(')') @@ -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 @@ -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('(') diff --git a/vlib/compiler/tests/fn_test.v b/vlib/compiler/tests/fn_test.v index bb730b88ca..f4498b9cea 100644 --- a/vlib/compiler/tests/fn_test.v +++ b/vlib/compiler/tests/fn_test.v @@ -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 +} +