Skip to content

Commit 5c1662d

Browse files
authored
cgen: fix codegen for if comptime and array fixed (fix #25691) (#25697)
1 parent e502877 commit 5c1662d

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

vlib/v/gen/c/comptime.v

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) {
393393
}
394394
tmp_var := g.new_tmp_var()
395395
is_opt_or_result := node.typ.has_option_or_result()
396+
is_array_fixed := g.table.final_sym(node.typ).kind == .array_fixed
396397
line := if node.is_expr {
397398
stmt_str := g.go_before_last_stmt()
398399
g.write(util.tabs(g.indent))
@@ -485,10 +486,19 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) {
485486
g.skip_stmt_pos = true
486487
if is_opt_or_result {
487488
tmp_var2 := g.new_tmp_var()
488-
g.write('{ ${g.base_type(node.typ)} ${tmp_var2} = ')
489+
base_styp := g.base_type(node.typ)
490+
g.write('{ ${base_styp} ${tmp_var2} = ')
489491
g.stmt(last)
490-
g.writeln('builtin___result_ok(&(${g.base_type(node.typ)}[]) { ${tmp_var2} }, (_result*)(&${tmp_var}), sizeof(${g.base_type(node.typ)}));')
492+
g.writeln('builtin___result_ok(&(${base_styp}[]) { ${tmp_var2} }, (_result*)(&${tmp_var}), sizeof(${base_styp}));')
491493
g.writeln('}')
494+
} else if is_array_fixed {
495+
base_styp := g.base_type(node.typ)
496+
g.write('memcpy(&${tmp_var}, (${base_styp})')
497+
g.stmt(last)
498+
if g.out.last_n(2).contains(';') {
499+
g.go_back(2)
500+
}
501+
g.write(', sizeof(${base_styp}))')
492502
} else {
493503
g.write('${tmp_var} = ')
494504
g.stmt(last)

vlib/v/gen/c/consts_and_globals.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ fn (mut g Gen) const_decl_init_later(mod string, name string, cname string, expr
319319
if surround_cbr {
320320
init.writeln('{')
321321
}
322-
if expr is ast.ArrayInit && (expr as ast.ArrayInit).has_index {
322+
if (expr is ast.ArrayInit && expr.has_index)
323+
|| (expr is ast.IfExpr && g.table.type_kind(expr.typ) == .array_fixed) {
323324
init.writeln(g.expr_string_surround('\tmemcpy(&${cname}, &', expr, ', sizeof(${styp}));'))
324325
} else if expr is ast.CallExpr
325326
&& g.table.final_sym(g.unwrap_generic((expr as ast.CallExpr).return_type)).kind == .array_fixed {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// vtest vflags: -d some_other_define
2+
pub const fixed_sized_comptime_array_const = $if some_other_define ? {
3+
[u8(0x01), 0x02]!
4+
} $else $if some_other_define_2 ? {
5+
[u8(0x01), 0x02, 0x03]!
6+
} $else {
7+
[u8(0)]!
8+
}
9+
10+
pub const fixed_sized_comptime_array_const2 = $if some_other_define_2 ? {
11+
[u8(0x01), 0x02, 0x03]!
12+
} $else $if some_other_define ? {
13+
[u8(0x01), 0x02]!
14+
} $else {
15+
[u8(0)]!
16+
}
17+
18+
fn test_main() {
19+
assert fixed_sized_comptime_array_const == [u8(1), 2]!
20+
assert fixed_sized_comptime_array_const2 == [u8(1), 2]!
21+
}

0 commit comments

Comments
 (0)