Skip to content

Commit

Permalink
fix none initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Dec 15, 2024
1 parent 066f825 commit 7916b05
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/tools/vast/vast.v
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -1546,12 +1546,13 @@ 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
has_cap bool
has_init bool
has_index bool // true if temp variable index is used
has_index bool // true if temp variable index is used
pub mut:
exprs []Expr // `[expr, expr]` or `[expr]Type{}` for fixed array
len_expr Expr // len: expr
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/checker/containers.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
}
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 && !node.is_option {
g.write('{')
}
if node.has_val {
Expand Down Expand Up @@ -212,6 +212,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 node.is_option {
g.gen_option_error(node.typ, ast.None{})
} else {
std := g.type_default(node.elem_type)
if g.can_use_c99_designators() && std == '0' {
Expand All @@ -226,7 +228,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
}
}
}
if !is_struct {
if !is_struct && !node.is_option {
g.write('}')
}
}
Expand Down
1 change: 1 addition & 0 deletions vlib/v/parser/containers.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
13 changes: 13 additions & 0 deletions vlib/v/tests/array_fixed_none_init_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn test_none_init() {
a := ?[3]u8(none)
println(a)
assert a == none

b := [3]u8{}
println(b)
assert b == none

Check failure on line 8 in vlib/v/tests/array_fixed_none_init_test.v

View workflow job for this annotation

GitHub Actions / gcc

infix expr: cannot use `none` (right expression) as `[3]u8`

Check failure on line 8 in vlib/v/tests/array_fixed_none_init_test.v

View workflow job for this annotation

GitHub Actions / msvc

infix expr: cannot use `none` (right expression) as `[3]u8`

Check failure on line 8 in vlib/v/tests/array_fixed_none_init_test.v

View workflow job for this annotation

GitHub Actions / clang (macos-14)

infix expr: cannot use `none` (right expression) as `[3]u8`

c := [3]u8{}
println(c)
assert c == none

Check failure on line 12 in vlib/v/tests/array_fixed_none_init_test.v

View workflow job for this annotation

GitHub Actions / gcc

infix expr: cannot use `none` (right expression) as `[3]u8`

Check failure on line 12 in vlib/v/tests/array_fixed_none_init_test.v

View workflow job for this annotation

GitHub Actions / msvc

infix expr: cannot use `none` (right expression) as `[3]u8`

Check failure on line 12 in vlib/v/tests/array_fixed_none_init_test.v

View workflow job for this annotation

GitHub Actions / clang (macos-14)

infix expr: cannot use `none` (right expression) as `[3]u8`
}

0 comments on commit 7916b05

Please sign in to comment.