https://issues.dlang.org/show_bug.cgi?id=24162

--- Comment #6 from Jonathan M Davis <issues.dl...@jmdavisprog.com> ---
All the GC needs is a pointer to an address in block of memory that it
controls, and taking a reference to a member of a dynamic array that's a slice
of GC-allocated memory will do that. There is absolutely nothing untoward going
on here with regards to memory safety.

Yes, the caller might be very surprised if the result isn't reflected in arr[2]
of the dynamic array after the function returns, but any time that you pass a
dynamic array to a function that could choose to append to it, it could result
in the dynamic array being reallocated. And if the function in question didn't
append to the array, then having passed arr[2] wouldn't be a problem at all.
But as for mutable aliasing in general, that's how dynamic arrays function in
D. They're just slices of memory that they don't control, and you're allowed to
slice those arrays as much as you'd like. And as long as they're slices of
GC-allocated memory, it's all memory safe. So, trying to prevent mutable
aliasing when it comes to dynamic arrays is not going to work. For better or
worse, it's fundamental to how dynamic arrays work in D. And for the most part,
it works just fine much as it can be confusing at first.

--

Reply via email to