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
1
u/MEaster Nov 04 '24
It takes time to learn the details of a new system. I'm having this from the other side learning about your sccptools.
Yeah. You can think of this as being analogous to iterator invalidation: the pointers are valid up until the source is accessed in the right way. Though for this "virtual borrow" it's any access.
Note that this only applies when references are involved. If you only have raw pointers then you can have mutable aliasing (as long as you don't data race), because raw pointers are not subject to this requirement. That's why it can be easier/safer to stick to raw pointers while doing unsafe operations.
For the consuming idea, this has two issue. The first is that it's more restrictive than safe Rust. For example, this is perfectly valid according to the borrow checker:
The second is that it actually runs into one of the implicit manipulations that Rust has to make it not be super annoying to write, which is that it inserts reborrows when you pass a &mut into a function call. The definition of
inner_refabove is a reborrow. If it didn't do this, you would need to insert them manually every time due to &muts being move-types.The second idea has the problem of requiring the raw pointer to be borrow checked to prevent it escaping the closure, but the entire point of a raw pointer is that it's not borrow checked. Though the idea is very similar to how Rust's scoped threads make it safe to share references to stack-owned data with other threads.
Yeah, like the old HP calculators. The use case is so I can hack on a compiler because it's fun. It's stack based primarily because the project started as a Rust implementation of Porth, which is also stack based. I started doing my own thing shortly after functions were added.
Possibly. That would have to be discussed with Sean Baxer, though, and whether there's other potential soundness issues that we haven't thought of.
Yeah, the borrow checker wants your ownership and borrow structure to be tree-like, and doubly-linked lists are not trees. The borrow checker gets very cranky.
Ah, I see! So it's got some sort of list (intrusive singly-linked list?) of the pointees that it can check when it's moved? Or could this just be done with some sort of reference count?
Wouldn't this effectively require whole-program analysis? Though for libraries (which are possibly on the smaller side) this is more feasible.
Plus you can do some manual cleanup afterwards for times when the auto-translator can't see that things could be done in a simpler way.
So specialization exists as an unstable feature in Rust, and the standard library takes advantage of it. However, from what I understand it is extremely easy to create undefined behaviour, because of (I think) something to do with lifetimes and variance. I believe work is ongoing, but there's only so many people to go around with enough knowledge to do it.
That matches benchmarks that I've seen over the years. Sometimes C++ is faster, sometimes Rust, but they're both capable of the same level of performance.