Skip to content

Commit 232da78

Browse files
committed
Add notes on parametricity and Any.
Props to huonw for pointing this out, since it forced me to think through the cases where the near-parametricity of `T` matters. Notably, it does not suffice to focus solely on the case where `v` owns data of type `T`; one must also consider the scenario where the destructor for `v` constructs a `T` on the fly.
1 parent 9fd6c72 commit 232da78

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

text/0000-sound-generic-drop.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,37 @@ Condition (B.) is catch cases like `Zook<B<'a>>` from
287287
[the Zook example], where the destructor's interaction with borrowed
288288
data is hidden behind a method call in the `fn drop`.
289289

290+
## Near-complete parametricity suffices
291+
292+
### Noncopy types
293+
294+
All non-`Copy` type parameters are (still) assumed to have a
295+
destructor. Thus, one would be correct in noting that even a type
296+
`T` with no bounds may still have one hidden method attached; namely,
297+
its `Drop` implementation.
298+
299+
However, the drop implementation for `T` can only be called when
300+
running the destructor for value `v` if either:
301+
302+
1. the type of `v` owns data of type `T`, or
303+
304+
2. the destructor of `v` constructs an instance of `T`.
305+
306+
In the first case, the Drop-Check rule ensures that `T` must satisfy
307+
either Condition (A.) or (B.). In this second case, the freshly
308+
constructed instance of `T` will only be able to access either
309+
borrowed data from `v` itself (and thus such data will already have
310+
lifetime that strictly outlives `v`) or data created during the
311+
execution of the destructor.
312+
313+
### `Any` instances
314+
315+
All types implementing `Any` is forced to outlive `'static`. So one
316+
should not be able to hide borrowed data behind the `Any` trait, and
317+
therefore it is okay for the analysis to treat `Any` like a black box
318+
whose destructor is safe to run (at least with respect to not
319+
accessing borrowed data).
320+
290321
## Strictly outlives
291322
[strictly-outlives]: #strictly-outlives
292323

0 commit comments

Comments
 (0)