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

operator overloading not work: invalid operator + to voidptr and voidptr #21654

Closed
kbkpbot opened this issue Jun 7, 2024 · 5 comments · Fixed by #21663
Closed

operator overloading not work: invalid operator + to voidptr and voidptr #21654

kbkpbot opened this issue Jun 7, 2024 · 5 comments · Fixed by #21663
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Type System Bugs/feature requests, that are related to the V types system.

Comments

@kbkpbot
Copy link
Contributor

kbkpbot commented Jun 7, 2024

Describe the bug

When overloading operator, the return result seems has bug

Reproduction Steps

test.v

module main

type AF_ARRAY = voidptr

fn (a AF_ARRAY)add(b AF_ARRAY) AF_ARRAY {
        mut y := AF_ARRAY(0)
        return y
}

fn (a AF_ARRAY)mul(b AF_ARRAY) AF_ARRAY {
        mut y := AF_ARRAY(0)
        return y
}


fn (a AF_ARRAY) + (b AF_ARRAY) AF_ARRAY {
        return a.add(b)
}

fn (a AF_ARRAY) * (b AF_ARRAY) AF_ARRAY {
        return a.mul(b)
}

fn main() {
        a := AF_ARRAY(0)
        b := AF_ARRAY(0)

        c := a + b

        y := a*a + b*b
}

Expected Behavior

compile OK

Current Behavior

test.v:30:7: error: invalid operator `+` to `voidptr` and `voidptr`
   28 |     c := a + b
   29 |
   30 |     y := a*a + b*b
      |          ~~~~~~~~~
   31 | }

Possible Solution

No response

Additional Information/Context

remove line 30, that can compile OK.

change define from voidptr to a struct, also compile OK

for test only, the following code compile OK

module main

type AF_ARRAY = f64


fn (a AF_ARRAY)add(b AF_ARRAY) AF_ARRAY {
        mut y := AF_ARRAY(0)
        return y
}

fn (a AF_ARRAY)mul(b AF_ARRAY) AF_ARRAY {
        mut y := AF_ARRAY(0)
        return y
}


fn (a AF_ARRAY) + (b AF_ARRAY) AF_ARRAY {
        return a.add(b)
}

fn (a AF_ARRAY) * (b AF_ARRAY) AF_ARRAY {
        return a.mul(b)
}

fn main() {
        a := AF_ARRAY(0)
        b := AF_ARRAY(0)

        c := a + b
        d := a * b
        z := c + d

        y := a*a + b*b
}

V version

V full version: V 0.4.6 96751ed.1096173

Environment details (OS name and version, etc.)

V full version: V 0.4.6 96751ed.1096173
OS: linux, Ubuntu 22.04.4 LTS
Processor: 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz

getwd: /home/mars/.vmodules/varray/examples/benchmarks
vexe: /media/HD/github/lang/v/v
vexe mtime: 2024-06-02 12:15:49

vroot: OK, value: /media/HD/github/lang/v
VMODULES: OK, value: /home/mars/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.34.1
Git vroot status: weekly.2024.22-38-g096226bf
.git/config present: true

CC version: cc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
thirdparty/tcc status: thirdparty-linux-amd64 40e5cbb5

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 Jun 7, 2024
@changrui
Copy link
Contributor

changrui commented Jun 7, 2024

I think compiler is right, limited overload.

@kbkpbot
Copy link
Contributor Author

kbkpbot commented Jun 7, 2024

I think compiler is right, limited overload.

No, I think there should has some bug in the compiler.
Change voidptr to f64, this will make compile OK

Possible bug may be in the type system. My guess is:

        c := a + b    // `c` is now voidptr, this is not correct, it should be `AF_ARRAY`
        d := a * b    // `d` is now voidptr, this is not correct, it should be `AF_ARRAY`
        z := c + d

@kbkpbot kbkpbot changed the title operator override not work: invalid operator + to voidptr and voidptr operator overloading not work: invalid operator + to voidptr and voidptr Jun 7, 2024
@spytheman
Copy link
Member

I agree that there is a bug in the compiler.
Aliasing a type, should work fine, with then doing an operator overload on the type alias, even if the original type is voidptr.

@spytheman spytheman added Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Type System Bugs/feature requests, that are related to the V types system. labels Jun 7, 2024
@Delta456
Copy link
Member

Delta456 commented Jun 7, 2024

I will work on this!

@Delta456 Delta456 self-assigned this Jun 7, 2024
@Delta456
Copy link
Member

Delta456 commented Jun 8, 2024

This program works if there's an explicit alias cast.

module main

type AF_ARRAY = voidptr

fn (a AF_ARRAY)add(b AF_ARRAY) AF_ARRAY {
	mut y := AF_ARRAY(0)
	return y
}

fn (a AF_ARRAY)mul(b AF_ARRAY) AF_ARRAY {
	mut y := AF_ARRAY(0)
	return y
}


fn (a AF_ARRAY) + (b AF_ARRAY) AF_ARRAY {
	return a.add(b)
}

fn (a AF_ARRAY) * (b AF_ARRAY) AF_ARRAY {
	return a.mul(b)
}

fn main() {
	a := AF_ARRAY(0)
	b := AF_ARRAY(0)

	c := AF_ARRAY(a) +AF_ARRAY(b)

	y := AF_ARRAY(a*a) +  AF_ARRAY(b*b)
}

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. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Type System Bugs/feature requests, that are related to the V types system.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants