Skip to content

Commit 7c6dba3

Browse files
authored
cgen: fix option array init with non option values (fix #26148) (#26170)
1 parent 29d0f7a commit 7c6dba3

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎vlib/v/gen/c/array.v‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
175175
} else {
176176
node.elem_type
177177
}
178-
g.expr_with_cast(expr, expr_type, node.elem_type)
178+
if node.elem_type.has_flag(.option) {
179+
g.expr_with_opt(expr, expr_type, node.elem_type)
180+
} else {
181+
g.expr_with_cast(expr, expr_type, node.elem_type)
182+
}
179183
}
180184
g.add_commas_and_prevent_long_lines(i, nelen)
181185
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn test_main() {
2+
a := [?string('a'), 'b']!
3+
println(a)
4+
assert '${a}' == "[Option('a'), Option('b')]"
5+
6+
b := [?int(1), 7]!
7+
println(b)
8+
assert '${b}' == '[Option(1), Option(7)]'
9+
}

0 commit comments

Comments
 (0)