modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.25k stars 2.6k forks source link

[BUG] `a<b` is ok but not `b<a` #2329

Open YichengDWu opened 6 months ago

YichengDWu commented 6 months ago

Bug description

I'm not sure how arithmetic operations between Int and UInt32 types are supposed to behave, but it seems strange to me that the following code results in an error.

Steps to reproduce

fn main():
    var a: Int = 1
    var b: UInt32 = 2
    # var c = b < a # This is ok
    var c = a < b
    print(c)

System information

ethan@ethan-MS-7E07:~/moye$ mojo -v
mojo 2024.4.1618 (37a3e965)
ethan@ethan-MS-7E07:~/moye$ modular -v
modular 0.7.1 (28ddab26)
ematejska commented 6 months ago

Error is: error: invalid call to 'lt': right side cannot be converted from 'SIMD[ui32, 1]' to 'Int' var c = a < b

ematejska commented 6 months ago

@JoeLoser Library or not?

JoeLoser commented 6 months ago

@JoeLoser Library or not?

Probably library only, yeah.

zhoujingya commented 6 months ago

I think #2346 also referred to SIMD compare operator @VMois

soraros commented 6 months ago

@zhoujingya I don't think they are related at all.

Mojo (just like Python) has left biased operators. b < a doesn't work because less than is not a free function but a method, and Int doesn't have a SIMD overload. I'm not sure we want to add the overload to Int though.

YichengDWu commented 4 months ago

Update with UInt

fn main():
    var a: Int = 1
    var b: UInt32 = 2
    # var c = b < a # compile error
    var c = a < b   # compile error
    print(c)

Does this mean there is no sign promotion whatsoever? Is this intended?