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
Helehex6d 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
blblblbl6d 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 Daniel6d ago
I'm interested in understanding this as well. @Owen Hilyard can you help?
Darkmatter
Darkmatter6d ago
This looks like a compiler bug, the existence of element should prevent calling list.append.
blblblbl
blblblbl6d ago
Why should it prevent it? I haven't found anything about it in docs
Darkmatter
Darkmatter6d 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
ModularBot6d ago
Congrats @Owen Hilyard, you just advanced to level 24!
Darkmatter
Darkmatter6d ago
That's the property that provides memory safety and data race safety to both Mojo and Rust.
blblblbl
blblblbl6d ago
What makes smth an Alias?
ModularBot
ModularBot6d ago
Congrats @blblblbl, you just advanced to level 1!
Darkmatter
Darkmatter6d 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
blblblbl6d 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
Darkmatter6d 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
blblblbl6d 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
Darkmatter6d ago
__lifetime_of(self) is the correct behavior
blblblbl
blblblbl6d ago
So __lifetime_of(self) should already provide information to compiler that Reference(list[0]) is an Alias to list?
Darkmatter
Darkmatter6d ago
Yes
blblblbl
blblblbl6d 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
Darkmatter6d 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
blblblbl6d ago
On a side note, should I make an issue in Github about this?
Darkmatter
Darkmatter6d ago
Please do, I was about to start filing it.
blblblbl
blblblbl6d 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
Darkmatter6d ago
Thank you
Want results from more Discord servers?
Add your server