cgen: fix return of mut symtype (#13214)

pull/13217/head
Tim Basel 2022-01-19 13:37:40 +01:00 committed by GitHub
parent 38d3239b50
commit d714dcef75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -5004,6 +5004,8 @@ fn (mut g Gen) return_stmt(node ast.Return) {
if g.fn_decl.return_type.is_ptr() {
var_str := g.expr_string(expr0)
g.write(var_str.trim('&'))
} else if g.table.sym(g.fn_decl.return_type).kind in [.sum_type, .interface_] {
g.expr_with_cast(expr0, node.types[0], g.fn_decl.return_type)
} else {
g.write('*')
g.expr(expr0)

View File

@ -0,0 +1,23 @@
type Sum = Struct | int
struct Struct {
mut:
value int
}
fn update(mut s Struct) Sum {
s.value += 1
return s
}
fn test_fn_return_mut_sumtype() {
mut s := Sum(Struct{
value: 1
})
if mut s is Struct {
s = update(mut s)
assert s.value == 2
} else {
assert false
}
}