How do safe Reference and List work together?

From my understanding Reference is just a pointer in memory with associated lifetime. But I don't understand how the following code doesn't crash
var list: List[Int] = List(10,11,12)
var element = Reference(list[0])
list.resize(0)
for i in range(10000):
list.append(i)
print(element[])
var list: List[Int] = List(10,11,12)
var element = Reference(list[0])
list.resize(0)
for i in range(10000):
list.append(i)
print(element[])
Certainly reallocation should have happened here and therefore element should point to invalid memory location, which should cause segfault. But it prints 0. What am I missing?
23 Replies
Helehex
Helehex2mo ago
On latest nightly:
fn main():
var list: List[Int] = List(10,11,12)
var element = Pointer.address_of(list[0])
print(element[]) # prints 10
for i in range(10000):
list.append(i)
print(element[]) # prints 0
fn main():
var list: List[Int] = List(10,11,12)
var element = Pointer.address_of(list[0])
print(element[]) # prints 10
for i in range(10000):
list.append(i)
print(element[]) # prints 0
blblblbl
blblblblOP2mo ago
But why doesn't it segfault? Or maybe even more precise questions are: 1. Does this code has well defined behaviour? 2. If yes, how does this work? If not, then how safe is safe Reference?
Melody Daniel
Melody Daniel2mo ago
I'm interested in understanding this as well. @Owen Hilyard can you help?
Darkmatter
Darkmatter2mo ago
This looks like a compiler bug, the existence of element should prevent calling list.append.
blblblbl
blblblblOP2mo ago
Why should it prevent it? I haven't found anything about it in docs
Darkmatter
Darkmatter2mo ago
Mutable xor Alias. You need to mutate the list to append to it, but doing there is an alias to a list element (element), which means mutation should not be allowed until element is dropped.
ModularBot
ModularBot2mo ago
Congrats @Owen Hilyard, you just advanced to level 24!
Darkmatter
Darkmatter2mo ago
That's the property that provides memory safety and data race safety to both Mojo and Rust.
blblblbl
blblblblOP2mo ago
What makes smth an Alias?
ModularBot
ModularBot2mo ago
Congrats @blblblbl, you just advanced to level 1!
Darkmatter
Darkmatter2mo ago
If there are multiple references to it. Calling any method takes a reference to self, so should only be able to call methods which can't change the list in any way until you no longer have a pointer into the list. The Pointer[is_mutable=False, T] type is closer to const &T in C++. UnsafePointer is like T*. Pointer[is_mutable=True, T] is &T.
blblblbl
blblblblOP2mo ago
But we have mutable reference to a value inside UnsafePointer inside a List. Should compiler understand that this reference is also an alias for List?
Darkmatter
Darkmatter2mo ago
Collections are supposed to return a type which has a reference tied to self. It looks like Pointer is not actually handing that information correctly.
blblblbl
blblblblOP2mo ago
In my original question I used Reference(list[0]). __getitem__ of list returns a ref [__lifetime_of(self)] T, so I guess there is no room to have reference to self So, should __getitem__ return something else more complex, that has reference to self?
Darkmatter
Darkmatter2mo ago
__lifetime_of(self) is the correct behavior
blblblbl
blblblblOP2mo ago
So __lifetime_of(self) should already provide information to compiler that Reference(list[0]) is an Alias to list?
Darkmatter
Darkmatter2mo ago
Yes
blblblbl
blblblblOP2mo ago
Got you, thx! Probably it is a good idea to write in docs about aliases and the fact that lifetime not only manages value lifetime but also controls aliasing
Darkmatter
Darkmatter2mo ago
There was a big rewrite of a bunch of these semantics a week or two ago, so docs are waiting for experimentation to end.
blblblbl
blblblblOP2mo ago
On a side note, should I make an issue in Github about this?
Darkmatter
Darkmatter2mo ago
Please do, I was about to start filing it.
blblblbl
blblblblOP2mo ago
GitHub
[BUG] Compiler doesn't prevent mutation of an aliased variable · Is...
Bug description Compiler doesn't prevent mutation of a List variable using append method, when a Reference to a list element exists Link to discussion in Discord: link Steps to reproduce fn mai...
Darkmatter
Darkmatter2mo ago
Thank you
Want results from more Discord servers?
Add your server