cgen: fix if_expr of multi-stmts optional (#9039)

pull/9053/head
yuyi 2021-03-02 04:39:04 +08:00 committed by GitHub
parent dc04c3196b
commit 65900e55e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View File

@ -937,6 +937,9 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) {
}
} else {
g.stmt(stmt)
if g.inside_if_optional && stmt is ast.ExprStmt {
g.writeln(';')
}
}
g.skip_stmt_pos = false
if g.inside_ternary > 0 && i < stmts.len - 1 {

View File

@ -23,3 +23,23 @@ fn test_if_expr_of_optional() {
println(a3)
assert a3 == 2
}
fn foo_complex() ?int {
a := 2
return if a > 1 {
mut b := 1
b *= 10
b
} else {
mut c := 0
c += 2
println(c)
none
}
}
fn test_if_expr_of_optional_complex() {
a := foo_complex() or { panic('error') }
println(a)
assert a == 10
}