Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Dec 15, 2024
1 parent 7916b05 commit aee0683
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
3 changes: 3 additions & 0 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 12 additions & 4 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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('}')
}
}
Expand Down
13 changes: 8 additions & 5 deletions vlib/v/tests/array_fixed_none_init_test.v
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit aee0683

Please sign in to comment.