Skip to content

Commit fa1326c

Browse files
authored
Merge pull request #276 from JohnTitor/phantomdata-link
Add a link to show why unused lifetimes on structs are forbidden
2 parents 5d7bec0 + 0d8cefe commit fa1326c

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/phantom-data.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ struct Iter<'a, T: 'a> {
1313
```
1414

1515
However because `'a` is unused within the struct's body, it's *unbounded*.
16-
Because of the troubles this has historically caused, unbounded lifetimes and
17-
types are *forbidden* in struct definitions. Therefore we must somehow refer
18-
to these types in the body. Correctly doing this is necessary to have
19-
correct variance and drop checking.
16+
[Because of the troubles this has historically caused][unused-param],
17+
unbounded lifetimes and types are *forbidden* in struct definitions.
18+
Therefore we must somehow refer to these types in the body.
19+
Correctly doing this is necessary to have correct variance and drop checking.
20+
21+
[unused-param]: https://rust-lang.github.io/rfcs/0738-variance.html#the-corner-case-unused-parameters-and-parameters-that-are-only-used-unsafely
2022

2123
We do this using `PhantomData`, which is a special marker type. `PhantomData`
2224
consumes no space, but simulates a field of the given type for the purpose of
@@ -25,7 +27,7 @@ the type-system the kind of variance that you want, while also providing other
2527
useful things such as the information needed by drop check.
2628

2729
Iter logically contains a bunch of `&'a T`s, so this is exactly what we tell
28-
the PhantomData to simulate:
30+
the `PhantomData` to simulate:
2931

3032
```rust
3133
use std::marker;
@@ -63,7 +65,7 @@ soundness. This will in turn allow people to create unsoundness using
6365
Vec's destructor.
6466

6567
In order to tell dropck that we *do* own values of type T, and therefore may
66-
drop some T's when *we* drop, we must add an extra PhantomData saying exactly
68+
drop some T's when *we* drop, we must add an extra `PhantomData` saying exactly
6769
that:
6870

6971
```rust

0 commit comments

Comments
 (0)