Stream: t-lang/wg-unsafe-code-guidelines

Topic: stacked borrows: change tag on deref or cast?

RalfJ (Nov 16 2018 at 22:01, on Zulip):

One slightly unsatisfying point in stacked borrows is that I have a rule that when you deref a raw pointer (*x), that deref forgets the tag -- so e.g. &*x is not actually the identity on a raw pointer. I now realized I could get rid of this if instead I make it so that casts forget the tag. Now I am torn because I am not sure what I like more: cast being the same as transmute (in MIR; the surface-level x as *mut _ also emits an EscapeToRaw and is very much not like a transmute), or deref not changing the tag (and hence &* being the identity). I think the compiler optimizes &* away, maybe even for raw ptr, and does not optimize casts away, which indicates erase-tag-on-cast might be better than erase-tag-on-raw-ptr-deref. Any thoughts?

RalfJ (Nov 17 2018 at 08:07, on Zulip):

After sleeping about it, I think I prefer erase-tag-on-cast. That makes a raw ptr deref do absolutely nothing to the tag and the stack, which means we can faithfully say that ptr derefs in C are like raw ptr derefs in Rust.
It also means that you better use a cast, not a transmute, to "export" your pointers. But that has pretty much already been true before this change (and I am aware it can be a problem for some patterns).

Last update: Jul 03 2020 at 17:20UTC