r/cpp
•
u/steveklabnik1
•
Oct 24 '24
•
1
Why Safety Profiles Failed
https://www.circle-lang.org/draft-profiles.html
180
Upvotes
View all comments
Show parent comment
2
u/MEaster Oct 28 '24
I had a similar idea about generalizing it, and worked on it a bit last night. This is my effort, which has the key type genericised in the same way as the hashmap API, and also supports nested slices.
However, when I used it with a String key MIRI got cranky. It says the unique reference got invalidated by the second lookup. I'm not 100% sure why, it could be down to the internal implementation of the hashmap making a reasonable assumption that it doesn't need to worry about creating a &mut to the key. This could be something that just needs to be part of the library implementing the hashmap.
It's kind of a double-edged sword. Having this prohibition against aliasing is very helpful in that you have guarantees about access. If I have a &mut I know for a fact that this is the only part in the entire program across all threads that has access to that memory. That enables me to make certain assumptions when writing the code that would not be reasonable otherwise, which can result in being able to write code that performs better.
For a simple example, Rust's Mutex<T> is a container, and the only way to get access to the item inside is to lock the mutex first, then use the guard to gain a &mut T to the item. However, if you are able to get a &mut to the mutex, then you can get a &mut T directly without locking. The aliasing prohibition guarantees that the runtime synchronization isn't needed.
On the other hand it can be an issue if when you are doing cannot be proven to not alias in such a way to satisfy the imperfect enforcers. Especially as when you are doing becomes more complex.
If you're implementing your own custom data structures, there's a reasonable chance that you'd need unsafe anyway if you're doing it at a low level, and not just wrapping up an existing structure.
One thing I think is worth considering here is separating the rules being enforced from the enforcer of those rules. Having unsafe is an acceptance that the thing enforcing the rules (the compiler) is not perfect, and cannot be perfect (thank you Rice). There are limits to what it can reason about, meaning it will reject code that technically follows the rules.
A classic example here would be this. The two methods borrow the entirety of Foo, so the borrow checker rejects it. But any programmer can look at that code and see that the two returned references are disjoint, and that it wouldn't violate the aliasing rules. There's two problems at play here: the first is that the current borrow checker implementation isn't capable of reasoning about it across function calls. The next generation Polonius model is capable of it, but hasn't been fully implemented yet.
However that brings us to the second issue, which should sound familiar: even if we have a fully implemented Polonius model, the rust source code doesn't have enough information. Those two function signatures state that they borrow the entirety of Foo, not a specific field. So even though the Polonius model could reason about it, it's limited by the information its given.
Yes, the mere existence of aliased &muts is fully undefined behaviour in the same sense as C++ uses it. But you are correct, in that you need to be very careful when your unsafe code involves both references and raw pointers. It can be surprisingly easy to accidentally create a reference. In fact, the latest release of Rust added syntax to help with that. It can actually be easier and safer to stay with raw pointers as much as possible, and only deal with references on the "edges" of your code. This is because raw pointers do not have the aliasing restriction.