-
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
Aliasing rules for Unique
#384
Comments
Miri now has optional support for making the Still, this lets us do some experiments. The good news is that the basic pattern of using a fn vec_push_ptr_stable() {
let mut v = Vec::with_capacity(10);
v.push(0);
let v0 = unsafe { &mut *(&mut v[0] as *mut _) }; // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
v.push(1);
*v0 = *v0;
} This works since after The bad news is that in this prototype, even some safe code raises an error: use std::cell::Cell;
use std::iter::FromIterator;
fn main() {
let dummies = Vec::from_iter((0..2).map(|id| Cell::new(id)));
let d = &dummies[0];
d.set(1);
for dummy in &dummies {
dummy.get();
}
d.set(1);
} This is due to the part I said above about the semantics having a lot more UB than LLVM We probably want |
Note: #326 (comment) If neither |
Sure, this entire discussion presupposes that we want some form of |
Here's another example of surprising UB (this time with no fn main() {
let mut s = Vec::with_capacity(10);
s.push(13);
let ptr = s.as_mut_ptr();
let ptr2 = s.as_mut_ptr();
unsafe { ptr2.write(0) };
unsafe { ptr.write(97) };
println!("{s:?}");
} The problem is that So for I should look into the newer scope-based |
This seems to be adding to the arguments of "Just Don't". |
Unique
is an ancient type that was originally intended to getnoalias
semantics, but hasn't been treated specially by the compiler in a while. However it could be a useful building block to let programmers inform the compiler about optimization potential.Unique
would benoalias
withoutdereferenceable
, which is not a combination that Stacked Borrows supports. However I hope that the next aliasing model will support that combination (it is also needed for the&Header
issue), and then we could consider imbuingUnique
with special semantics again.This is mostly orthogonal to #326, which boils down to deciding whether we want to use
Unique
in standard library containers or not.The text was updated successfully, but these errors were encountered: