v
v copied to clipboard
Generic function fails to be created on second use of generic function
Describe the bug
There is a bug with the generics where it will not produce the generic function for the second use of the generic function. In this case, func2[Struct1] is generated, but func2[Struct2] is not, causing a compilation error
Reproduction Steps
The following code will produce the issue
module main
struct ClassInfo {
func fn () = unsafe { nil }
}
pub fn func2[T]() {
}
pub fn func1[T]() {
ci := ClassInfo{
func: func2[T]
}
ci.func()
}
struct Struct1 {}
struct Struct2 {}
pub fn main() {
func1[Struct1]()
func1[Struct2]()
}
Expected Behavior
successful compile
Current Behavior
Fails to compile with the following error
PS C:\Users\jcwea\Documents\git\vtest> v -cg run .
C:/Users/jcwea/AppData/Local/Temp/v_0/vtest.01HPSP98VD487P3GG4CG3VMKQV.tmp.c:497: warning: WINVER redefined
C:/Users/jcwea/AppData/Local/Temp/v_0/vtest.01HPSP98VD487P3GG4CG3VMKQV.tmp.c:6880: warning: implicit declaration of function 'tcc_backtrace'
C:/Users/jcwea/AppData/Local/Temp/v_0/vtest.01HPSP98VD487P3GG4CG3VMKQV.tmp.c:13104: error: 'main__func2_T_main__Struct2' undeclared
builder error:
==================
C error. This should never happen.
This is a compiler bug, please report it using `v bug file.v`.
https://github.com/vlang/v/issues/new/choose
You can also use #help on Discord: https://discord.gg/vlang
Possible Solution
No response
Additional Information/Context
No response
V version
V 0.4.4 1d3147e
Environment details (OS name and version, etc.)
V full version: V 0.4.4 afd74ad.1d3147e
OS: windows, Microsoft Windows 11 Pro v26058 64-bit
Processor: 24 cpus, 64bit, little endian,
getwd: C:\Users\jcwea\Documents\git\vtest
vexe: C:\Users\jcwea\Documents\git\v\v.exe
vexe mtime: 2024-02-16 19:26:43
vroot: OK, value: C:\Users\jcwea\Documents\git\v
VMODULES: OK, value: C:\Users\jcwea\.vmodules
VTMP: OK, value: C:\Users\jcwea\AppData\Local\Temp\v_0
Git version: git version 2.40.0.windows.1
Git vroot status: weekly.2024.06-86-g1d3147e1
.git/config present: true
CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.
thirdparty/tcc status: thirdparty-windows-amd64 b99a453d
[!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.
This one has a workaround, just create a local variable with the function
module main
struct ClassInfo {
func fn () = unsafe { nil }
}
pub fn func2[T]() {
}
pub fn func1[T]() {
f := func2[T]
ci := ClassInfo{
func: f
}
ci.func()
}
struct Struct1 {}
struct Struct2 {}
pub fn main() {
func1[Struct1]()
func1[Struct2]()
}