]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0623.md
Rollup merge of #102215 - alexcrichton:wasm-link-whole-archive, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0623.md
1 A lifetime didn't match what was expected.
2
3 Erroneous code example:
4
5 ```compile_fail,E0623
6 struct Foo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>)
7 where
8     T: Convert<'a, 'b>;
9
10 trait Convert<'a, 'b>: Sized {
11     fn cast(&'a self) -> &'b Self;
12 }
13 impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
14     fn cast(&'long self) -> &'short T {
15         self
16     }
17 }
18 // error
19 fn badboi<'in_, 'out, T>(
20     x: Foo<'in_, 'out, T>,
21     sadness: &'in_ T
22 ) -> &'out T {
23     sadness.cast()
24 }
25 ```
26
27 In this example, we tried to set a value with an incompatible lifetime to
28 another one (`'in_` is unrelated to `'out`). We can solve this issue in
29 two different ways:
30
31 Either we make `'in_` live at least as long as `'out`:
32
33 ```
34 struct Foo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>)
35 where
36     T: Convert<'a, 'b>;
37
38 trait Convert<'a, 'b>: Sized {
39     fn cast(&'a self) -> &'b Self;
40 }
41 impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
42     fn cast(&'long self) -> &'short T {
43         self
44     }
45 }
46 fn badboi<'in_: 'out, 'out, T>(
47     x: Foo<'in_, 'out, T>,
48     sadness: &'in_ T
49 ) -> &'out T {
50     sadness.cast()
51 }
52 ```
53
54 Or we use only one lifetime:
55
56 ```
57 struct Foo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>)
58 where
59     T: Convert<'a, 'b>;
60
61 trait Convert<'a, 'b>: Sized {
62     fn cast(&'a self) -> &'b Self;
63 }
64 impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
65     fn cast(&'long self) -> &'short T {
66         self
67     }
68 }
69 fn badboi<'out, T>(x: Foo<'out, 'out, T>, sadness: &'out T) -> &'out T {
70     sadness.cast()
71 }
72 ```