modularml / mojo

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

[BUG] Can't form alias to `Int`'s dunder methods #3213

Open soraros opened 2 months ago

soraros commented 2 months ago

Bug description

As title. It's odd since most other types work.

Steps to reproduce

fn main():
    alias le = Int.__le__  # error: cannot form a reference to overloaded declaration of '__le__'

System information

Mojo 2024.7.1022 (1e94b6a0)
Mogball commented 2 months ago
cannot form a reference to overloaded declaration of '__le__'

This is intended behaviour. The real question is whether this method should be overloaded.

Mogball commented 2 months ago

Will defer to @joe

soraros commented 2 months ago

This is intended behaviour. The real question is whether this method should be overloaded.

That's the thing: it's not overloaded, unless overloading means something very different in Mojo.

Mogball commented 2 months ago

Ah, then this is indeed a bug

soraros commented 2 months ago

It also happens to Bool, but for instance not to SIMD.

soraros commented 2 months ago

The problem seems to be that @register_passable doesn't work well with trait explicit bound (EqualityComparable in this case). If either is removed, the problem goes away.

fn main():
    Int.__eq__

@register_passable
struct Int(EqualityComparable):
    fn __eq__(self, rhs: Self) -> Bool:
        return False

    fn __ne__(self, rhs: Self) -> Bool:
        return False