Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker, cgen: fix fixed array of option with default value (related #19405) #19422

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
spytheman marked this conversation as resolved.
Show resolved Hide resolved
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)]'
}
Loading