-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Comments
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. |
This should be fixed in the next release |
The crash -- it should emit an error |
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:
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. |
Bug description
The following code crashes the 'mojo build' on Apple M1.
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:or sometimes:
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
The text was updated successfully, but these errors were encountered: