mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix a bug with autofree in if expr (#11182)
This commit is contained in:
parent
d540b3f1a8
commit
d3cf53ec41
@ -505,6 +505,22 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
|
||||
// It handles auto dereferencing of variables, as well as automatic casting
|
||||
// (see Gen.expr_with_cast for more details)
|
||||
fn (mut g Gen) gen_plain_infix_expr(node ast.InfixExpr) {
|
||||
if node.left is ast.Ident && node.right is ast.IfExpr {
|
||||
// b := a && if true { a = false ...} else {...}
|
||||
if g.need_tmp_var_in_if(node.right) {
|
||||
tmp := g.new_tmp_var()
|
||||
styp := g.typ(node.left_type)
|
||||
cur_line := g.go_before_stmt(0)
|
||||
g.empty_line = true
|
||||
g.write('$styp $tmp = ')
|
||||
g.expr(node.left)
|
||||
g.writeln(';')
|
||||
g.stmt_path_pos << g.out.len
|
||||
g.write('$cur_line $tmp $node.op.str() ')
|
||||
g.expr(node.right)
|
||||
return
|
||||
}
|
||||
}
|
||||
if node.left_type.is_ptr() && node.left.is_auto_deref_var() {
|
||||
g.write('*')
|
||||
}
|
||||
|
11
vlib/v/tests/valgrind/if_expr.v
Normal file
11
vlib/v/tests/valgrind/if_expr.v
Normal file
@ -0,0 +1,11 @@
|
||||
fn main() {
|
||||
mut a := true
|
||||
b := a && if true {
|
||||
a = false
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
println(b)
|
||||
assert b == true
|
||||
}
|
Loading…
Reference in New Issue
Block a user