From e19b2dd46f4a73218ca3fc904bc7aed88b78db0e Mon Sep 17 00:00:00 2001 From: shove Date: Fri, 5 Jan 2024 06:08:33 +0800 Subject: [PATCH] cgen: fix option/result fixed array init and related tests (#20384) --- vlib/v/gen/c/cgen.v | 20 ++----------------- vlib/v/tests/return_fixed_array_test.v | 10 +++++----- .../struct_init_with_fixed_array_field_test.v | 8 ++++---- 3 files changed, 11 insertions(+), 27 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 711f930a9..2165af085 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5208,15 +5208,7 @@ fn (mut g Gen) return_stmt(node ast.Return) { } for i, expr in node.exprs { if return_sym.kind == .array_fixed && expr !is ast.ArrayInit { - line := g.go_before_last_stmt().trim_space() - g.empty_line = true - tmp_var_2 := g.new_tmp_var() - g.writeln('${ret_typ} ${tmp_var_2} = {0};') - g.write('memcpy(${tmp_var_2}.data, ') - g.expr(expr) - g.writeln(', sizeof(${styp}));') - g.write(line) - g.write(' *(${tmp_var_2}.data)') + g.fixed_array_var_init(expr, (return_sym.info as ast.ArrayFixed).size) } else { g.expr_with_cast(expr, node.types[i], fn_ret_type.clear_flags(.option, .result)) @@ -5252,15 +5244,7 @@ fn (mut g Gen) return_stmt(node ast.Return) { if fn_ret_type.has_flag(.option) { g.expr_with_opt(expr, node.types[i], fn_ret_type.clear_flag(.result)) } else if return_sym.kind == .array_fixed && expr !is ast.ArrayInit { - line := g.go_before_last_stmt().trim_space() - g.empty_line = true - tmp_var_2 := g.new_tmp_var() - g.writeln('${ret_typ} ${tmp_var_2} = {0};') - g.write('memcpy(${tmp_var_2}.data, ') - g.expr(expr) - g.writeln(', sizeof(${styp}));') - g.write(line) - g.write(' *(${tmp_var_2}.data)') + g.fixed_array_var_init(expr, (return_sym.info as ast.ArrayFixed).size) } else { g.expr_with_cast(expr, node.types[i], fn_ret_type.clear_flag(.result)) } diff --git a/vlib/v/tests/return_fixed_array_test.v b/vlib/v/tests/return_fixed_array_test.v index aae2eee84..b6b9d0710 100644 --- a/vlib/v/tests/return_fixed_array_test.v +++ b/vlib/v/tests/return_fixed_array_test.v @@ -28,9 +28,9 @@ fn returns_mut_fixed_array(mut fixed_array [3]int) [3]int { } fn test_returns_mut_fixed_array() { - mut fixed := [3]int{} + mut fixed := [59, 101, 200]! res := returns_mut_fixed_array(mut fixed) - assert res == [0, 0, 0]! + assert res == [59, 101, 200]! } // for issue 20373: returns option / result fixed array @@ -43,17 +43,17 @@ pub fn returns_result_fixed_array(fixed [3]int) ![3]int { } fn test_returns_option_and_result_fixed_array() { - mut fixed := [3]int{} + mut fixed := [int(59), 101, 200]! mut res := returns_option_fixed_array(fixed) or { assert false return } - assert res == [0, 0, 0]! + assert res == [59, 101, 200]! res = returns_result_fixed_array(fixed) or { assert false return } - assert res == [0, 0, 0]! + assert res == [59, 101, 200]! } diff --git a/vlib/v/tests/struct_init_with_fixed_array_field_test.v b/vlib/v/tests/struct_init_with_fixed_array_field_test.v index bfe638eff..2d5ad222a 100644 --- a/vlib/v/tests/struct_init_with_fixed_array_field_test.v +++ b/vlib/v/tests/struct_init_with_fixed_array_field_test.v @@ -38,9 +38,9 @@ pub fn returns_struct_with_mut_fixed_array_init(mut fixed [3]int) Foo { } fn test_returns_struct_with_mut_fixed_array_init() { - mut fixed := [3]int{} + mut fixed := [59, 101, 200]! mut foo := returns_struct_with_mut_fixed_array_init(mut fixed) - assert foo.buf == [0, 0, 0]! + assert foo.buf == [59, 101, 200]! } // for issue 20361(part 2): returns struct with init mut fixed array fields with generics @@ -54,7 +54,7 @@ pub fn returns_struct_with_mut_fixed_array_init_with_generics[T](mut fixed T) Ba } fn test_returns_struct_with_mut_fixed_array_init_with_generics() { - mut fixed := [3]int{} + mut fixed := [59, 101, 200]! mut bar := returns_struct_with_mut_fixed_array_init_with_generics(mut fixed) - assert bar.buf == [0, 0, 0]! + assert bar.buf == [59, 101, 200]! } -- 2.39.5