From b9c0d2d362f82bbc635bda418d5ff81d5d149cd9 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Tue, 21 Apr 2020 22:45:20 +1000 Subject: [PATCH] cgen: fix passing high order function with body --- vlib/v/gen/cgen.v | 10 ++++++---- vlib/v/tests/fn_test.v | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 59a6c7dacc..7ae4568f7a 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -782,15 +782,15 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { return_type = it.return_type } ast.AnonFn { - g.expr(*it) // TODO: no buffer fiddling - fsym := g.table.get_type_symbol(it.typ) ret_styp := g.typ(it.decl.return_type) g.write('$ret_styp (*$ident.name) (') def_pos := g.definitions.len g.fn_args(it.decl.args, it.decl.is_variadic) g.definitions.go_back(g.definitions.len - def_pos) - g.writeln(') = &${fsym.name};') + g.write(') = ') + g.expr(*it) + g.writeln(';') continue } else {} @@ -1140,9 +1140,11 @@ fn (mut g Gen) expr(node ast.Expr) { def_pos := g.definitions.len g.stmt(it.decl) fn_body := g.out.after(pos) + g.out.go_back(fn_body.len) g.definitions.go_back(g.definitions.len - def_pos) g.definitions.write(fn_body) - g.out.go_back(fn_body.len) + fsym := g.table.get_type_symbol(it.typ) + g.write('&${fsym.name}') } else { // #printf("node=%d\n", node.typ); diff --git a/vlib/v/tests/fn_test.v b/vlib/v/tests/fn_test.v index a530ed298b..ece36ed611 100644 --- a/vlib/v/tests/fn_test.v +++ b/vlib/v/tests/fn_test.v @@ -107,6 +107,10 @@ fn high_fn(f fn(int) int) { } +fn high_fn_no_ret(f fn(int)) { + f(111) +} + fn high_fn_array(f fn(a []int) []int) { } @@ -135,12 +139,16 @@ fn test_anon_fn() { } f2(1) - /* - high_fn(fn (x int) int { - println('hello') - return x + 1 + + // TODO: fix return + // high_fn(fn (x int) int { + // println('hello') + // return x + 1 + // }) + + high_fn_no_ret(fn (x int) { + println('hello $x') }) -*/ } fn assert_in_bool_fn(v int) bool {