mojo
mojo copied to clipboard
[BUG] Mojo parser crash.
The code below produces a LSP parser crash.
struct Type(CollectionElement):
var data: Int
fn __init__(inout self, num: Int):
self.data = num
fn __moveinit__(inout self, owned other):
self.data = other.data
fn __copyinit__(inout self, owned other):
self.data = other.data
fn __str__(inout self) -> String:
return str(self.data)
fn main():
var d0 = Type(0)
var d1 = Type(1)
var d2 = Type(2)
System information
OS: Debian 12
Mojo version: mojo 24.2.1 (58157dc0)
Modular CLI version: modular 0.6.0 (04c05243)
Smaller repro:
struct T:
fn __moveinit__(inout self, owned other):
...
Crash the compiler (not just the LSP) in nightly.
OK...
Hello, @joelflaig @soraros
After reviewing the code and comparing it with the standard library implementations, I noticed that the copyinit function in your example includes the owned qualifier for a parameter that typically does not transfer ownership. In standard copyinit implementations, this qualifier is usually omitted because the function is intended to copy the data without transferring ownership.
To resolve the parser crash, you should remove the owned qualifier from the other parameter in the copyinit function. Here's the corrected version:
fn __copyinit__(inout self, other: Self):
self.data = other.data
Please try this change and if the issue remains, feel free to reopen the issue.
Hi @JoeLoser, could you please close this issue?
@dayeondev you are correct that this is invalid code. However, the compiler should reject it gracefully instead of crashing, thus we shouldn't close the issue.
Reproduces