You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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)
The text was updated successfully, but these errors were encountered:
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.
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.
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: