v
v copied to clipboard
Compiler bug, implicit declaration of function
V version: V 0.3.1 f7f0e0b OS: MacOS 12.6
What did you do? Tried to fix the following error...
error: generic struct cannot be used in non-generic function
109 |
110 | fn test_double_sum() {
111 | mut data := [][]T{}
| ~~~~~~
By adding a generic type to the function (a test function)...
fn test_double_sum<T>() {
mut data := [][]T{}
data = [
[0,0,0],
...
[5.0,10.0,15.0]]
What did you expect to see? Test compiled and running. Just started with V so not clear how to define [][]T.
What did you see instead?
tmp.c:23707:3: error: implicit
declaration of function 'main__test_double_sum' is inv
alid in C99 [-Werror,-Wimplicit-function-declaration]
main__test_double_sum();
^
6 warnings and 1 error generated.
...
==================
(Use `v -cg` to print the entire error message)
builder error:
==================
C error. This should never happen.
All test_ functions, should not have arguments, and should not return values; they are invoked by the V's builtin test framework. You can invoke generic functions and methods inside them though.
Something like this for example works:
fn f<T>() [][]T {
return [][]T{}
}
fn test_2d_array() {
x := f<int>()
assert typeof(x).name == '[][]int'
assert x.len == 0
assert x == []
dump(x)
}
Can you provide a reproducible test case?
fn double_sum<T>(a T, b T) T {
return
if a == b { 2 * (a + b)}
else { a + b}
}
fn test_double_sum() {
mut data := [][]T{} // error: generic struct cannot be used in non-generic function
data = [
[0,0,0],
[1,1,4],
[-1,-1,-4],
[10,10,40],
[0,1,1],
[-1,0,-1],
[5, -10, -5],
[5,10,15],
[5.0,10.0,15.0] // error: invalid array element: expected `[]int`, not `[]f64`
]
//fn_double_sum := double_sum
for d in data {
assert double_sum(d[0],d[1]) == d[2]
}
}
fn double_sum[T](a T, b T) T {
return if a == b { 2 * (a + b) } else { a + b }
}
fn t_double_sum[T]() {
mut data := [][]T{} // error: generic struct cannot be used in non-generic function
data = [
[0, 0, 0],
[5.0, 10.0, 15.0], // error: invalid array element: expected `[]int`, not `[]f64`
]
for d in data {
assert double_sum(d[0], d[1]) == d[2]
}
}
fn main() {
t_double_sum[f64]()
}
Already fixed. No more bug about [][]T{}.