Skip to content

Commit ffeff23

Browse files
authored
cgen: fix fixed-array const initializer (fix #25291) (#25293)
1 parent a8d200a commit ffeff23

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7591,7 +7591,7 @@ fn (mut g Gen) type_default_impl(typ_ ast.Type, decode_sumtype bool) string {
75917591
field_sym := g.table.sym(field.typ)
75927592
is_option := field.typ.has_flag(.option)
75937593
if is_option || field.has_default_expr
7594-
|| field_sym.kind in [.enum, .array_fixed, .array, .map, .string, .bool, .alias, .i8, .i16, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, .char, .voidptr, .byteptr, .charptr, .struct, .chan, .sum_type] {
7594+
|| field_sym.kind in [.enum, .array_fixed, .array, .map, .string, .bool, .alias, .i8, .i16, .i32, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, .char, .voidptr, .byteptr, .charptr, .struct, .chan, .sum_type] {
75957595
if sym.language == .c && !field.has_default_expr && !is_option {
75967596
continue
75977597
}
@@ -7668,7 +7668,7 @@ fn (mut g Gen) type_default_impl(typ_ ast.Type, decode_sumtype bool) string {
76687668
if has_none_zero {
76697669
init_str += '}'
76707670
if !typ_is_shared_f {
7671-
type_name := if info.is_anon || g.inside_global_decl {
7671+
type_name := if info.is_anon || g.inside_global_decl || g.inside_const {
76727672
// No name needed for anon structs, C figures it out on its own.
76737673
''
76747674
} else {

vlib/v/tests/modules/sub/global_fixed_array_test.v

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ fn test_main() {
2020
assert dump(f) == [foo.num_elements]DummyStruct{}
2121
assert dump(d) == [foo.num_elements]DummyStruct{}
2222
}
23+
24+
struct DummyStruct2 {
25+
dummy_item1 int
26+
dummy_item2 i32
27+
dummy_item3 u8
28+
}
29+
30+
const m = [foo.num_elements]DummyStruct2{}
31+
32+
fn test_many_dummy_fields_with_diff_types() {
33+
assert dump(m) == [foo.num_elements]DummyStruct2{}
34+
}

0 commit comments

Comments
 (0)