Skip to content

msvc fixed array init bug #25291

@kbkpbot

Description

@kbkpbot

Describe the bug

vlib/v/tests/modules/sub/global_fixed_array_test.v has bug with msvc .

Reproduction Steps

a modified version of vlib/v/tests/modules/sub/global_fixed_array_test.v

bug.v

@[has_globals]
module main

import sub.foo

struct DummyStruct {
	dummy_item int    // change `i32` to `int` will fire the bug
}

// vfmt off
__global (
	d [foo.num_elements]DummyStruct
)
// vfmt on

const f = [foo.num_elements]DummyStruct{}

fn test_main() {
	assert dump(foo.num_elements) == 2
	assert dump(f) == [foo.num_elements]DummyStruct{}
	assert dump(d) == [foo.num_elements]DummyStruct{}
}
v -cc msvc bug.v

Expected Behavior

compile

Current Behavior

D:\v\v\v>v vlib/v/tests/modules/sub/global_fixed_array_test.v -showcc -keepc -cc msvc
> C compiler cmd: "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\Hostx64\x64\cl.exe" "@C:\Users\DDT\AppData\Local\Temp\v_0\global_fixed_array_test.tmp.c.rsp"
> C compiler response file "C:\Users\DDT\AppData\Local\Temp\v_0\global_fixed_array_test.tmp.c.rsp":
-w /we4013 /volatile:ms /F 16777216 /MD /DNDEBUG /DNO_DEBUGGING "C:\Users\DDT\AppData\Local\Temp\v_0\global_fixed_array_test.tmp.c" -I "C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt" -I "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\include" -I "C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um" -I "C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\shared" -I "D:\v\v\v\vlib\v\tests\modules\sub\foo\c" kernel32.lib user32.lib advapi32.lib ws2_32.lib dbghelp.lib ws2_32.lib advapi32.lib /link /nologo /OUT:"D:\v\v\v\vlib\v\tests\modules\sub\global_fixed_array_test.exe" /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\ucrt\X64" /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\um\X64" /LIBPATH:"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\lib\X64" /DEBUG:FULL
================== C compilation error (from msvc): ==============
global_fixed_array_test.tmp.c
C:\Users\DDT\AppData\Local\Temp\v_0\global_fixed_array_test.tmp.c(2132): error C2099: ʼֵ趨dz��

builder error: msvc error

Possible Solution

In cgen, it just skip the i32 check, which make the original vlib/v/tests/modules/sub/global_fixed_array_test.v passed test.

in fn (mut g Gen) type_default_impl(

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]

need a fix.

Additional Information/Context

No response

V version

V 0.4.11 ce88c56.a8d200a

Environment details (OS name and version, etc.)

V full version V 0.4.11 ce88c56.a8d200a
OS windows, Microsoft Windows 10 企业版 LTSC 19044 64 位
Processor 16 cpus, 64bit, little endian, AMD Ryzen 7 7840H with Radeon 780M Graphics
Memory 17.52GB/27.69GB
V executable D:\v\v\v\v.exe
V last modified time 2025-09-12 11:27:18
V home dir OK, value: D:\v\v\v
VMODULES OK, value: C:\Users\DDT.vmodules
VTMP OK, value: C:\Users\DDT\AppData\Local\Temp\v_0
Current working dir OK, value: D:\v\v\v
Git version git version 2.43.0.windows.1
V git status weekly.2025.30-306-ga8d200ac-dirty
.git/config present true
cc version N/A
gcc version N/A
clang version N/A
msvc version x64 Microsoft (R) C/C++ Ż 19.44.35211
tcc version tcc version 0.9.28rc 2025-04-19_mob@36ff4f52* (x86_64 Windows)
tcc git status N/A
emcc version N/A
glibc version N/A

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.Compilers: MSVCBugs/feature requests, that are related to compiling V programs with MSVC.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions