Skip to content

Commit

Permalink
cgen: fix const array of struct initiatization on msvc (#19969)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 23, 2023
1 parent 9ca4c22 commit 043ebb8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions vlib/v/gen/c/array.v
Expand Up @@ -148,6 +148,11 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
}
g.write('{')
if node.has_val {
tmp_inside_array := g.inside_array_item
g.inside_array_item = true
defer {
g.inside_array_item = tmp_inside_array
}
elem_type := (array_type.unaliased_sym.info as ast.ArrayFixed).elem_type
elem_sym := g.table.final_sym(elem_type)
for i, expr in node.exprs {
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/gen/c/cgen.v
Expand Up @@ -142,7 +142,9 @@ mut:
inside_for_c_stmt bool
inside_comptime_for_field bool
inside_cast_in_heap int // inside cast to interface type in heap (resolve recursive calls)
inside_cast bool
inside_const bool
inside_array_item bool
inside_const_opt_or_res bool
inside_lambda bool
inside_cinit bool
Expand Down Expand Up @@ -4564,6 +4566,11 @@ fn (mut g Gen) ident(node ast.Ident) {
}

fn (mut g Gen) cast_expr(node ast.CastExpr) {
tmp_inside_cast := g.inside_cast
g.inside_cast = true
defer {
g.inside_cast = tmp_inside_cast
}
node_typ := g.unwrap_generic(node.typ)
mut expr_type := node.expr_type
sym := g.table.sym(node_typ)
Expand Down
7 changes: 5 additions & 2 deletions vlib/v/gen/c/struct.v
Expand Up @@ -50,7 +50,10 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
}
is_array := sym.kind in [.array_fixed, .array]

if !g.inside_cinit && !is_anon && !is_array {
// detect if we need type casting on msvc initialization
const_msvc_init := g.is_cc_msvc && g.inside_const && !g.inside_cast && g.inside_array_item

if !g.inside_cinit && !is_anon && !is_array && !const_msvc_init {
g.write('(')
defer {
g.write(')')
Expand Down Expand Up @@ -89,7 +92,7 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
if g.table.sym(node.typ).kind == .alias && g.table.unaliased_type(node.typ).is_ptr() {
g.write('&')
}
if is_array {
if is_array || const_msvc_init {
g.write('{')
} else if is_multiline {
g.writeln('(${styp}){')
Expand Down
11 changes: 11 additions & 0 deletions vlib/v/tests/const_array_struct_test.v
@@ -0,0 +1,11 @@
struct Uint128 {
mut:
lo u64
hi u64
}

const zzz_test = [Uint128{1, 2}, Uint128{3, 4}]!

fn test_main() {
assert dump(zzz_test) == zzz_test
}

0 comments on commit 043ebb8

Please sign in to comment.