mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
This commit is contained in:
parent
75928d18e0
commit
b1502d4ff1
7 changed files with 46 additions and 4 deletions
|
@ -1645,6 +1645,7 @@ fn (t Tree) array_init(node ast.ArrayInit) &Node {
|
|||
obj.add('pre_cmnts', t.array_node_comment(node.pre_cmnts))
|
||||
obj.add('elem_type_pos', t.pos(node.elem_type_pos))
|
||||
obj.add_terse('is_fixed', t.bool_node(node.is_fixed))
|
||||
obj.add_terse('is_option', t.bool_node(node.is_option))
|
||||
obj.add_terse('has_val', t.bool_node(node.has_val))
|
||||
obj.add_terse('mod', t.string_node(node.mod))
|
||||
obj.add_terse('len_expr', t.expr(node.len_expr))
|
||||
|
|
|
@ -1546,6 +1546,7 @@ pub:
|
|||
ecmnts [][]Comment // optional iembed comments after each expr
|
||||
pre_cmnts []Comment
|
||||
is_fixed bool
|
||||
is_option bool // true if it was declared as ?[2]Type or ?[]Type
|
||||
has_val bool // fixed size literal `[expr, expr]!`
|
||||
mod string
|
||||
has_len bool
|
||||
|
|
|
@ -285,6 +285,9 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
|
|||
|| c.array_fixed_has_unresolved_size(sym.info as ast.ArrayFixed) {
|
||||
mut size_expr := node.exprs[0]
|
||||
node.typ = c.eval_array_fixed_sizes(mut size_expr, 0, node.elem_type)
|
||||
if node.is_option {
|
||||
node.typ = node.typ.set_flag(.option)
|
||||
}
|
||||
node.elem_type = (c.table.sym(node.typ).info as ast.ArrayFixed).elem_type
|
||||
}
|
||||
if node.has_init {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 && !is_none) {
|
||||
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 {
|
||||
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,6 +217,8 @@ 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 is_none {
|
||||
g.gen_option_error(node.typ, ast.None{})
|
||||
} else {
|
||||
std := g.type_default(node.elem_type)
|
||||
if g.can_use_c99_designators() && std == '0' {
|
||||
|
@ -226,7 +233,10 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
|
|||
}
|
||||
}
|
||||
}
|
||||
if !is_struct {
|
||||
if node.is_option && !is_none {
|
||||
g.write('}')
|
||||
}
|
||||
if !is_struct && !is_none {
|
||||
g.write('}')
|
||||
}
|
||||
}
|
||||
|
|
|
@ -192,6 +192,7 @@ fn (mut p Parser) array_init(is_option bool, alias_array_type ast.Type) ast.Arra
|
|||
has_index: has_index
|
||||
cap_expr: cap_expr
|
||||
init_expr: init_expr
|
||||
is_option: is_option
|
||||
}
|
||||
}
|
||||
|
||||
|
|
23
vlib/v/tests/array_fixed_none_init_test.v
Normal file
23
vlib/v/tests/array_fixed_none_init_test.v
Normal file
|
@ -0,0 +1,23 @@
|
|||
fn test_none_init() {
|
||||
a := ?[3]u8(none)
|
||||
assert a == none
|
||||
|
||||
b := ?[3]u8{}
|
||||
assert b == none
|
||||
|
||||
c := ?[3]u8{}
|
||||
assert c == none
|
||||
}
|
||||
|
||||
fn foo() u8 {
|
||||
return 123
|
||||
}
|
||||
|
||||
fn test_non_none_init() {
|
||||
c := ?[3]u8{init: 2}
|
||||
assert c? == [u8(2), 2, 2]!
|
||||
assert c != none
|
||||
|
||||
d := ?[3]u8{init: foo()}
|
||||
assert d? == [u8(123), 123, 123]!
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue