Skip to content

cgen: fix generic_fn_name generating incorrect names for C structs#25164

Merged
spytheman merged 1 commit into
vlang:masterfrom
dy-tea:master
Aug 25, 2025
Merged

cgen: fix generic_fn_name generating incorrect names for C structs#25164
spytheman merged 1 commit into
vlang:masterfrom
dy-tea:master

Conversation

@dy-tea

@dy-tea dy-tea commented Aug 24, 2025

Copy link
Copy Markdown
Member

Currently using C structs in generics does not work. Given the below files:

// foo.h
#ifndef TEST_H
#define TEST_H

struct foo {
  int a;
};

#endif
// generic_struct_cstruct_test.v
#insert "@VMODROOT/foo.h"

struct C.foo {
	a int
}

fn ref[T](x T) &T {
	return &x
}

fn test_main() {
	a := C.foo{}
	b := ref(a)
}

Currently outputs this C code:

VV_LOC struct foo* main__ref_T_struct foo(struct foo _v_toheap_x) {
struct foo* x = HEAP(struct foo, _v_toheap_x);
	return &(*(x));
}
VV_LOC void main__test_main(void) {
	struct foo a = ((struct foo){.a = 0,});
	struct foo* b = main__ref_T_struct foo(a);
}

Notice the space between main__ref_T_struct and foo on the first line.

This fails to compile:

/tmp/v_1000/generic_struct_cstruct_test.01K3EZ101KHRXGH9XKC03Q2DEW.tmp.c:1872:39: error: expected=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ beforefoo1872 | VV_LOC struct foo* main__ref_T_struct foo(struct foo _v_toheap_x);
      |                                       ^~~
/tmp/v_1000/generic_struct_cstruct_test.01K3EZ101KHRXGH9XKC03Q2DEW.tmp.c:8195:39: error: expected=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ beforefoo8195 | VV_LOC struct foo* main__ref_T_struct foo(struct foo _v_toheap_x) {
      |                                       ^~~
/tmp/v_1000/generic_struct_cstruct_test.01K3EZ101KHRXGH9XKC03Q2DEW.tmp.c: In functionmain__test_main’:
/tmp/v_1000/generic_struct_cstruct_test.01K3EZ101KHRXGH9XKC03Q2DEW.tmp.c:8201:25: error: ‘main__ref_T_structundeclared (first use in this function)
 8201 |         struct foo* b = main__ref_T_struct foo(a);
      |                         ^~~~~~~~~~~~~~~~~~
/tmp/v_1000/generic_struct_cstruct_test.01K3EZ101KHRXGH9XKC03Q2DEW.tmp.c:8201:25: note: each undeclared identifier is reported only once for each function it appears in
/tmp/v_1000/generic_struct_cstruct_test.01K3EZ101KHRXGH9XKC03Q2DEW.tmp.c:8201:44: error: expected ‘,’ or ‘;’ beforefoo8201 |         struct foo* b = main__ref_T_struct foo(a);
      |                                            ^~~

Wasn't sure if I should put the test under tests/generics or tests/c_structs. Let me know if I should change it.

@huly-for-github

Copy link
Copy Markdown

Connected to Huly®: V_0.6-24000

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman spytheman merged commit 70f694f into vlang:master Aug 25, 2025
83 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants