diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 9244fdd7f3..71c3db9ad6 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3280,6 +3280,7 @@ fn (mut g Gen) expr(node ast.Expr) { g.writeln('($shared_styp*)__dup${shared_styp}(&($shared_styp){.mtx = {0}, .val =') } } + last_stmt_pos := g.stmt_path_pos.last() g.call_expr(node) // if g.fileis('1.strings') { // println('before:' + node.autofree_pregen) @@ -3291,7 +3292,7 @@ fn (mut g Gen) expr(node ast.Expr) { // so just skip it g.autofree_call_pregen(node) if g.strs_to_free0.len > 0 { - g.insert_before_stmt(g.strs_to_free0.join('\n') + '/* inserted before */') + g.insert_at(last_stmt_pos, g.strs_to_free0.join('\n') + '/* inserted before */') } g.strs_to_free0 = [] // println('pos=$node.pos.pos') @@ -5840,6 +5841,12 @@ fn (mut g Gen) insert_before_stmt(s string) { g.write(cur_line) } +fn (mut g Gen) insert_at(pos int, s string) { + cur_line := g.out.cut_to(pos) + g.writeln(s) + g.write(cur_line) +} + // fn (mut g Gen) start_tmp() { // } // If user is accessing the return value eg. in assigment, pass the variable name. @@ -5925,6 +5932,7 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty } } g.writeln('}') + g.stmt_path_pos << g.out.len } [inline] diff --git a/vlib/v/tests/option_struct_init_test.v b/vlib/v/tests/option_struct_init_test.v new file mode 100644 index 0000000000..908a8e3145 --- /dev/null +++ b/vlib/v/tests/option_struct_init_test.v @@ -0,0 +1,22 @@ +struct Foo { + a int + b int + c int +} + +struct Holder { +mut: + i int +} + +fn add(mut h Holder) ?int { + h.i++ + return h.i +} + +fn test_struct_init_with_multiple_optionals() { + mut h := Holder{} + foo := Foo{add(mut h) or { 0 }, add(mut h) or { 0 }, add(mut h) or { 0 }} + + assert foo == Foo{1, 2, 3} +}