Skip to content

Commit 7b20c9c

Browse files
author
Jesus Alvarez
authored
cgen: fix or-block with fixed array constants with GCC 15.2 (fix #26442) (#26443)
1 parent bd2dac6 commit 7b20c9c

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎vlib/v/gen/c/cgen.v‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7404,7 +7404,19 @@ fn (mut g Gen) gen_or_block_stmts(cvar_name string, cast_typ string, stmts []ast
74047404
g.write('${cvar_name} = ')
74057405
}
74067406
if is_array_fixed {
7407-
g.write('memcpy(${cvar_name}${tmp_op}data, (${cast_typ})')
7407+
// For fixed arrays, we need memcpy. Use compound literal cast only for
7408+
// expressions that generate brace-enclosed initializers (which need
7409+
// to become compound literals), not for constants/variables where
7410+
// casting to array type is invalid C.
7411+
is_array_init := expr_stmt.expr is ast.ArrayInit
7412+
|| expr_stmt.expr is ast.StructInit
7413+
|| (expr_stmt.expr is ast.CastExpr
7414+
&& (expr_stmt.expr as ast.CastExpr).expr is ast.ArrayInit)
7415+
if is_array_init {
7416+
g.write('memcpy(${cvar_name}${tmp_op}data, (${cast_typ})')
7417+
} else {
7418+
g.write('memcpy(${cvar_name}${tmp_op}data, ')
7419+
}
74087420
}
74097421
// return expr or { fn_returns_option() }
74107422
if is_option && g.inside_return && expr_stmt.expr is ast.CallExpr
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Test for or-block returning a fixed array constant.
2+
// Regression test for issue where cgen generated invalid C cast to array type.
3+
const default_arr = [f32(1.0), 2.0, 3.0, 4.0]!
4+
5+
fn get_arr(key string) ?[4]f32 {
6+
if key == 'valid' {
7+
return [f32(0.1), 0.2, 0.3, 0.4]!
8+
}
9+
return none
10+
}
11+
12+
fn test_or_block_with_fixed_array_const() {
13+
result := get_arr('invalid') or { default_arr }
14+
assert result == default_arr
15+
}
16+
17+
fn test_or_block_with_valid_key() {
18+
result := get_arr('valid') or { default_arr }
19+
assert result == [f32(0.1), 0.2, 0.3, 0.4]!
20+
}

0 commit comments

Comments
 (0)