Skip to content

CGen: Compilation error when using generics instantiated with 2 different types in the same file #26753

@Yv-vY

Description

@Yv-vY
V version: V 0.5.1 d261348, press to see full `v doctor` output
V full version V 0.5.1 d261348
OS linux, Linux version 6.19.8-200.fc43.x86_64 (mockbuild@18ffb6c38e5a42008437d0b88e2dfd4e) (gcc (GCC) 15.2.1 20260123 (Red Hat 15.2.1-7), GNU ld version 2.45.1-4.fc43) #1 SMP PREEMPT_DYNAMIC Fri Mar 13 22:06:06 UTC 2026
Processor 16 cpus, 64bit, little endian, AMD Ryzen 7 7800X3D 8-Core Processor
Memory 20.94GB/30.94GB
V executable /home/yann/Apps/v/v
V last modified time 2026-03-21 18:06:53
V home dir OK, value: /home/yann/Apps/v
VMODULES OK, value: /home/yann/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/yann/devel/perso/v/bug_reports/bug_generics_func_invalid_arg_types
Git version git version 2.53.0
V git status weekly.2026.08-256-gd261348c-dirty
.git/config present true
cc version cc (GCC) 15.2.1 20260123 (Red Hat 15.2.1-7)
gcc version gcc (GCC) 15.2.1 20260123 (Red Hat 15.2.1-7)
clang version clang version 21.1.8 (Fedora 21.1.8-4.fc43)
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84-dirty
emcc version N/A
glibc version ldd (GNU libc) 2.42

What did you do?
./v -g -o vdbg cmd/v && ./vdbg my_struct_test.v && my_struct_test

module main

struct MyInt {
	value int
	name  string
}

fn greater_or_equal(a MyInt, b MyInt) bool {
	return a.value >= b.value
}

fn test_ge() {
	s := MyStruct[MyInt]{
		val: MyInt{value: 3, name: '3'}
		others: [
			MyInt{value: 2, name: '2'},
			MyInt{value: 3, name: '3'},
			MyInt{value: 4, name: '4'},
		],
		my_func: greater_or_equal
	}

	s.run()
}

struct Other {
	name string
	size u64
}

fn lower(a &Other, b &Other) bool {
	return a.size < b.size
}

fn test_l() {
	s := MyStruct[&Other]{
		val: &Other{name: 'al', size: 3}
		others: [
			&Other{name: 'joe', size: 42},
			&Other{name: 'jim', size: 2},
		]
		my_func: lower
	}

	s.run()
}

With the generic struct in my_struct.v:

module main

struct MyStruct[T] {
pub:
	val     T
	others  []T
	my_func fn(a T, b T) bool @[required]
}

pub fn (s &MyStruct[T]) run() {
	for c in s.others {
		if s.my_func(s.val, c) {
			println(c.name)
		}
	}
}

What did you see?

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/my_struct_test.01KM8ZZXG1BHZX77392DZNG6D0.tmp.c:8541: error: cannot convert 'void *' to 'struct main__MyInt'
=================================================================
Try passing `-g` when compiling, to see a .v file:line information, that correlates more with the C error.
(Alternatively, pass `-show-c-output`, to print the full C error message).
builder error: 
==================
C error found while compiling generated C code.
This can be caused by invalid C interop code, C compiler flags, or a V compiler bug.
If your code is pure V and this still happens, 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 .

What did you expect to see?

A successful compilation

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions