Skip to content

generic array of map cause C compile fail #24090

Description

@kbkpbot

Describe the bug

When compile code for generic array of map[string]string, it will cause C compile fail

Reproduction Steps

g.v

module main
fn encode_map[K,V](m map[K]V) {
	println('${typeof(m).name}')
}

fn encode_array[T](arr []T) {
	for element in arr {
		$if T is $map {
			encode_map(element)
		}
	}
}

fn main() {
	x := [{'123':'456'},{'abc':'def'},]
	encode_array(x)
}

just compile with v g.v

Expected Behavior

compile ok

Current Behavior

$ v g.v
================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/g.01JQKJSA0PTK3MQ144VG9AM1XC.tmp.c:1540: error: ',' expected (got "m")
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
builder error:
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .

Possible Solution

By run v g.v -showcc -keepc and check the generated C code, it is found that v compiler generate extra C code for the program:

VV_LOCAL_SYMBOL void main__encode_map_T_string_string(Map_string_string m);
VV_LOCAL_SYMBOL void main__encode_map_T_Map_string_string_string(Map_Map_string_string_string m);
VV_LOCAL_SYMBOL void main__encode_array_T_Map_string_string(Array_Map_string_string arr);
VV_LOCAL_SYMBOL void main__main(void);

Note that the compiler should not generate the main__encode_map_T_Map_string_string_string() function.
Or miss a declare for typedef map Map_Map_string_string_string;

Maybe a fix for the cgen can fix this.

Additional Information/Context

related C code:

VV_LOCAL_SYMBOL void main__encode_map_T_string_string(Map_string_string m) {
        println(str_intp(2, _MOV((StrIntpData[]){{_SLIT0, 0xfe10, {.d_s = _SLIT("map[string]string")}}, {_SLIT0, 0, { .d_c = 0 }}})));
}
VV_LOCAL_SYMBOL void main__encode_map_T_Map_string_string_string(Map_Map_string_string_string m) {
        println(str_intp(2, _MOV((StrIntpData[]){{_SLIT0, 0xfe10, {.d_s = _SLIT("map[map[string]string]string")}}, {_SLIT0, 0, { .d_c = 0 }}})));
}
VV_LOCAL_SYMBOL void main__encode_array_T_Map_string_string(Array_Map_string_string arr) {
        for (int _t1 = 0; _t1 < arr.len; ++_t1) {
                Map_string_string element = ((Map_string_string*)arr.data)[_t1];
                #if 1
                {
                        main__encode_map_T_string_string(element);
                }
                #endif
        }
}

V version

V 0.4.10 690f845.a3180e5

Environment details (OS name and version, etc.)

V full version V 0.4.10 690f845.a3180e5
OS linux, Ubuntu 24.04.2 LTS
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Memory 8.02GB/15.51GB
V executable /media/HD/github/kbkpbot/v/v
V last modified time 2025-03-30 13:06:53
V home dir OK, value: /media/HD/github/kbkpbot/v
VMODULES OK, value: /home/mars/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /media/HD/github/kbkpbot/v
Git version git version 2.43.0
V git status weekly.2025.13-31-ga3180e57 (1 commit(s) behind V master)
.git/config present true
cc version cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
gcc version gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
clang version Ubuntu clang version 18.1.3 (1ubuntu1)
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (Ubuntu GLIBC 2.39-0ubuntu8.4) 2.39

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

Labels

BugThis tag is applied to issues which reports bugs.Generics[T]Bugs/feature requests, that are related to the V generics.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: CompilerBugs/feature requests, that are related to the V compiler in general.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions