diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 3294218c17..293971b69f 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -4798,6 +4798,7 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { } } g.writeln('}') + g.stmt_path_pos << g.out.len if needs_tmp_var { if g.infix_left_var_name.len > 0 { g.indent-- diff --git a/vlib/v/tests/init_multiple_branches_test.v b/vlib/v/tests/init_multiple_branches_test.v new file mode 100644 index 0000000000..0144101d0f --- /dev/null +++ b/vlib/v/tests/init_multiple_branches_test.v @@ -0,0 +1,34 @@ +// fixes https://github.com/vlang/v/issues/11564, test is copied from the code by https://github.com/ken0x0a and formatted +import x.json2 + +struct SA {} + +struct SB {} + +struct SC {} + +type Sum = SA | SB | SC + +struct App { + s map[string]Sum + t bool +} + +fn test_init_multiple_branches() ? { + mut m := map[string]json2.Any{} + app := App{ + t: m['t'] or { 0 }.bool() + s: if a := m['a'] { + println('a => $a') + b := return_optional(a) ? // Fails only if the expr in this line has or_block (or_block kind (.propagation or .block) doesn't matter) + b + } else { + map[string]Sum{} + } + } + println('app => $app') +} + +fn return_optional(input json2.Any) ?map[string]Sum { + return map[string]Sum{} +}