1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix return match expr of sumtype result (#16264)

This commit is contained in:
yuyi 2022-10-31 02:26:33 +08:00 committed by GitHub
parent 64cbadc6f1
commit 0390a7a988
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -1666,8 +1666,10 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
g.expr(stmt.expr)
g.writeln(';')
} else {
ret_typ := g.fn_decl.return_type.clear_flag(.optional)
styp = g.base_type(ret_typ)
g.write('_option_ok(&($styp[]) { ')
g.expr(stmt.expr)
g.expr_with_cast(stmt.expr, stmt.typ, ret_typ)
g.writeln(' }, ($c.option_name*)(&$tmp_var), sizeof($styp));')
}
}
@ -1696,8 +1698,10 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
g.expr(stmt.expr)
g.writeln(';')
} else {
ret_typ := g.fn_decl.return_type.clear_flag(.result)
styp = g.base_type(ret_typ)
g.write('_result_ok(&($styp[]) { ')
g.expr(stmt.expr)
g.expr_with_cast(stmt.expr, stmt.typ, ret_typ)
g.writeln(' }, ($c.result_name*)(&$tmp_var), sizeof($styp));')
}
}

View File

@ -0,0 +1,25 @@
type Foo = int | string
fn foo1() !Foo {
return match true {
true { 1 }
else { '' }
}
}
fn foo2() ?Foo {
return match true {
true { 1 }
else { '' }
}
}
fn test_return_match_expr_of_sumtype_opt_res() {
ret1 := foo1() or { return }
println(ret1)
assert '$ret1' == 'Foo(1)'
ret2 := foo2() or { return }
println(ret2)
assert '$ret2' == 'Foo(1)'
}