From aee0683eeecc88ae24572f485cc872a93945310e Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 15 Dec 2024 18:30:35 -0300 Subject: [PATCH] fix --- vlib/v/fmt/fmt.v | 3 +++ vlib/v/gen/c/array.v | 16 ++++++++++++---- vlib/v/tests/array_fixed_none_init_test.v | 13 ++++++++----- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 32925545ef3f1a..b3bfc4145c0095 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1818,6 +1818,9 @@ pub fn (mut f Fmt) array_decompose(node ast.ArrayDecompose) { } pub fn (mut f Fmt) array_init(node ast.ArrayInit) { + if node.is_fixed && node.is_option { + f.write('?') + } if node.exprs.len == 0 && node.typ != 0 && node.typ != ast.void_type { // `x := []string{}` f.mark_types_import_as_used(node.typ) diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 16a6b83941a978..ef36a043756055 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -125,15 +125,20 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st g.set_current_pos_as_last_stmt_pos() return } - if g.inside_struct_init && g.inside_cast && !g.inside_memset { + is_none := node.is_option && !node.has_init && !node.has_val + + if (g.inside_struct_init && g.inside_cast && !g.inside_memset) || node.is_option { ret_typ_str := g.styp(node.typ) g.write('(${ret_typ_str})') } elem_sym := g.table.final_sym(node.elem_type) is_struct := g.inside_array_fixed_struct && elem_sym.kind == .struct - if !is_struct && !node.is_option { + if !is_struct && !is_none { g.write('{') } + if node.is_option && !is_none { + g.write('.state=0, .err=_const_none__, .data={') + } if node.has_val { tmp_inside_array := g.inside_array_item g.inside_array_item = true @@ -212,7 +217,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st }) schan_expr := g.out.cut_to(before_chan_expr_pos) g.write_c99_elements_for_array(array_info.size, schan_expr) - } else if node.is_option { + } else if is_none { g.gen_option_error(node.typ, ast.None{}) } else { std := g.type_default(node.elem_type) @@ -228,7 +233,10 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st } } } - if !is_struct && !node.is_option { + if node.is_option && !is_none { + g.write('}') + } + if !is_struct && !is_none { g.write('}') } } diff --git a/vlib/v/tests/array_fixed_none_init_test.v b/vlib/v/tests/array_fixed_none_init_test.v index a30a77f876503b..12811d50a3aa38 100644 --- a/vlib/v/tests/array_fixed_none_init_test.v +++ b/vlib/v/tests/array_fixed_none_init_test.v @@ -1,13 +1,16 @@ fn test_none_init() { a := ?[3]u8(none) - println(a) assert a == none - b := [3]u8{} - println(b) + b := ?[3]u8{} assert b == none - c := [3]u8{} - println(c) + c := ?[3]u8{} assert c == none } + +fn test_non_none_init() { + c := ?[3]u8{init: 2} + assert c? == [u8(2), 2, 2]! + assert c != none +}