]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0657.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0657.md
1 A lifetime bound on a trait implementation was captured at an incorrect place.
2
3 Erroneous code example:
4
5 ```compile_fail,E0657
6 trait Id<T> {}
7 trait Lt<'a> {}
8
9 impl<'a> Lt<'a> for () {}
10 impl<T> Id<T> for T {}
11
12 fn free_fn_capture_hrtb_in_impl_trait()
13     -> Box<for<'a> Id<impl Lt<'a>>> // error!
14 {
15     Box::new(())
16 }
17
18 struct Foo;
19 impl Foo {
20     fn impl_fn_capture_hrtb_in_impl_trait()
21         -> Box<for<'a> Id<impl Lt<'a>>> // error!
22     {
23         Box::new(())
24     }
25 }
26 ```
27
28 Here, you have used the inappropriate lifetime in the `impl Trait`,
29 The `impl Trait` can only capture lifetimes bound at the fn or impl
30 level.
31
32 To fix this we have to define the lifetime at the function or impl
33 level and use that lifetime in the `impl Trait`. For example you can
34 define the lifetime at the function:
35
36 ```
37 trait Id<T> {}
38 trait Lt<'a> {}
39
40 impl<'a> Lt<'a> for () {}
41 impl<T> Id<T> for T {}
42
43 fn free_fn_capture_hrtb_in_impl_trait<'b>()
44     -> Box<for<'a> Id<impl Lt<'b>>> // ok!
45 {
46     Box::new(())
47 }
48
49 struct Foo;
50 impl Foo {
51     fn impl_fn_capture_hrtb_in_impl_trait<'b>()
52         -> Box<for<'a> Id<impl Lt<'b>>> // ok!
53     {
54         Box::new(())
55     }
56 }
57 ```