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

Arithmetic operator overloading result type should be the same as the operands #23194

Open
jorgeluismireles opened this issue Dec 17, 2024 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs. Operator Overload Status: Confirmed This bug has been confirmed to be valid by a contributor. Type Aliases 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. Unit: Type System Bugs/feature requests, that are related to the V types system.

Comments

@jorgeluismireles
Copy link

jorgeluismireles commented Dec 17, 2024

Describe the bug

Arithmetic operations should remain associative and distributive. So A + B + C = D should be to valid in any case, to mention a simple example.

Current arithmetic operator overloading permits the result to be different from the operands. Problems:

  • The user don't know what type the result would be.
  • Associative rule and others will fails.
  • Extra bugs (see the last code).

Inconsistencies were mentioned already here: #23177

Reproduction Steps

type Byte = u8
type String = string

pub fn (b1 Byte) + (b2 Byte) String {
	return '${u16(b1) + u16(b2)}'
}

fn main() {
	println('${Byte(255) + Byte(1) + Byte(2)}')
}

Expected Behavior

A Byte whatever the value, truncated or even as an error when operation does not apply.

Current Behavior

Running code...
Can't run code. The server returned an error:
code.v:9:13: error: infix expr: cannot use `Byte` (right expression) as `String`
    7 | 
    8 | fn main() {
    9 |     println('${Byte(255) + Byte(1) + Byte(2)}')
      |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   10 | }
Exited with error status 1
Please try again.

Possible Solution

Force result type to be the same of the operands.

Additional Information/Context

More complications:

type Byte = u8

pub fn (b1 Byte) + (b2 Byte) string {
	return '${u16(b1) + u16(b2)}'
}

fn main() {
	println('${Byte(255) + Byte(1)}')
}

Produces C error:

Running code...
Can't run code. The server returned an error:
/tmp/v_60000/../../../../../../box/code.v:8: error: cannot convert 'struct string' to 'unsigned char'
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .
Exited with error status 1
Please try again.

V version

V 0.4.8 00148d1

Environment details (OS name and version, etc.)

https://play.vlang.io/p/0b6cc9e21c

https://play.vlang.io/p/b09d63132f

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.

Huly®: V_0.6-21631

@jorgeluismireles jorgeluismireles added the Bug This tag is applied to issues which reports bugs. label Dec 17, 2024
@spytheman
Copy link
Member

Afaik that is a bug that only affects type aliases. Overriding operators on the non aliased types is checked and the types are required to be the same for the operands and for the result.

@spytheman spytheman added Unit: Checker Bugs/feature requests, that are related to the type checker. Type Aliases Operator Overload Status: Confirmed This bug has been confirmed to be valid by a contributor. 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 Dec 18, 2024
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. Operator Overload Status: Confirmed This bug has been confirmed to be valid by a contributor. Type Aliases 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. Unit: Type System Bugs/feature requests, that are related to the V types system.
Projects
None yet
Development

No branches or pull requests

2 participants