]> git.proxmox.com Git - rustc.git/blame - src/librustc_error_codes/error_codes/E0657.md
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0657.md
CommitLineData
ba9703b0
XL
1A lifetime bound on a trait implementation was captured at an incorrect place.
2
3Erroneous code example:
4
5```compile_fail,E0657
6trait Id<T> {}
7trait Lt<'a> {}
8
9impl<'a> Lt<'a> for () {}
10impl<T> Id<T> for T {}
11
12fn free_fn_capture_hrtb_in_impl_trait()
13 -> Box<for<'a> Id<impl Lt<'a>>> // error!
14{
15 Box::new(())
16}
17
18struct Foo;
19impl 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
28Here, you have used the inappropriate lifetime in the `impl Trait`,
29The `impl Trait` can only capture lifetimes bound at the fn or impl
30level.
31
32To fix this we have to define the lifetime at the function or impl
33level and use that lifetime in the `impl Trait`. For example you can
34define the lifetime at the function:
35
36```
37trait Id<T> {}
38trait Lt<'a> {}
39
40impl<'a> Lt<'a> for () {}
41impl<T> Id<T> for T {}
42
43fn free_fn_capture_hrtb_in_impl_trait<'b>()
44 -> Box<for<'a> Id<impl Lt<'b>>> // ok!
45{
46 Box::new(())
47}
48
49struct Foo;
50impl 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```