Open
Description
Currently, the following is illegal according to Stacked Borrows:
let val = [1u8, 2];
let ptr = &val[0] as *const u8;
let _val = unsafe { *ptr.add(1) };
The problem is that the cast to *const u8
creates a raw pointer that may only be used for the u8
it points to, not anything else. The most common case is to do &slice[0] as *const _
instead of slice.as_ptr()
.
This has lead to problems:
- rand did the
&slice[0]
thing. - Same for hashbrown.
Rc::into_raw
+Rc::from_raw
don't work well together because of this.- capnproto also used the
&slice[0]
pattern
Maybe this is too restrictive and raw pointers should be allowed to access their "surroundings"? I am not sure what exactly that would look like though. It would probably require having the raw pointer fully inherit all permissions from the reference it is created from.
I'll use this issue to collect such cases.