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:
parent
90197fd58a
commit
7f72ffdd66
@ -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(';')
|
||||
|
29
vlib/v/tests/results_multi_return_test.v
Normal file
29
vlib/v/tests/results_multi_return_test.v
Normal 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'
|
||||
}
|
Loading…
Reference in New Issue
Block a user