Skip to content

Commit 4d23931

Browse files
committed
attempts at clarification
1 parent f5c12b3 commit 4d23931

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

text/0000-maybe-dangling.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ The examples described above are far from artificial, here are some real-world c
9494
To handle situations like this, Rust has a special type called `MaybeDangling<P>`:
9595
references and boxes in `P` do *not* have to be dereferenceable or follow aliasing guarantees.
9696
This applies inside nested references/boxes inside `P` as well.
97-
They still have to be non-null and aligned, and it has to at least be *possible* that there exists valid data behind that reference (i.e., `MaybeDangling<&!>` is still invalid), but the rules are relaxed when compared with just a plain `P`.
97+
They still have to be non-null and aligned, and it has to at least be *possible* that there exists valid data behind that reference (i.e., `MaybeDangling<&!>` is still invalid).
9898
Also note that safe code can still generally assume that every `MaybeDangling<P>` it encounters is a valid `P`, but within unsafe code this makes it possible to store data of arbitrary type without making reference guarantees (this is similar to `ManuallyDrop`).
99+
In other words, `MaybeDangling<P>` is entirely like `P`, except that the rules that relate to the contents of memory that pointers in `P` point to (dereferencability and aliasing restrictions) are suspended when the pointers are not being actively used.
100+
You can think of the `P` as being "suspended" or "inert".
99101

100102
The `ManuallyDrop<T>` type internally wraps `T` in a `MaybeDangling`.
101103

@@ -129,7 +131,7 @@ As long as the `buffer` field is not used, the pointer stored in `ref_` will rem
129131
[reference-level-explanation]: #reference-level-explanation
130132

131133
The standard library contains a type `MaybeDangling<P>` that is safely convertible with `P` (i.e., the safety invariant is the same), and that has all the same niches as `P`, but that does allow passing around dangling boxes and references within unsafe code.
132-
`MaybeDangling` propagates auto traits and has (at least) `derive(Copy, Clone, Debug)`.
134+
`MaybeDangling<P>` propagates auto traits, drops the `P` when it is dropped, and has (at least) `derive(Copy, Clone, Debug)`.
133135

134136
"Behavior considered undefined" is adjusted as follows:
135137

@@ -200,6 +202,7 @@ Miri is adjusted as follows:
200202
- The most obvious alternative is to declare `ManuallyDrop` to be that magic type with the memory model exception.
201203
This has the disadvantage that one risks memory leaks when all one wants to do is pass around data of some `T` without upholding reference liveness.
202204
For instance, the third example would have to remember to call `drop` on the `buffer`.
205+
This alternative has the advantage that we avoid introducing another type, and it is future-compatible with factoring that aspect of `ManuallyDrop` into a dedicated type in the future.
203206
- The other alternative is to change the memory model such that the example code is fine as-is.
204207
There are several variants of this:
205208
- [Make all examples legal] All newtype wrappers behave the way `MaybeDangling` is specified in this RFC.
@@ -248,10 +251,11 @@ Notice that `UnsafeCell` acts "behind references" while `MaybeDangling`, like `M
248251
[unresolved-questions]: #unresolved-questions
249252

250253
- What should the type be called?
251-
`MaybeDangling` is somewhat misleading since the safety invariant still requires everything to be dereferenceable, only the validity invariant is relaxed.
254+
`MaybeDangling` is somewhat misleading since the safety invariant still requires everything to be dereferenceable, only the requirement of dereferencability and noalias is relaxed.
252255
This is a bit like `ManuallyDrop` which supports dropping via an `unsafe` function but its safety invariant says that the data is not dropped (so that it can implement `Deref` and `DerefMut` and a safe `into_inner`).
253256
Furthermore, the type also allows maybe-aliasing references, not just maybe-dangling references.
254-
- Should `MaybeDangling` implement `Deref` and `DerefMut` like `ManuallyDrop` does, or should accessing the inner data be more explicit since that is when the aliasing and validity requirements do come back in full force?
257+
Other possible names might be things like `InertPointers` or `SuspendedPointers`.
258+
- Should `MaybeDangling` implement `Deref` and `DerefMut` like `ManuallyDrop` does, or should accessing the inner data be more explicit since that is when the aliasing and dereferencability requirements do come back in full force?
255259

256260
# Future possibilities
257261
[future-possibilities]: #future-possibilities

0 commit comments

Comments
 (0)