Skip to content

Commit f78333e

Browse files
committed
Add details on PhantomData
1 parent ea79264 commit f78333e

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/librustc_typeck/diagnostics.rs

+35-10
Original file line numberDiff line numberDiff line change
@@ -2112,38 +2112,63 @@ For more information see the [opt-in builtin traits RFC](https://github.com/rust
21122112
"##,
21132113

21142114
E0392: r##"
2115-
This error indicates that a type parameter has been declared but not actually
2116-
used.
2117-
2118-
Here is an example that demonstrates the error:
2115+
This error indicates that a type or lifetime parameter has been declared
2116+
but not actually used. Here is an example that demonstrates the error:
21192117
21202118
```
21212119
enum Foo<T> {
21222120
Bar
21232121
}
21242122
```
21252123
2126-
The first way to fix this error is by removing the type parameter, as
2127-
shown below:
2124+
If the type parameter was included by mistake, this error can be fixed
2125+
by simply removing the type parameter, as shown below:
21282126
21292127
```
21302128
enum Foo {
21312129
Bar
21322130
}
21332131
```
21342132
2135-
The second method is to actually make use of the type parameter:
2133+
Alternatively, if the type parameter was intentionally inserted, it must be
2134+
used. A simple fix is shown below:
21362135
21372136
```
21382137
enum Foo<T> {
21392138
Bar(T)
21402139
}
21412140
```
21422141
2143-
See the 'Type Parameters' section of the reference for more details
2144-
on this topic:
2142+
This error may also commonly be found when working with unsafe code. For
2143+
example, when using raw pointers one may wish to specify the lifetime for
2144+
which the pointed-at data is valid. An initial attempt (below) causes this
2145+
error:
2146+
2147+
```
2148+
struct Foo<'a, T> {
2149+
x: *const T
2150+
}
2151+
```
2152+
2153+
We want to express the constraint that Foo should not outlive `'a`, because
2154+
the data pointed to by `T` is only valid for that lifetime. The problem is
2155+
that there are no actual uses of `'a`. It's possible to work around this
2156+
by adding a PhantomData type to the struct, using it to tell the compiler
2157+
to act as if the struct contained a borrowed reference `&'a T`:
2158+
2159+
```
2160+
use std::marker::PhantomData;
2161+
2162+
struct Foo<'a, T: 'a> {
2163+
x: *const T,
2164+
phantom: PhantomData<&'a T>
2165+
}
2166+
```
2167+
2168+
PhantomData can also be used to express information about unused type parameters.
2169+
You can read more about it in the API documentation:
21452170
2146-
http://doc.rust-lang.org/reference.html#type-parameters-1
2171+
https://doc.rust-lang.org/std/marker/struct.PhantomData.html
21472172
"##
21482173

21492174
}

0 commit comments

Comments
 (0)