gen: fix mut sumtype arguments for fields (#13205)

pull/13215/head
Tim Basel 2022-01-18 18:53:09 +01:00 committed by GitHub
parent 95b0c3789f
commit bb6c46e1ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 6 deletions

View File

@ -1647,8 +1647,8 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
}
return
} else if arg_sym.kind == .sum_type && exp_sym.kind == .sum_type
&& arg.expr is ast.Ident {
g.write('&')
&& (arg.expr is ast.Ident || arg.expr is ast.SelectorExpr) {
g.write('&/*sum*/')
g.expr(arg.expr)
return
}

View File

@ -1,11 +1,18 @@
type Sum = Struct1 | int
type Sum1 = Struct1 | int
struct Struct1 {
mut:
value int
}
fn update_sum(mut sum Sum) {
type Sum2 = Struct1 | Struct2 | int
struct Struct2 {
mut:
sum Sum2
}
fn update_sum_1(mut sum Sum1) {
match mut sum {
Struct1 {
sum.value = 42
@ -14,12 +21,24 @@ fn update_sum(mut sum Sum) {
}
}
fn update_sum_2(mut sum Sum2) {
match mut sum {
Struct1 {
sum.value = 42
}
Struct2 {
update_sum_2(mut sum.sum)
}
else {}
}
}
fn test_fn_call_mut_sumtype_args() {
mut s := Sum(Struct1{
mut s := Sum1(Struct1{
value: 6
})
update_sum(mut s)
update_sum_1(mut s)
if mut s is Struct1 {
println(s.value)
@ -28,3 +47,24 @@ fn test_fn_call_mut_sumtype_args() {
assert false
}
}
fn test_fn_call_mut_sumtype_args_field() {
mut s := Sum2(Struct2{
sum: Sum2(Struct1{
value: 6
})
})
update_sum_2(mut s)
if mut s is Struct2 {
if mut s.sum is Struct1 {
println(s.sum.value)
assert s.sum.value == 42
} else {
assert false
}
} else {
assert false
}
}