cgen: fix struct init with multiple branches, fixes #11564 (#11570)

pull/11936/head
Alexander Ivanov 2021-09-22 00:46:07 +03:00 committed by GitHub
parent 2f3925a0cf
commit b9720f5d9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -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--

View File

@ -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{}
}