Skip to content

Commit 6a98e38

Browse files
authored
cgen: fix assign from for mut var in arr { to pointer (fix #24432) (#24456)
1 parent d0de083 commit 6a98e38

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

vlib/v/gen/c/assign.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,11 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
969969
defer {
970970
g.is_option_auto_heap = old_is_auto_heap
971971
}
972+
if val is ast.Ident && val.is_mut() && var_type.is_ptr() {
973+
if var_type.nr_muls() < val_type.nr_muls() {
974+
g.write('*'.repeat(var_type.nr_muls()))
975+
}
976+
}
972977
g.is_option_auto_heap = val_type.has_flag(.option) && val is ast.PrefixExpr
973978
&& val.right is ast.Ident && (val.right as ast.Ident).is_auto_heap()
974979
if var_type.has_flag(.option) || gen_or {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@[heap]
2+
struct Demo {
3+
a string
4+
}
5+
6+
fn test_main() {
7+
mut rl := [&Demo{'A'}]
8+
9+
assert rl[0].a == 'A'
10+
11+
mut p := &Demo{}
12+
for mut e in rl {
13+
p = e
14+
}
15+
16+
assert p.a == 'A'
17+
18+
for i in 0 .. rl.len {
19+
mut e := rl[i]
20+
p = e
21+
}
22+
23+
assert p.a == 'A'
24+
}

0 commit comments

Comments
 (0)