Skip to content

Commit ccbc774

Browse files
author
Lukas Markeffsky
committed
add sanity test for inferred lifetimes in PhantomData
1 parent 92720a6 commit ccbc774

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Sanity check that `PhantomData<&'a T>` infers `T: 'a`.
2+
3+
use std::marker::PhantomData;
4+
use std::ptr;
5+
6+
struct InferredBound<'a, T> {
7+
ptr: *const T,
8+
phantom: PhantomData<&'a T>,
9+
}
10+
11+
struct RedundantBound<'a, T: 'a> {
12+
ptr: *const T,
13+
phantom: PhantomData<&'a T>,
14+
}
15+
16+
struct ManualBound<'a, T: 'a> {
17+
ptr: *const T,
18+
phantom: PhantomData<&'a ()>,
19+
}
20+
21+
struct NoBound<'a, T> {
22+
ptr: *const T,
23+
phantom: PhantomData<&'a ()>,
24+
}
25+
26+
fn inferred_bound<'a>(_: &'a u8) -> InferredBound<'static, &'a u8> {
27+
InferredBound {
28+
ptr: ptr::null(),
29+
phantom: PhantomData,
30+
}
31+
}
32+
33+
fn redundant_bound<'a>(_: &'a u8) -> RedundantBound<'static, &'a u8> {
34+
RedundantBound {
35+
ptr: ptr::null(),
36+
phantom: PhantomData,
37+
}
38+
}
39+
40+
fn manual_bound<'a>(_: &'a u8) -> ManualBound<'static, &'a u8> {
41+
ManualBound {
42+
ptr: ptr::null(),
43+
phantom: PhantomData,
44+
}
45+
}
46+
47+
fn no_bound<'a>(_: &'a u8) -> NoBound<'static, &'a u8> {
48+
NoBound {
49+
ptr: ptr::null(),
50+
phantom: PhantomData,
51+
}
52+
}
53+
54+
fn main() {
55+
let local = 0;
56+
inferred_bound(&local); //~ ERROR: `local` does not live long enough [E0597]
57+
redundant_bound(&local); //~ ERROR: `local` does not live long enough [E0597]
58+
manual_bound(&local); //~ ERROR: `local` does not live long enough [E0597]
59+
no_bound(&local); // OK
60+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0597]: `local` does not live long enough
2+
--> $DIR/phatom-data.rs:56:20
3+
|
4+
LL | inferred_bound(&local);
5+
| ---------------^^^^^^-
6+
| | |
7+
| | borrowed value does not live long enough
8+
| argument requires that `local` is borrowed for `'static`
9+
...
10+
LL | }
11+
| - `local` dropped here while still borrowed
12+
13+
error[E0597]: `local` does not live long enough
14+
--> $DIR/phatom-data.rs:57:21
15+
|
16+
LL | redundant_bound(&local);
17+
| ----------------^^^^^^-
18+
| | |
19+
| | borrowed value does not live long enough
20+
| argument requires that `local` is borrowed for `'static`
21+
...
22+
LL | }
23+
| - `local` dropped here while still borrowed
24+
25+
error[E0597]: `local` does not live long enough
26+
--> $DIR/phatom-data.rs:58:18
27+
|
28+
LL | manual_bound(&local);
29+
| -------------^^^^^^-
30+
| | |
31+
| | borrowed value does not live long enough
32+
| argument requires that `local` is borrowed for `'static`
33+
LL | no_bound(&local); // OK
34+
LL | }
35+
| - `local` dropped here while still borrowed
36+
37+
error: aborting due to 3 previous errors
38+
39+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)