]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0700.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0700.md
1 The `impl Trait` return type captures lifetime parameters that do not
2 appear within the `impl Trait` itself.
3
4 Erroneous code example:
5
6 ```compile_fail,E0700
7 use std::cell::Cell;
8
9 trait Trait<'a> { }
10
11 impl<'a, 'b> Trait<'b> for Cell<&'a u32> { }
12
13 fn foo<'x, 'y>(x: Cell<&'x u32>) -> impl Trait<'y>
14 where 'x: 'y
15 {
16     x
17 }
18 ```
19
20 Here, the function `foo` returns a value of type `Cell<&'x u32>`,
21 which references the lifetime `'x`. However, the return type is
22 declared as `impl Trait<'y>` -- this indicates that `foo` returns
23 "some type that implements `Trait<'y>`", but it also indicates that
24 the return type **only captures data referencing the lifetime `'y`**.
25 In this case, though, we are referencing data with lifetime `'x`, so
26 this function is in error.
27
28 To fix this, you must reference the lifetime `'x` from the return
29 type. For example, changing the return type to `impl Trait<'y> + 'x`
30 would work:
31
32 ```
33 use std::cell::Cell;
34
35 trait Trait<'a> { }
36
37 impl<'a,'b> Trait<'b> for Cell<&'a u32> { }
38
39 fn foo<'x, 'y>(x: Cell<&'x u32>) -> impl Trait<'y> + 'x
40 where 'x: 'y
41 {
42     x
43 }
44 ```