-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should a mutable reference which is only used for reads become invalid on reads through other ptrs? #351
Comments
I'm very mixed on whether this is important or not. On the one hand, the code that the linked PR fixes is "obviously" silly. But it was checked in to the standard library, which I would hope gets some of the best code review out there, and is generally exposed to a lot of eyeballs (seems like every day I skim the list of open PRs on rust-lang/rust I see a few fixes for typos). So this is code that sort of obviously could have been cleaned up for reasons other than soundness... but it wasn't. But on the other hand, writing Rust code which is both generic and uses Ultimately though, I don't know how often this situation comes up. I feel like the code pattern that makes this UB is both bizarre yet easily enough written, so I don't know if my lack of examples is more an indication that it's not a problem or an indication that people are simply not testing for it and generic code is full of land mines. |
Note that one motivation for making this legal is to allow all &mut borrows to be "two-phase", using a special borrow stack element to encode this (see can &mut just always be two-phase). Under that proposal, a &mut which is only ever used for reading is essentially the same as a shared borrow in terms of the legal programs it permits. |
Note that a |
Prior art: LLVM's
|
Isn't LLVM's |
Yes indeed, Stacked Borrows quite deliberately is more restrictive than noalias/restrict, and allows more optimizations.
|
FWIW this is exactly what happens in Tree Borrows (for other reasons: to allow reordering reads). A mutable reference that is only read from accepts as much "interference" from other references as a shared reference does. |
Brought up by @saethlin around here: rust-lang/rust#92092 fixes a standard library issue where
So, this is a case where using a shared ref rather than a mutable ref was more correct, in the face of mutation inside an
UnsafeCell
.I have to say I consider this fully intended, the promise of a mutable reference is uniqueness and
UnsafeCell
deliberately has no effect on&mut
. But it is worth tracking whether violating this rule is a common issue, and whether it might be worth somehow allowing this. The most extreme version of this would in general allow reads through other pointers when using an&mut
(perhaps making the&mut
read-only in the process? that would be a new mechanism in the aliasing model though). But that will severely impact our even entirely remove our ability to reorder writes to mutable references.The text was updated successfully, but these errors were encountered: