Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Jan 11, 2025
1 parent bed28d1 commit b022085
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
12 changes: 9 additions & 3 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) {
g.write('HEAP(${array_styp}, ')
}
len := node.exprs.len
elem_sym := g.table.sym(g.unwrap_generic(node.elem_type))
if array_type.unaliased_sym.kind == .array_fixed {
g.fixed_array_init(node, array_type, var_name, is_amp)
if is_amp {
Expand All @@ -44,12 +45,17 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) {
g.writeln('')
g.write('\t\t')
}
is_sumtype := elem_sym.kind == .sum_type
for i, expr in node.exprs {
if node.expr_types[i] == ast.string_type
&& expr !in [ast.IndexExpr, ast.CallExpr, ast.StringLiteral, ast.StringInterLiteral, ast.InfixExpr] {
g.write('string_clone(')
g.expr(expr)
g.write(')')
if is_sumtype {
g.expr_with_cast(expr, node.expr_types[i], node.elem_type)
} else {
g.write('string_clone(')
g.expr(expr)
g.write(')')
}
} else {
if node.elem_type.has_flag(.option) {
g.expr_with_opt(expr, node.expr_types[i], node.elem_type)
Expand Down
21 changes: 21 additions & 0 deletions vlib/v/tests/builtin_arrays/array_sumtype_init_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type SumFoo = int | string

struct Foo {
bar []SumFoo
}

struct Bar {
foo []Foo
}

fn test_main() {
str := 'foobar'
f := Bar{
foo: [Foo{
bar: [str]
}]
}
assert f.foo.len == 1
assert f.foo[0].bar.len == 1
assert f.foo[0].bar[0] as string == str
}

0 comments on commit b022085

Please sign in to comment.