Skip to content

Commit 6b31c86

Browse files
authored
cgen: fix codegen for global array passed as mut (fix #23873) (#23881)
1 parent 44f8ba6 commit 6b31c86

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

vlib/v/gen/c/fn.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -2639,7 +2639,7 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
26392639
&& g.table.unaliased_type(arg_typ).is_pointer() && expected_type.is_pointer()) {
26402640
if arg.is_mut {
26412641
if exp_sym.kind == .array {
2642-
if (arg.expr is ast.Ident && arg.expr.kind == .variable)
2642+
if (arg.expr is ast.Ident && arg.expr.kind in [.global, .variable])
26432643
|| arg.expr is ast.SelectorExpr {
26442644
g.write('&')
26452645
g.expr(arg.expr)

vlib/v/tests/global_init_array_test.v

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@[has_globals]
2+
module main
3+
4+
struct Rectangle {
5+
x int
6+
y int
7+
w int
8+
h int
9+
}
10+
11+
__global bricks = []Rectangle{}
12+
13+
fn init_bricks(mut bricks []Rectangle) {
14+
for i in 0 .. 5 {
15+
bricks << Rectangle{i, 2 * i, 3 * i, 4 * i}
16+
}
17+
dump(bricks.len)
18+
}
19+
20+
fn test_main() {
21+
assert bricks.len == 0
22+
init_bricks(mut bricks)
23+
assert bricks.len == 5
24+
}

0 commit comments

Comments
 (0)