Skip to content

Commit

Permalink
checker, cgen: fix fixed array of option with default value (#19422)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Sep 23, 2023
1 parent 1c9ab77 commit 9954f89
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion vlib/v/checker/containers.v
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
node.typ = ast.new_type(idx)
}
if node.has_default {
c.expr(mut node.default_expr)
node.default_type = c.expr(mut node.default_expr)
}
}
return node.typ
Expand Down
18 changes: 15 additions & 3 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
g.indent++
g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden
g.write('*pelem = ')
g.expr(node.default_expr)
if node.elem_type.has_flag(.option) {
g.expr_with_opt(node.default_expr, node.default_type, node.elem_type)
} else {
g.expr_with_cast(node.default_expr, node.default_type, node.elem_type)
}
g.writeln(';')
g.indent--
g.writeln('}')
Expand Down Expand Up @@ -167,7 +171,11 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
} else if node.has_default {
info := array_type.unaliased_sym.info as ast.ArrayFixed
for i in 0 .. info.size {
g.expr(node.default_expr)
if info.elem_type.has_flag(.option) {
g.expr_with_opt(node.default_expr, node.default_type, info.elem_type)
} else {
g.expr_with_cast(node.default_expr, node.default_type, info.elem_type)
}
if i != info.size - 1 {
g.write(', ')
}
Expand Down Expand Up @@ -334,7 +342,11 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
g.indent++
g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden
g.write('*pelem = ')
g.expr(node.default_expr)
if node.elem_type.has_flag(.option) {
g.expr_with_opt(node.default_expr, node.default_type, node.elem_type)
} else {
g.expr_with_cast(node.default_expr, node.default_type, node.elem_type)
}
g.writeln(';')
g.indent--
g.writeln('}')
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/tests/fixed_array_of_option_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ fn test_fixed_array_of_option() {
println(a1)
assert '${a1}' == '[Option(none), Option(2), Option(1)]'

mut a11 := [3]?int{init: 1}
a11[0] = none
a11[1] = 2
println(a11)
assert '${a11}' == '[Option(none), Option(2), Option(1)]'

mut a2 := [3]?int{}
a2[0] = 1
println(a2)
Expand All @@ -13,4 +19,8 @@ fn test_fixed_array_of_option() {
a3 := [3]?int{init: ?int(index * 2)}
println(a3)
assert '${a3}' == '[Option(0), Option(2), Option(4)]'

a33 := [3]?int{init: index * 2}
println(a33)
assert '${a33}' == '[Option(0), Option(2), Option(4)]'
}

0 comments on commit 9954f89

Please sign in to comment.