1
0
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:
yuyi 2021-08-15 12:55:27 +08:00 committed by GitHub
parent d540b3f1a8
commit d3cf53ec41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -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 // It handles auto dereferencing of variables, as well as automatic casting
// (see Gen.expr_with_cast for more details) // (see Gen.expr_with_cast for more details)
fn (mut g Gen) gen_plain_infix_expr(node ast.InfixExpr) { 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() { if node.left_type.is_ptr() && node.left.is_auto_deref_var() {
g.write('*') g.write('*')
} }

View File

@ -0,0 +1,11 @@
fn main() {
mut a := true
b := a && if true {
a = false
true
} else {
false
}
println(b)
assert b == true
}