vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.69k stars 2.15k forks source link

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

Closed kbkpbot closed 3 months ago

kbkpbot commented 3 months ago

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.

changrui commented 3 months ago

I think compiler is right, limited overload.

kbkpbot commented 3 months ago

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
spytheman commented 3 months ago

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.

Delta456 commented 3 months ago

I will work on this!

Delta456 commented 3 months ago

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