From 72056f36d886978ef098f19670649bfc1eba33aa Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 29 Aug 2022 13:50:19 +0800 Subject: [PATCH] cgen: fix cross assign of fixed array (#15587) --- vlib/v/gen/c/assign.v | 24 +++++++++++++++++++ vlib/v/tests/cross_assign_fixed_array_test.v | 25 ++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 vlib/v/tests/cross_assign_fixed_array_test.v diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index ff0f771d7..635da4e73 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -658,6 +658,30 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) { g.write(', ') g.expr(left.index) g.writeln(');') + } else if sym.kind == .array_fixed { + info := sym.info as ast.ArrayFixed + elem_typ := g.table.sym(info.elem_type) + if elem_typ.kind == .function { + left_typ := node.left_types[i] + left_sym := g.table.sym(left_typ) + g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_$left.pos.pos') + g.write(' = *(voidptr*)') + } else { + styp := g.typ(info.elem_type) + g.write('$styp _var_$left.pos.pos = ') + } + if left.left_type.is_ptr() { + g.write('*') + } + needs_clone := info.elem_type == ast.string_type && g.is_autofree + if needs_clone { + g.write('/*1*/string_clone(') + } + g.expr(left) + if needs_clone { + g.write(')') + } + g.writeln(';') } else if sym.kind == .map { info := sym.info as ast.Map skeytyp := g.typ(info.key_type) diff --git a/vlib/v/tests/cross_assign_fixed_array_test.v b/vlib/v/tests/cross_assign_fixed_array_test.v new file mode 100644 index 000000000..1e108a29a --- /dev/null +++ b/vlib/v/tests/cross_assign_fixed_array_test.v @@ -0,0 +1,25 @@ +fn test_cross_assign_fixed_array() { + number := 5 + ans := fib(number) + + println(ans) + assert ans == 21 +} + +fn fib(n int) u64 { + if n <= 0 { + panic('Bad number') + } + return match n { + 1 | 2 { + 1 + } + else { + mut pair := [1, 2]! + for _ in 0 .. n { + pair[0], pair[1] = pair[1], pair[0] + pair[1] + } + pair[1] + } + } +} -- 2.30.2