v icon indicating copy to clipboard operation
v copied to clipboard

Compiler bug, implicit declaration of function

Open eptx opened this issue 3 years ago • 5 comments

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.

eptx avatar Oct 18 '22 22:10 eptx

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.

spytheman avatar Oct 19 '22 13:10 spytheman

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)
}

spytheman avatar Oct 19 '22 14:10 spytheman

Can you provide a reproducible test case?

felipensp avatar Dec 19 '22 22:12 felipensp

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]
    }

  }

eptx avatar Aug 10 '23 01:08 eptx

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]()
}

felipensp avatar May 21 '24 22:05 felipensp

Already fixed. No more bug about [][]T{}.

felipensp avatar Oct 16 '24 12:10 felipensp