mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[BUG] Mojo parser crash.

Open joelflaig opened this issue 10 months ago • 6 comments

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)

joelflaig avatar Apr 12 '24 23:04 joelflaig

Smaller repro:

struct T:
  fn __moveinit__(inout self, owned other):
    ...

Crash the compiler (not just the LSP) in nightly.

soraros avatar Apr 13 '24 00:04 soraros

OK...

joelflaig avatar Apr 13 '24 20:04 joelflaig

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.

dayeondev avatar Apr 18 '24 17:04 dayeondev

Hi @JoeLoser, could you please close this issue?

dayeondev avatar Apr 18 '24 17:04 dayeondev

@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.

soraros avatar Apr 18 '24 18:04 soraros

Reproduces

ematejska avatar Apr 18 '24 23:04 ematejska