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

cgen: fix error for result multi_return (#14936)

This commit is contained in:
yuyi 2022-07-04 15:54:08 +08:00 committed by GitHub
parent 90197fd58a
commit 7f72ffdd66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -508,9 +508,9 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
fn (mut g Gen) gen_multi_return_assign(node &ast.AssignStmt, return_type ast.Type) {
// multi return
// TODO Handle in if_expr
is_opt := return_type.has_flag(.optional)
is_opt := return_type.has_flag(.optional) || return_type.has_flag(.result)
mr_var_name := 'mr_$node.pos.pos'
mr_styp := g.typ(return_type.clear_flag(.optional))
mr_styp := g.typ(return_type.clear_flag(.optional).clear_flag(.result))
g.write('$mr_styp $mr_var_name = ')
g.expr(node.right[0])
g.writeln(';')

View File

@ -0,0 +1,29 @@
struct Err {
msg string
code int
}
fn (e Err) msg() string {
return e.msg
}
fn (e Err) code() int {
return e.code
}
fn foo() ?string {
return 'foo'
}
fn bar() !(string, int) {
a := foo() or { return IError(Err{
msg: 'error test'
}) }
return a, 1
}
fn test_results_multi_return() {
b, _ := bar() or { panic(err) }
println(b)
assert b == 'foo'
}