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

cgen: fix return if_guard expr (#9375)

This commit is contained in:
yuyi 2021-03-20 10:53:47 +08:00 committed by GitHub
parent 3f9e921c95
commit ead0dff55a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -4357,6 +4357,9 @@ fn (mut g Gen) need_tmp_var_in_if(node ast.IfExpr) bool {
return true
}
for branch in node.branches {
if branch.cond is ast.IfGuardExpr {
return true
}
if branch.stmts.len == 1 {
if branch.stmts[0] is ast.ExprStmt {
stmt := branch.stmts[0] as ast.ExprStmt

View File

@ -59,3 +59,22 @@ fn test_chan_pop() {
assert res == [6.75, -3.25, -37.5]
}
struct Thing {
name string
}
fn test_return_if_guard() {
ret := option_check('zoo')
println(ret)
assert ret == 'zs'
}
fn option_check(name string) string {
return if thing := find_thing_by_name(name) { thing.name } else { 'safename' }
}
fn find_thing_by_name(name string) ?&Thing {
return &Thing{
name: 'zs'
}
}