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] Mojo build crashes when using Optional and struct #2670

Closed
ericqu opened this issue May 15, 2024 · 4 comments
Closed

[BUG] Mojo build crashes when using Optional and struct #2670

ericqu opened this issue May 15, 2024 · 4 comments
Labels
bug Something isn't working mojo-repo Tag all issues with this label

Comments

@ericqu
Copy link

ericqu commented May 15, 2024

Bug description

The following code crashes the 'mojo build' on Apple M1.

from collections import Optional

@value
struct Tree:
    var left :Optional[Tree]
    var right :Optional[Tree]

fn main() -> None :
    var t = Tree(Optional[Tree](None), Optional[Tree](None))

I was experimenting with Optional, so I likely did something wrong, although I hoped for a compiler error, not a crash.
I wonder if it is related to the recursive nature of the struct, although it seems a natural way to implement something like a tree.

Steps to reproduce

the source code is provided in the description above. Then I execute mojo build src/test_bin.mojo and it gives:

[47079:11093979:20240515,231828.233868:WARNING crash_report_exception_handler.cc:257] UniversalExceptionRaise: (os/kern) failure (5)
zsh: illegal hardware instruction  mojo build src/test_bin.mojo

or sometimes:

[47132:11096302:20240515,232253.957242:WARNING crash_report_exception_handler.cc:257] UniversalExceptionRaise: (os/kern) failure (5)
zsh: bus error  mojo build src/test_bin.mojo

I did also try on the playground and got the following crash details
mojo-stack.txt

It looks like there are other issues reported with Optional, although they don't crash the compiler.
Let me know if any other information could help.

System information

- What OS did you do install Mojo on ? MacOs with Apple M1
- Provide version information for Mojo by pasting the output of `mojo -v` : mojo 24.3.0 (9882e19d)
- Provide Modular CLI version by pasting the output of `modular -v` : modular 0.8.0 (39a426b5)
@ericqu ericqu added bug Something isn't working mojo-repo Tag all issues with this label labels May 15, 2024
@ericqu ericqu changed the title [BUG] Mojo build crash when using Optional and struct [BUG] Mojo build crashes when using Optional and struct May 15, 2024
@Ryul0rd
Copy link

Ryul0rd commented May 22, 2024

This is because your struct is recursive. A struct has all its fields stored contiguously in memory and the total size is the sum of the size of those fields. This means you can't have a struct with a field that has the same type as the struct itself because you get infinite recursion trying to determine the size. You get around this by using pointers since a pointer has fixed size regardless of what's being pointed to.

The fact that you get a crash instead of a useful error is still an issue though. The mojo vscode plugin also crashes if you attempt this.

@Mogball
Copy link
Collaborator

Mogball commented May 22, 2024

This should be fixed in the next release

@Mogball Mogball closed this as completed May 22, 2024
@Mogball
Copy link
Collaborator

Mogball commented May 22, 2024

The crash -- it should emit an error

@ericqu
Copy link
Author

ericqu commented May 30, 2024

Thanks for looking into this, and my apologies for the belated answer.

I would point out that the following, suggested, code gives a similar output in the same version of Mojo and the Playground:

from collections import Optional
from memory.unsafe_pointer import UnsafePointer

@value
struct Tree:
    var item: Optional[UnsafePointer[Tree]]

fn main() -> None:
    var t = Tree(None)

I am unsure what the next release means (a bit off-topic: I tried to install nightly/mojo. However, it failed, so I couldn't try the code with nightly if it meant the next version on nightly). It would be nice to have a nightly Playground so that a regular user could test if a minimal reproducible example is fixed.

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

3 participants