Skip to content

Commit 02ca30a

Browse files
authored
cgen: fix codegen for returning an initialised fixed array (fix #23693) (#23700)
1 parent e3d8cdc commit 02ca30a

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

vlib/v/gen/c/array.v

+9-3
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,15 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
350350
g.set_current_pos_as_last_stmt_pos()
351351
g.indent++
352352
g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden
353-
g.write('*pelem = ')
354-
g.expr_with_init(node)
355-
g.writeln(';')
353+
if elem_type.unaliased_sym.kind != .array_fixed {
354+
g.write('*pelem = ')
355+
g.expr_with_init(node)
356+
g.writeln(';')
357+
} else {
358+
g.write('memcpy(pelem, ')
359+
g.expr_with_init(node)
360+
g.writeln(', sizeof(${elem_styp}));')
361+
}
356362
g.indent--
357363
g.writeln('}')
358364
g.indent--

vlib/v/parser/parse_type.v

+5-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn (mut p Parser) parse_array_type(expecting token.Kind, is_option bool) ast.Typ
9595
p.error_with_pos('fixed size cannot be zero or negative', size_expr.pos())
9696
}
9797
idx := p.table.find_or_register_array_fixed(elem_type, fixed_size, size_expr,
98-
p.fixed_array_dim == 1 && !is_option && p.inside_fn_return)
98+
p.array_dim == 1 && p.fixed_array_dim == 1 && !is_option && p.inside_fn_return)
9999
if elem_type.has_flag(.generic) {
100100
return ast.new_type(idx).set_flag(.generic)
101101
}
@@ -646,6 +646,10 @@ fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_dot b
646646
}
647647
.lsbr, .nilsbr {
648648
// array
649+
p.array_dim++
650+
defer {
651+
p.array_dim--
652+
}
649653
return p.parse_array_type(p.tok.kind, is_option)
650654
}
651655
else {

vlib/v/parser/parser.v

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ mut:
7272
inside_orm bool
7373
inside_chan_decl bool
7474
inside_attr_decl bool
75+
array_dim int // array dim parsing level
7576
fixed_array_dim int // fixed array dim parsing level
7677
or_is_handled bool // ignore `or` in this expression
7778
builtin_mod bool // are we in the `builtin` module?
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn separate_wires(coo_adj_wires [][2]u32, id u64) [][2]u32 {
2+
return [][2]u32{len: coo_adj_wires.len, init: coo_adj_wires[index]}
3+
}
4+
5+
fn test_main() {
6+
t := separate_wires([[u32(1), 2]!], 0)
7+
assert t.str() == '[[1, 2]]'
8+
}

0 commit comments

Comments
 (0)