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

[BUG] Optional[ArcPointer] as Struct member is crashing #3913

Open
gtvicentini opened this issue Dec 27, 2024 · 1 comment
Open

[BUG] Optional[ArcPointer] as Struct member is crashing #3913

gtvicentini opened this issue Dec 27, 2024 · 1 comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label

Comments

@gtvicentini
Copy link

Bug description

I'm trying to implement a self-referential Struct using Optional[ArcPointer], since this seems to be the recommended approach while OwnedPointer can't be used with Optional. Mojo is crashing when I define the following Struct:

# f.mojo

from memory import ArcPointer
from collections import Optional

struct S:
    var s: Optional[ArcPointer[S]]

    fn __init__(out self):
        self.s = None

    fn __moveinit__(out self, owned existing: Self):
        self.s = existing.s

def main():
    print("")
> mojo run f.mojo
[34050:1789565:20241226,195419.434048:WARNING crash_report_exception_handler.cc:257] UniversalExceptionRaise: (os/kern) failure (5)
[1]    34048 bus error  mojo run f.mojo

Steps to reproduce

# f.mojo

from memory import ArcPointer
from collections import Optional

struct S:
    var s: Optional[ArcPointer[S]]

    fn __init__(out self):
        self.s = None

    fn __moveinit__(out self, owned existing: Self):
        self.s = existing.s

def main():
    print("")
> mojo run f.mojo
[34050:1789565:20241226,195419.434048:WARNING crash_report_exception_handler.cc:257] UniversalExceptionRaise: (os/kern) failure (5)
[1]    34048 bus error  mojo run f.mojo

System information

- OS: MacOS Sequoia 15.2 (Apple Silicon)
- mojo -v: 24.6.0 (4487cd6e)
- magic -V: magic 0.5.1 - (based on pixi 0.37.0)
@gtvicentini gtvicentini added bug Something isn't working mojo-repo Tag all issues with this label labels Dec 27, 2024
@thatstoasty
Copy link
Contributor

I've run into this as well while working on a cli library. I ended up using a ArcPointer[Optional[T]] for a bit, but recently switched to a List[ArcPointer[T]] and just make sure the list is only ever 0 or 1 elements. Not ideal, but works in the interim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

2 participants