mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix cross assign for functions (#5734)
This commit is contained in:
parent
3bb1d24dad
commit
9c87ace153
@ -1132,8 +1132,26 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
||||
for i, left in assign_stmt.left {
|
||||
match left {
|
||||
ast.Ident {
|
||||
styp := g.typ(assign_stmt.left_types[i])
|
||||
g.writeln('$styp _var_$left.pos.pos = $left.name;')
|
||||
var_type := assign_stmt.left_types[i]
|
||||
left_sym := g.table.get_type_symbol(var_type)
|
||||
if left_sym.kind == .function {
|
||||
func := left_sym.info as table.FnType
|
||||
ret_styp := g.typ(func.func.return_type)
|
||||
g.write('$ret_styp (*_var_$left.pos.pos) (')
|
||||
arg_len := func.func.args.len
|
||||
for j, arg in func.func.args {
|
||||
arg_type := g.table.get_type_symbol(arg.typ)
|
||||
g.write('${arg_type.str()} ${arg.name}')
|
||||
if j < arg_len - 1 {
|
||||
g.write(', ')
|
||||
}
|
||||
}
|
||||
g.writeln(') = $left.name;')
|
||||
}
|
||||
else {
|
||||
styp := g.typ(var_type)
|
||||
g.writeln('$styp _var_$left.pos.pos = $left.name;')
|
||||
}
|
||||
}
|
||||
ast.IndexExpr {
|
||||
sym := g.table.get_type_symbol(left.left_type)
|
||||
|
69
vlib/v/tests/fn_cross_assign_test.v
Normal file
69
vlib/v/tests/fn_cross_assign_test.v
Normal file
@ -0,0 +1,69 @@
|
||||
|
||||
fn cross_assign_anon_fn_one (a int, b bool) string {
|
||||
return 'one'
|
||||
}
|
||||
|
||||
fn cross_assign_anon_fn_two (a int, b bool) string {
|
||||
return 'two'
|
||||
}
|
||||
|
||||
fn cross_assign_anon_fn_three() (string, string) {
|
||||
return 'three', 'three'
|
||||
}
|
||||
|
||||
fn cross_assign_anon_fn_four() (string, string) {
|
||||
return 'four', 'four'
|
||||
}
|
||||
|
||||
fn cross_assign_anon_fn_five(a ...int) string {
|
||||
return 'five'
|
||||
}
|
||||
|
||||
fn cross_assign_anon_fn_six(a ...int) string {
|
||||
return 'six'
|
||||
}
|
||||
|
||||
fn cross_assign_anon_fn_seven (a int, b bool) string {
|
||||
return 'seven'
|
||||
}
|
||||
|
||||
fn cross_assign_anon_fn_eight (a int, b bool) string {
|
||||
return 'eight'
|
||||
}
|
||||
|
||||
fn test_cross_assign_anon_fn() {
|
||||
mut one := cross_assign_anon_fn_one
|
||||
mut two := cross_assign_anon_fn_two
|
||||
one, two = two, one
|
||||
foo := two(0, true) + one(0, true)
|
||||
assert foo == 'onetwo'
|
||||
|
||||
mut three := cross_assign_anon_fn_three
|
||||
mut four := cross_assign_anon_fn_four
|
||||
three, four = four, three
|
||||
mut foo2, mut foo3 := four()
|
||||
foo4, foo5 := three()
|
||||
foo2 += foo4
|
||||
foo3 += foo5
|
||||
assert foo2 == 'threefour'
|
||||
assert foo3 == 'threefour'
|
||||
|
||||
mut five := cross_assign_anon_fn_five
|
||||
mut six := cross_assign_anon_fn_six
|
||||
five, six = six, five
|
||||
foo6 := six(1, 2, 3) + five(1, 2, 3)
|
||||
assert foo6 == 'fivesix'
|
||||
|
||||
one, two, three, four, five, six = two, one, four, three, six, five
|
||||
mut foo7, _ := three()
|
||||
foo8, _ := four()
|
||||
foo7 += foo8
|
||||
foo9 := one(0, true) + two(0, true) + foo7 + five(1, 2, 3) + six(1, 2, 3)
|
||||
assert foo9 == 'onetwothreefourfivesix'
|
||||
|
||||
mut seven := cross_assign_anon_fn_seven
|
||||
mut eight := cross_assign_anon_fn_eight
|
||||
one, two, seven, eight = two, seven, eight, one
|
||||
foo10 := one(0, true) + two(0, true) + seven(0, true) + eight(0, true)
|
||||
assert foo10 == 'twoseveneightone'
|
||||
}
|
Loading…
Reference in New Issue
Block a user