mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix if cond with result or optional in infix expr (#16625)
This commit is contained in:
parent
68fb4e9fe5
commit
6c0f22416f
@ -688,7 +688,14 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
|
||||
if gen_or && !is_gen_or_and_assign_rhs {
|
||||
cur_line = g.go_before_stmt(0)
|
||||
}
|
||||
g.write('${styp} ${tmp_opt} = ')
|
||||
if gen_or && g.infix_left_var_name.len > 0 {
|
||||
g.writeln('${styp} ${tmp_opt};')
|
||||
g.writeln('if (${g.infix_left_var_name}) {')
|
||||
g.indent++
|
||||
g.write('${tmp_opt} = ')
|
||||
} else {
|
||||
g.write('${styp} ${tmp_opt} = ')
|
||||
}
|
||||
}
|
||||
if node.is_method && !node.is_field {
|
||||
if node.name == 'writeln' && g.pref.experimental && node.args.len > 0
|
||||
@ -705,6 +712,11 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
|
||||
g.or_block(tmp_opt, node.or_block, node.return_type)
|
||||
unwrapped_typ := node.return_type.clear_flag(.optional).clear_flag(.result)
|
||||
unwrapped_styp := g.typ(unwrapped_typ)
|
||||
if g.infix_left_var_name.len > 0 {
|
||||
g.indent--
|
||||
g.writeln('}')
|
||||
g.set_current_pos_as_last_stmt_pos()
|
||||
}
|
||||
if unwrapped_typ == ast.void_type {
|
||||
g.write('\n ${cur_line}')
|
||||
} else {
|
||||
|
@ -868,6 +868,24 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) {
|
||||
g.expr(node.right)
|
||||
g.infix_left_var_name = ''
|
||||
return
|
||||
} else if node.right is ast.CallExpr {
|
||||
if node.right.or_block.kind != .absent {
|
||||
prev_inside_ternary := g.inside_ternary
|
||||
g.inside_ternary = 0
|
||||
tmp := g.new_tmp_var()
|
||||
cur_line := g.go_before_stmt(0).trim_space()
|
||||
g.empty_line = true
|
||||
g.write('bool ${tmp} = (')
|
||||
g.expr(node.left)
|
||||
g.writeln(');')
|
||||
g.set_current_pos_as_last_stmt_pos()
|
||||
g.write('${cur_line} ${tmp} ${node.op.str()} ')
|
||||
g.infix_left_var_name = if node.op == .and { tmp } else { '!${tmp}' }
|
||||
g.expr(node.right)
|
||||
g.infix_left_var_name = ''
|
||||
g.inside_ternary = prev_inside_ternary
|
||||
return
|
||||
}
|
||||
}
|
||||
g.gen_plain_infix_expr(node)
|
||||
}
|
||||
|
3
vlib/v/tests/inout/printing_result_in_infix_expr.out
Normal file
3
vlib/v/tests/inout/printing_result_in_infix_expr.out
Normal file
@ -0,0 +1,3 @@
|
||||
test
|
||||
f2
|
||||
-> 03
|
24
vlib/v/tests/inout/printing_result_in_infix_expr.vv
Normal file
24
vlib/v/tests/inout/printing_result_in_infix_expr.vv
Normal file
@ -0,0 +1,24 @@
|
||||
module main
|
||||
|
||||
fn f2() bool {
|
||||
println('f2')
|
||||
return false
|
||||
}
|
||||
|
||||
fn f3() !bool {
|
||||
println('f3')
|
||||
return false
|
||||
}
|
||||
|
||||
fn test() ! {
|
||||
println('test')
|
||||
if f2() && f3()! {
|
||||
println('-> 02')
|
||||
} else {
|
||||
println('-> 03')
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
test() or { panic(err) }
|
||||
}
|
Loading…
Reference in New Issue
Block a user