diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 45340bef39..1d8ab9d1de 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -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) diff --git a/vlib/v/tests/fn_cross_assign_test.v b/vlib/v/tests/fn_cross_assign_test.v new file mode 100644 index 0000000000..7b1e708510 --- /dev/null +++ b/vlib/v/tests/fn_cross_assign_test.v @@ -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' +}