1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix anon fn direct call with option (#18827)

This commit is contained in:
yuyi 2023-07-10 05:15:25 +08:00 committed by GitHub
parent 921a2e1c2e
commit 1728e4c73e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -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)

View File

@ -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
}