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

cgen: fix optional in if expr (#15411)

This commit is contained in:
yuyi 2022-08-12 22:22:27 +08:00 committed by GitHub
parent 09f1eb9ad2
commit e6606d8670
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 15 deletions

View File

@ -17,26 +17,39 @@ fn (mut g Gen) need_tmp_var_in_if(node ast.IfExpr) bool {
if branch.stmts.len == 1 { if branch.stmts.len == 1 {
if branch.stmts[0] is ast.ExprStmt { if branch.stmts[0] is ast.ExprStmt {
stmt := branch.stmts[0] as ast.ExprStmt stmt := branch.stmts[0] as ast.ExprStmt
if is_noreturn_callexpr(stmt.expr) { if g.need_tmp_var_in_expr(stmt.expr) {
return true return true
} }
if stmt.expr is ast.MatchExpr { }
}
}
}
return false
}
fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
if is_noreturn_callexpr(expr) {
return true return true
} }
if stmt.expr is ast.CallExpr { if expr is ast.MatchExpr {
if stmt.expr.is_method { return true
left_sym := g.table.sym(stmt.expr.receiver_type) }
if expr is ast.CallExpr {
if expr.is_method {
left_sym := g.table.sym(expr.receiver_type)
if left_sym.kind in [.array, .array_fixed, .map] { if left_sym.kind in [.array, .array_fixed, .map] {
return true return true
} }
} }
if stmt.expr.or_block.kind != .absent { if expr.or_block.kind != .absent {
return true return true
} }
} }
if expr is ast.CastExpr {
return g.need_tmp_var_in_expr(expr.expr)
} }
} if expr is ast.ParExpr {
} return g.need_tmp_var_in_expr(expr.expr)
} }
return false return false
} }

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,11 @@
fn optional() ?int {
return 1
}
fn main() {
println(if true {
i32(optional() or { 2 })
} else {
i32(8)
})
}