{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreihtstgut5d56w4nilprpbp7d3cxl3qtkehkitbt2u6avsflxhzd2i",
"uri": "at://did:plc:ivbknywyskln22er3nkssdhl/app.bsky.feed.post/3mlzpdjr7jq72"
},
"path": "/t/reducing-raw-pointer-footguns-preventing-reference-aliasing-violations-at-compile-time/24301?page=2#post_28",
"publishedAt": "2026-05-17T04:38:48.000Z",
"site": "https://internals.rust-lang.org",
"textContent": "robofinch:\n\n> I now get that you're trying to make a tool for reducing the likelihood of (accidentally) intermixing references and raw pointers. I do think it's worth noting that, AFAICT, the warning \"don't mix accesses between references and raw pointers\" is very common. It doesn't seem anywhere near to being the most complicated part of the aliasing rules, so I'm not sure that this tool has a good target audience.\n\nThe correct way to fix this is to give raw pointers a lifetime, and have them borrow the reference from which they were created. One potential downside of this approach is that people might interpret the lifetime on the raw pointer as being the lifetime of the object it points to, rather than the lifetime of the reference (if any) from which it was created – but it has the huge upside of preventing a wide range of provenance-related mistakes and thus making unsafe code a bit safer. There are still a couple of types of mistakes it won't catch (creating multiple mutable references from the same pointer, and offsetting the pointer outside the range of the original object and dereferencing it, but it would be much safer).\n\nI suspect that long-term, it would be a good approach to deprecate raw pointers in favour of using types that represent the specific invarians you want to relax (e.g. most `*mut T` could instead be represented as `&'a UnsafeCell<MaybeUninit<T>>` – possibly all, given that `&UnsafeCell` doesn't actually require its target to be allocated).",
"title": "Reducing Raw Pointer Footguns: Preventing Reference Aliasing Violations at Compile Time"
}