cgen: fix codegen for multi return assignment with option type (fix #24812) (#24817)

This commit is contained in:
Felipe Pena 2025-06-30 16:00:05 -03:00 committed by GitHub
parent 2c2ded2e0b
commit 65a5e968d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View file

@ -1083,8 +1083,11 @@ fn (mut g Gen) gen_multi_return_assign(node &ast.AssignStmt, return_type ast.Typ
'${mr_var_name}.arg${i}' '${mr_var_name}.arg${i}'
} }
if mr_types[i].has_flag(.option) { if mr_types[i].has_flag(.option) {
old_left_is_opt := g.left_is_opt
g.left_is_opt = true
g.expr(lx) g.expr(lx)
g.write(' = ${tmp_var};') g.writeln(' = ${tmp_var};')
g.left_is_opt = old_left_is_opt
} else { } else {
g.write('_option_ok(&(${base_typ}[]) { ${tmp_var} }, (${option_name}*)(&') g.write('_option_ok(&(${base_typ}[]) { ${tmp_var} }, (${option_name}*)(&')
tmp_left_is_opt := g.left_is_opt tmp_left_is_opt := g.left_is_opt

View file

@ -0,0 +1,23 @@
module main
type Cmd = fn () string
fn cplx() (bool, ?Cmd) {
return true, stringer
}
fn stringer() string {
return 'a string'
}
fn test_main() {
mut cmd := ?Cmd(none)
mut truth := false
truth, cmd = cplx()
if cmd != none {
println(cmd())
assert true
} else {
assert false
}
}