Description
In the C++ world, typically, a "dangling" pointer is one to memory that was previously allocated and subsequently deallocated. Let's call these freed-dangling. These are just bad, period.
In Rust, APIs like NonNull::dangling()
` produce pointers that were not ever allocated, primarily constructed to be "aligned but not null".. Let's call these align-dangling. It's good practice to use align-dangling pointers when working with collections or ZSTs.
We tend to use these meanings interchangeably. They are largely the same when it comes to the validity of pointer accesses, except around zero-sized types: zero-sized reads are not valid when reading from freed memory, unless the pointer was obtained as a direct cast from an integer literal (presumably these get normalized to align-dangling pointers).
This can lead to some confusion around terminology, for example with this code (credit @kupiakos), where we have both a freed-dangling and an align-dangling NonNull::dangling()
pointer, and miri complains about the former, but says "out-of-bounds pointer use: alloc921 has been freed, so this pointer is dangling", implicitly calling it "dangling".
We should potentially pin down what meaning of "dangling" we intend everywhere, use it consistently, and update the docs of NonNull::dangling()
to reference ZST validity. We don't necessarily need to split these meanings as long as we're clear about it; splitting the meanings may cause more confusion than it's worth.