From 6c0f22416fe922f7f17ebbe9ad7966f37a7fedee Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 9 Dec 2022 18:01:14 +0800 Subject: [PATCH] cgen: fix if cond with result or optional in infix expr (#16625) --- vlib/v/gen/c/fn.v | 14 ++++++++++- vlib/v/gen/c/infix.v | 18 ++++++++++++++ .../inout/printing_result_in_infix_expr.out | 3 +++ .../inout/printing_result_in_infix_expr.vv | 24 +++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/inout/printing_result_in_infix_expr.out create mode 100644 vlib/v/tests/inout/printing_result_in_infix_expr.vv diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 13756bf3d8..0ebce349d2 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -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 { diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 9793d55c0e..1b4ac9b1d3 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -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) } diff --git a/vlib/v/tests/inout/printing_result_in_infix_expr.out b/vlib/v/tests/inout/printing_result_in_infix_expr.out new file mode 100644 index 0000000000..762d24a3d3 --- /dev/null +++ b/vlib/v/tests/inout/printing_result_in_infix_expr.out @@ -0,0 +1,3 @@ +test +f2 +-> 03 diff --git a/vlib/v/tests/inout/printing_result_in_infix_expr.vv b/vlib/v/tests/inout/printing_result_in_infix_expr.vv new file mode 100644 index 0000000000..2bba853f69 --- /dev/null +++ b/vlib/v/tests/inout/printing_result_in_infix_expr.vv @@ -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) } +}