mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
gen: fix mut sumtype arguments for fields (#13205)
This commit is contained in:
parent
95b0c3789f
commit
bb6c46e1ef
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user