Skip to content

Commit f183ec3

Browse files
authored
cgen: fix comptime assign with generic result return type (#19192)
1 parent ccb1438 commit f183ec3

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

vlib/v/gen/c/fn.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
804804
unwrapped_typ = unaliased_type.clear_flags(.option, .result)
805805
}
806806
}
807-
unwrapped_styp := g.typ(unwrapped_typ)
807+
mut unwrapped_styp := g.typ(unwrapped_typ)
808808
if g.infix_left_var_name.len > 0 {
809809
g.indent--
810810
g.writeln('}')
@@ -814,6 +814,11 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
814814
g.write('\n ${cur_line}')
815815
} else if !g.inside_curry_call {
816816
if !g.inside_const_opt_or_res {
817+
if g.assign_ct_type != 0
818+
&& node.or_block.kind in [.propagate_option, .propagate_result] {
819+
unwrapped_styp = g.typ(g.assign_ct_type.derive(node.return_type).clear_flags(.option,
820+
.result))
821+
}
817822
g.write('\n ${cur_line} (*(${unwrapped_styp}*)${tmp_opt}.data)')
818823
} else {
819824
g.write('\n ${cur_line} ${tmp_opt}')
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
struct Parent {
2+
pub mut:
3+
id int
4+
name string
5+
child Child
6+
other Other
7+
}
8+
9+
struct Child {
10+
pub mut:
11+
id int
12+
age int
13+
}
14+
15+
struct Other {
16+
pub mut:
17+
id int
18+
name string
19+
}
20+
21+
interface IdInterface {
22+
mut:
23+
id int
24+
}
25+
26+
fn insert_ids[T](val T) !T {
27+
mut clone := val
28+
29+
$for field in T.fields {
30+
$if field.typ is $struct {
31+
clone.$(field.name) = insert_ids(val.$(field.name))!
32+
}
33+
}
34+
$if T is IdInterface {
35+
clone.id = 1
36+
} $else {
37+
return error('${T.name} does not have an id field!')
38+
}
39+
return clone
40+
}
41+
42+
fn test_main() {
43+
inserted := insert_ids(Parent{
44+
name: 'test'
45+
})!
46+
assert inserted.child.id == 1
47+
assert inserted.other.id == 1
48+
}

0 commit comments

Comments
 (0)