cgen: fix optional generation order (#11070)

pull/11072/head
Enzo 2021-08-06 02:57:34 +02:00 committed by GitHub
parent 7346aeca5f
commit 44bacfc931
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

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

View 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}
}