modularml / mojo

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

[BUG] Can't create the trait `Divmodable` #2597

Open gabrieldemarmiesse opened 6 months ago

gabrieldemarmiesse commented 6 months ago

Bug description

The goal here was to create a trait Divmodable which accepts anything implementing __divmod__. By trying to do this, we get a very strange error. See below. Maybe I'm just misusing traits and I'm missing something.

Steps to reproduce

trait Divmodable(CollectionElement):
    fn __divmod__(self: Self, other: Self) -> Tuple[Self, Self]:
        ...

def main():
    pass
$ mojo trying_stuff.mojo
/projects/open_source/mojo/trying_stuff.mojo:3:53: error: no '__copyinit__' candidates have type 'fn(inout self: T, existing: T, /) -> None'
    fn __divmod__(self: Self, other: Self) -> Tuple[Self, Self]:
                                                    ^~~~
/projects/open_source/mojo/trying_stuff.mojo:3:53: error: 'Tuple' parameter #0 has 'CollectionElement' type, but value has type 'Divmodable'
    fn __divmod__(self: Self, other: Self) -> Tuple[Self, Self]:
                                                    ^~~~
/projects/open_source/mojo/trying_stuff.mojo:1:1: note: 'Tuple' declared here

^
mojo: error: failed to parse the provided Mojo source module

System information

ubuntu 22.04 in wsl

modular 0.7.2 (d0adc668)
mojo 2024.5.822 (2cfed0ad)
ematejska commented 5 months ago

Errors are updated with the latest nightly build (mojo 2024.5.2805 (87564503)):

error: no '__moveinit__' candidates have type 'fn(inout self: T, owned existing: T, /) -> None'

    fn __divmod__(self: Self, other: Self) -> Tuple[Self, Self]:

                                                    ^~~~

error: 'Tuple' parameter #0 has 'Movable' type, but value has type 'Divmodable'

    fn __divmod__(self: Self, other: Self) -> Tuple[Self, Self]:

                                                    ^~~~

note: 'Tuple' declared here

trait Divmodable(CollectionElement):