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:
parent
09f1eb9ad2
commit
e6606d8670
@ -17,23 +17,9 @@ fn (mut g Gen) need_tmp_var_in_if(node ast.IfExpr) bool {
|
||||
if branch.stmts.len == 1 {
|
||||
if branch.stmts[0] is 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
|
||||
}
|
||||
if stmt.expr is ast.MatchExpr {
|
||||
return true
|
||||
}
|
||||
if stmt.expr is ast.CallExpr {
|
||||
if stmt.expr.is_method {
|
||||
left_sym := g.table.sym(stmt.expr.receiver_type)
|
||||
if left_sym.kind in [.array, .array_fixed, .map] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if stmt.expr.or_block.kind != .absent {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -41,6 +27,33 @@ fn (mut g Gen) need_tmp_var_in_if(node ast.IfExpr) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
|
||||
if is_noreturn_callexpr(expr) {
|
||||
return true
|
||||
}
|
||||
if expr is ast.MatchExpr {
|
||||
return true
|
||||
}
|
||||
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] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if expr.or_block.kind != .absent {
|
||||
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
|
||||
}
|
||||
|
||||
fn (mut g Gen) if_expr(node ast.IfExpr) {
|
||||
if node.is_comptime {
|
||||
g.comptime_if(node)
|
||||
|
1
vlib/v/tests/inout/printing_optional_in_if_expr.out
Normal file
1
vlib/v/tests/inout/printing_optional_in_if_expr.out
Normal file
@ -0,0 +1 @@
|
||||
1
|
11
vlib/v/tests/inout/printing_optional_in_if_expr.vv
Normal file
11
vlib/v/tests/inout/printing_optional_in_if_expr.vv
Normal file
@ -0,0 +1,11 @@
|
||||
fn optional() ?int {
|
||||
return 1
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println(if true {
|
||||
i32(optional() or { 2 })
|
||||
} else {
|
||||
i32(8)
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user