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

Generic fntype error #23453

Open
kbkpbot opened this issue Jan 13, 2025 · 4 comments
Open

Generic fntype error #23453

kbkpbot opened this issue Jan 13, 2025 · 4 comments
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: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@kbkpbot
Copy link
Contributor

kbkpbot commented Jan 13, 2025

Describe the bug

When define a generic fntype, it has some bug.

Reproduction Steps

sort.v :

module main

type FnSortCB[T] = fn (const_a T, const_b T) int

fn sort[T](arr []T, sort_cb FnSortCB) []T {
	return arr
}

fn sort2[T](arr []T, sort_cb fn (const_a T, const_b T) int) []T {
	return arr
}

fn my_cmp(a int, b int) int {
	if a == b {
		return 0
	}
	if a < b {
		return 1
	}
	return -1
}

fn main() {
	mut a := [123,553,223,126,883,257]
	
	x := sort(a, my_cmp)	// if change to `sort2`, that can be compiled.
	println(x)
}

Expected Behavior

compile ok

Current Behavior

~/v/sort$ v sort.v
sort.v:26:15: error: cannot use `fn (int, int) int` as `fn (voidptr, voidptr) int` in argument 2 to `sort`
   24 |     mut a := [123,553,223,126,883,257]
   25 |
   26 |     x := sort(a, my_cmp)
      |                  ~~~~~~
   27 |     println(x)
   28 | }
Details: `FnSortCB`'s expected argument `const_a` to be a pointer, but the passed argument `a` is NOT a pointer

Possible Solution

use sort2 instead of sort

Additional Information/Context

No response

V version

V 0.4.9 9fc8352

Environment details (OS name and version, etc.)

V full version V 0.4.9 9fc8352
OS linux, Ubuntu 24.04.1 LTS
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Memory 0.58GB/15.51GB
V executable /media/HD/github/kbkpbot/v/v
V last modified time 2025-01-08 12:09:42
V home dir OK, value: /media/HD/github/kbkpbot/v
VMODULES OK, value: /home/mars/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/mars/v/sort
Git version git version 2.43.0
V git status weekly.2025.1-12-g9fc83526 (29 commit(s) behind V master)
.git/config present true
cc version cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
gcc version gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
clang version Ubuntu clang version 18.1.3 (1ubuntu1)
tcc version tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
tcc git status thirdparty-linux-amd64 0134e9b9
emcc version N/A
glibc version ldd (Ubuntu GLIBC 2.39-0ubuntu8.3) 2.39

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.

@kbkpbot kbkpbot added the Bug This tag is applied to issues which reports bugs. label Jan 13, 2025
Copy link

Connected to Huly®: V_0.6-21883

@spytheman spytheman added Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Checker Bugs/feature requests, that are related to the type checker. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. labels Jan 13, 2025
@kbkpbot
Copy link
Contributor Author

kbkpbot commented Jan 18, 2025

By replaceing FnSortCB with FnSort, will create a different error?

module main

type FnSort[T] = fn (const_a T, const_b T) int

fn sort[T](arr []T, sort_cb FnSort) []T {
        return arr
}

fn sort2[T](arr []T, sort_cb fn (const_a T, const_b T) int) []T {
        return arr
}

fn my_cmp(a int, b int) int {
        if a == b {
                return 0
        }
        if a < b {
                return 1
        }
        return -1
}

fn main() {
        mut a := [123,553,223,126,883,257]

        x := sort(a, my_cmp)    // if change to `sort2`, that can be compiled.
        println(x)
}

error message:

sort2.v:26:15: error: cannot use `fn (int, int) int` as `fn (T, T) int` in argument 2 to `sort`

@kbkpbot
Copy link
Contributor Author

kbkpbot commented Jan 19, 2025

I found the error has something related to vlib/builtin/cfns_wrapper.c.v, in this file,

type FnSortCB = fn (const_a voidptr, const_b voidptr) int

It should conflict with my FnSortCB type definition.

So I think the checker should be improved to check this case, by issue an error message "duplicate definition"

@felipensp
Copy link
Member

felipensp commented Jan 21, 2025

We have a checker error here. Because your code should be:
fn sort[T](arr []T, sort_cb FnSort[T]) []T {

As the alias is a generic one, it must receive the [T] there. As we do in the following case:

type Foo[T] = Struct[T] | int

struct Bar {
	a Foo // error: `Foo` type is generic sumtype, must specify the generic type names, e.g. Foo[T], Foo[int]
}

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: Checker Bugs/feature requests, that are related to the type checker. 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