modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
22.92k stars 2.58k forks source link

[BUG] Mojo parser crash. #2276

Open joelflaig opened 5 months ago

joelflaig commented 5 months ago

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)
soraros commented 5 months ago

Smaller repro:

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

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

joelflaig commented 5 months ago

OK...

dayeondev commented 5 months ago

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 commented 5 months ago

Hi @JoeLoser, could you please close this issue?

soraros commented 5 months ago

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

ematejska commented 5 months ago

Reproduces