mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix optional generation order (#11070)
This commit is contained in:
parent
7346aeca5f
commit
44bacfc931
@ -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]
|
||||
|
22
vlib/v/tests/option_struct_init_test.v
Normal file
22
vlib/v/tests/option_struct_init_test.v
Normal file
@ -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}
|
||||
}
|
Loading…
Reference in New Issue
Block a user