From dcbd8d64051198cde8c15c7bfb2545fd5b9d5d9f Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 3 Jun 2022 20:57:39 +0800 Subject: [PATCH] cgen: fix if expr with optional method call (#14600) --- vlib/v/gen/c/if.v | 3 ++- .../tests/if_expr_with_method_call_optional_test.v | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/if_expr_with_method_call_optional_test.v diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index 3e05821d88..92da23e3ee 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -29,7 +29,8 @@ fn (mut g Gen) need_tmp_var_in_if(node ast.IfExpr) bool { if left_sym.kind in [.array, .array_fixed, .map] { return true } - } else if stmt.expr.or_block.kind != .absent { + } + if stmt.expr.or_block.kind != .absent { return true } } diff --git a/vlib/v/tests/if_expr_with_method_call_optional_test.v b/vlib/v/tests/if_expr_with_method_call_optional_test.v new file mode 100644 index 0000000000..e8e9e2f985 --- /dev/null +++ b/vlib/v/tests/if_expr_with_method_call_optional_test.v @@ -0,0 +1,13 @@ +fn to_bit_string(i rune) []u8 { + return if i == 0 { []u8{} } else { '${i:b}'.split('').map(it.u8()) } +} + +fn from_bit_string(bits []u8) rune { + return if bits == [] { 0 } else { bits.map(it.str()).join('').parse_uint(2, 8) or { 0 } } +} + +fn test_if_expr_with_method_call_optional() { + a := from_bit_string(to_bit_string(u32(100))) + println(a) + assert a == `d` +}