Skip to content

Commit 4b038d7

Browse files
fix: restrict inside_cast_in_heap to interface types only
The original fix set inside_cast_in_heap for both sum types and interfaces when packing values into arrays/variadic args. However, setting it for sum types caused the compiler to fail self-compilation: struct init codegen in struct.v line 100 uses inside_cast_in_heap > 0 to add memdup wrapping, which breaks for sum type arrays like []ast.Expr used pervasively in the compiler itself. Restrict to .interface only since: - The regression test (#26760) only involves interface values - Interface casts use HEAP() macro (safe) vs & (dangling pointer risk) - Sum type casts use ADDR() macro which already calls memdup Fixes self-compilation failure while preserving the variadic interface chain forwarding fix.
1 parent f4b032a commit 4b038d7

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

vlib/v/gen/c/array.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) {
5959
g.write('\t\t')
6060
}
6161
is_iface_or_sumtype := elem_sym.kind in [.sum_type, .interface]
62-
if is_iface_or_sumtype {
62+
is_iface := elem_sym.kind == .interface
63+
if is_iface {
6364
g.inside_cast_in_heap++
6465
}
6566
for i, expr in node.exprs {
@@ -94,7 +95,7 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) {
9495
}
9596
}
9697
}
97-
if is_iface_or_sumtype {
98+
if is_iface {
9899
g.inside_cast_in_heap--
99100
}
100101
g.write('}))')

vlib/v/gen/c/fn.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,8 +2863,8 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
28632863
g.write('builtin___option_ok((${base_type}[]) {')
28642864
}
28652865
elem_sym := g.table.sym(arr_info.elem_type)
2866-
is_iface_or_sumtype := elem_sym.kind in [.sum_type, .interface]
2867-
if is_iface_or_sumtype {
2866+
is_iface := elem_sym.kind == .interface
2867+
if is_iface {
28682868
g.inside_cast_in_heap++
28692869
}
28702870
g.write('builtin__new_array_from_c_array${noscan}(${variadic_count}, ${variadic_count}, sizeof(${elem_type}), _MOV((${elem_type}[${variadic_count}]){')
@@ -2876,7 +2876,7 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
28762876
}
28772877
}
28782878
g.write('}))')
2879-
if is_iface_or_sumtype {
2879+
if is_iface {
28802880
g.inside_cast_in_heap--
28812881
}
28822882
if is_option {

0 commit comments

Comments
 (0)