Skip to content

Commit

Permalink
cgen: fix anon fn direct call with option (#18827)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jul 9, 2023
1 parent 921a2e1 commit 1728e4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,12 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
tmp_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)
line := g.go_before_stmt(0).trim_space()
g.empty_line = true
g.write('${fn_type} = ')
g.expr(node.left)
g.writeln(';')
g.write(line)
g.write(tmp_var)
} else if node.or_block.kind == .absent {
g.expr(node.left)
Expand Down
17 changes: 17 additions & 0 deletions vlib/v/tests/anon_fn_direct_call_with_option_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fn test_anon_fn_direct_call_with_option() {
z := true
a := fn [z] () ?int {
match z {
true { return 1 }
else { return none }
}
}()
b := a or {
println('failed')
return
}

println('b: ${b}')
println(a)
assert b == 1
}

0 comments on commit 1728e4c

Please sign in to comment.