mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[BUG] Owned keyword does not work as expected 🤢🤷‍♂️

Open a62690705 opened this issue 10 months ago • 12 comments

Bug description

Mojo's ownership system assumes that when ownership of a value is transferred from one variable to another, the value is not actually moved from memory, but rather the memory location of the value is renamed with the name of the new owner. The value remains in place without moving in memory, right? However, contrary to what is expected, the value in the following example moved from its place to a new location, from location 0x7ffdaf8b5240 to location 0x7ffdaf8b5260.

@value
struct MyPet:
    var name: String
    var age: Int

    fn __init__(inout self, owned name: String, age: Int):
        print("address of name    	", Pointer.address_of(name), "\n")

        self.name = name ^
        print("address of self.name    	", Pointer.address_of(self.name))
        self.age = age
        print("address of age	        ", Pointer.address_of(self.age))


def main():
    var s = String("mark")
    print("address of s       	", Pointer.address_of(s))

    var my_pet = MyPet(s, 33)


Output

address of s       	 0x7ffdaf8b5240
address of name    	 0x7ffdaf8b5240 

address of self.name     0x7ffdaf8b5260
address of age	         0x7ffdaf8b5258

Steps to reproduce

  • Include relevant code snippet or link to code that did not work as expected.
  • If applicable, add screenshots to help explain the problem.
  • If using the Playground, name the pre-existing notebook that failed and the steps that led to failure.
  • Include anything else that might help us debug the issue.

System information

- What OS did you do install Mojo on ?
- Provide version information for Mojo by pasting the output of `mojo -v`   24.2
- Provide Modular CLI version by pasting the output of `modular -v`

a62690705 avatar Mar 31 '24 16:03 a62690705