modular icon indicating copy to clipboard operation
modular copied to clipboard

[BUG] Redefinition of method with identical signatures when they're different

Open gabrieldemarmiesse opened this issue 1 year ago • 1 comments

Bug description

I get the following error:

$ mojo trying_stuff2.mojo
/projects/open_source/mojo/trying_stuff2.mojo:5:8: error: redefinition of function '__init__' with identical signature
    fn __init__(inout self, *, some_other_arg: Self):
       ^
/projects/open_source/mojo/trying_stuff2.mojo:2:8: note: previous definition here
    fn __init__(inout self, some_arg: Self):
       ^
mojo: error: failed to parse the provided Mojo source module

While the two signatures are clearly different. it's not possible to have ambiguity in the call of the constructor, so there is no reason for the compiler to disallow it.

Steps to reproduce

struct Something:
    fn __init__(inout self, some_arg: Self):
        pass

    fn __init__(inout self, *, some_other_arg: Self):
        pass

System information

Ubuntu
modular 0.8.0 (39a426b5)
mojo 2024.6.2805 (512c667b)

gabrieldemarmiesse avatar Jun 28 '24 11:06 gabrieldemarmiesse

Reproduces with mojo 2024.7.1005 (a9695bc7)

ematejska avatar Jul 11 '24 17:07 ematejska

It's been fixed as of mojo 25.1.0.dev2025011705 (92b12ebe), the following compiles successfully:

struct Something:
    fn __init__(out self):
        print("init")

    fn __init__(out self, some_arg: Self):
        print("first init")

    fn __init__(out self, *, some_other_arg: Self):
        print("second init")

fn main():
    s = Something()
    s2 = Something(s)
    s3 = Something(some_other_arg=s)

gabrieldemarmiesse avatar Jan 18 '25 14:01 gabrieldemarmiesse