Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler bug, implicit declaration of function #16105

Open
eptx opened this issue Oct 18, 2022 · 4 comments
Open

Compiler bug, implicit declaration of function #16105

eptx opened this issue Oct 18, 2022 · 4 comments
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@eptx
Copy link

eptx commented Oct 18, 2022

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 eptx added the Bug This tag is applied to issues which reports bugs. label Oct 18, 2022
@spytheman
Copy link
Member

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 spytheman added Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Status: Confirmed This bug has been confirmed to be valid by a contributor. labels Oct 19, 2022
@spytheman spytheman self-assigned this Oct 19, 2022
@spytheman
Copy link
Member

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

@felipensp
Copy link
Member

Can you provide a reproducible test case?

@eptx
Copy link
Author

eptx commented Aug 10, 2023

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

  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

No branches or pull requests

3 participants