Skip to content

Inconsistent C function naming for generic struct methods #25878

@kbkpbot

Description

@kbkpbot

Describe the bug

I've encountered an inconsistency in the C function naming convention for generic struct methods.

This inconsistency causes issues during compiler development, particularly when implementing features like auto_str() that automatically generate string conversion functions for types. The inconsistent naming makes it difficult to reliably locate and call these generated methods.

This may related to PR #25857

Reproduction Steps

Consider the following V code:
f.v

module main

struct MyS[T] {
mut:
        a T
}

fn (mut m MyS[T]) add(val T) {          // a `generic` method
        m.a += val
}

fn (mut m MyS[int]) sub(val int) {      // a `concrete` method?
        m.a -= val
}

fn main() {
        mut x := MyS[int]{}

        x.add(100)
        x.sub(50)
        dump(x)
}

The generated C code shows different naming patterns for the add and sub methods:

VV_LOC void main__MyS_T_int_add_T_int(main__MyS_T_int* m, int val) {
        m->a += val;
}
VV_LOC void main__MyS_T_int_sub(main__MyS_T_int* m, int val) {
        m->a -= val;
}
VV_LOC void main__main(void) {
        main__MyS_T_int x = ((main__MyS_T_int){.a = 0,});
        main__MyS_T_int_add_T_int(&x, 100);
        main__MyS_T_int_sub(&x, 50);
         _v_dump_expr_main__MyS_T_int(_S("f.v"), 21, _S("x"), x);
}

Expected Behavior

Standardize the naming convention to be consistent for both generic and concrete methods. A possible approach could be to always do not include the parameter type in the function name for consistency. e.g. :

        main__MyS_T_int_add(&x, 100);
        main__MyS_T_int_sub(&x, 50);

Current Behavior

        main__MyS_T_int_add_T_int(&x, 100);
        main__MyS_T_int_sub(&x, 50);

Possible Solution

Fix cgen

Additional Information/Context

No response

V version

V 0.4.12 3caa1b7

Environment details (OS name and version, etc.)

V full version V 0.4.12 3caa1b7
OS linux, Ubuntu 24.04.3 LTS
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Memory 10.88GB/15.51GB
V executable /media/HD/github/kbkpbot/v/v
V last modified time 2025-12-02 12:47:59
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: /home/mars/v/bug/autostr
Git version git version 2.43.0
V git status weekly.2025.46-126-gc54edf65-dirty
.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.6) 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.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