cgen: fix codegen for global array passed as mut (fix #23873) (#23881)

This commit is contained in:
Felipe Pena 2025-03-08 15:56:38 -03:00 committed by GitHub
parent 44f8ba6161
commit 6b31c86fea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View File

@ -2639,7 +2639,7 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
&& g.table.unaliased_type(arg_typ).is_pointer() && expected_type.is_pointer()) {
if arg.is_mut {
if exp_sym.kind == .array {
if (arg.expr is ast.Ident && arg.expr.kind == .variable)
if (arg.expr is ast.Ident && arg.expr.kind in [.global, .variable])
|| arg.expr is ast.SelectorExpr {
g.write('&')
g.expr(arg.expr)

View File

@ -0,0 +1,24 @@
@[has_globals]
module main
struct Rectangle {
x int
y int
w int
h int
}
__global bricks = []Rectangle{}
fn init_bricks(mut bricks []Rectangle) {
for i in 0 .. 5 {
bricks << Rectangle{i, 2 * i, 3 * i, 4 * i}
}
dump(bricks.len)
}
fn test_main() {
assert bricks.len == 0
init_bricks(mut bricks)
assert bricks.len == 5
}