diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 0e14ef1daacdb2..fddea8e4fe7251 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -790,21 +790,25 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { } // NOTE: everything could be done this way // see my comment in parser near anon_fn + mut tmp_anon_fn_var := '' if node.left is ast.AnonFn { if node.left.inherited_vars.len > 0 { - tmp_var := g.new_tmp_var() + tmp_anon_fn_var = g.new_tmp_var() fn_type := g.fn_var_signature(node.left.decl.return_type, node.left.decl.params.map(it.typ), - tmp_var) + tmp_anon_fn_var) line := g.go_before_last_stmt().trim_space() g.empty_line = true g.write('${fn_type} = ') g.expr(node.left) g.writeln(';') + g.set_current_pos_as_last_stmt_pos() g.write(line) - if g.out.last_n(1) != '\n' { - g.writeln('') + if node.or_block.kind == .absent { + if g.out.last_n(1) != '\n' { + g.writeln('') + } + g.write(tmp_anon_fn_var) } - g.write(tmp_var) } else if node.or_block.kind == .absent { g.expr(node.left) } @@ -887,7 +891,11 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { } g.write('${styp} ${tmp_opt} = ') if node.left is ast.AnonFn { - g.expr(node.left) + if node.left.inherited_vars.len > 0 { + g.write(tmp_anon_fn_var) + } else { + g.expr(node.left) + } } } } diff --git a/vlib/v/tests/closure_option_direct_call_test.v b/vlib/v/tests/closure_option_direct_call_test.v new file mode 100644 index 00000000000000..23a534eed1401f --- /dev/null +++ b/vlib/v/tests/closure_option_direct_call_test.v @@ -0,0 +1,8 @@ +fn test_closure_option_direct_call() { + tmp_wd := 'aaa' + b := fn [tmp_wd] () !string { + return tmp_wd + }() or { panic(err) } + println(b) + assert b == 'aaa' +}