Skip to content

Commit be85287

Browse files
authored
cgen, checker: fix for with mut generic value (fix #24360) (#24426)
1 parent 2d43b83 commit be85287

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎vlib/v/checker/for.v‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,12 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
200200
}
201201
}
202202

203-
value_type := c.table.value_type(unwrapped_typ)
203+
mut value_type := c.table.value_type(unwrapped_typ)
204+
if node.val_is_mut {
205+
value_type = value_type.ref()
206+
}
204207
node.scope.update_var_type(node.val_var, value_type)
208+
node.val_type = value_type
205209

206210
if is_comptime {
207211
c.type_resolver.update_ct_type(node.val_var, value_type)

‎vlib/v/gen/c/for.v‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) {
154154

155155
node.cond_type = unwrapped_typ
156156
node.val_type = g.table.value_type(unwrapped_typ)
157+
if node.val_is_mut {
158+
node.val_type = node.val_type.ref()
159+
}
157160
node.scope.update_var_type(node.val_var, node.val_type)
158161
node.kind = unwrapped_sym.kind
159162

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
struct ValueMenu {
2+
name string
3+
}
4+
5+
fn (mut v ValueMenu) resp(resp []u8) {
6+
println('menu ${v.name} ${resp}')
7+
}
8+
9+
fn value_resp[T](mut t T, r []u8) {
10+
$if T is $array {
11+
value_array_resp(mut t, r)
12+
}
13+
}
14+
15+
fn value_array_resp[T](mut t T, r []u8) {
16+
$if T is []ValueMenu {
17+
for _, mut i in t { // mut i -> C error
18+
i.resp(r)
19+
}
20+
for i := 0; i < t.len; i++ { // WORKAROUND
21+
t[i].resp(r)
22+
}
23+
}
24+
}
25+
26+
struct Colors {
27+
mut:
28+
normal []ValueMenu = [ValueMenu{'blink'}, ValueMenu{'hue'}]
29+
}
30+
31+
fn (c &Colors) resp(resp []u8) {
32+
$for f in c.fields {
33+
mut m := c.$(f.name)
34+
value_resp(mut m, resp)
35+
}
36+
}
37+
38+
fn test_main() {
39+
colors := &Colors{}
40+
colors.resp([u8(0), 1, 2])
41+
assert true
42+
}

0 commit comments

Comments
 (0)