File tree Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Original file line number Diff line number Diff line change @@ -366,6 +366,7 @@ E0644: include_str!("./error_codes/E0644.md"),
366366E0646 : include_str!( "./error_codes/E0646.md" ) ,
367367E0647 : include_str!( "./error_codes/E0647.md" ) ,
368368E0648 : include_str!( "./error_codes/E0648.md" ) ,
369+ E0657 : include_str!( "./error_codes/E0657.md" ) ,
369370E0658 : include_str!( "./error_codes/E0658.md" ) ,
370371E0659 : include_str!( "./error_codes/E0659.md" ) ,
371372E0660 : include_str!( "./error_codes/E0660.md" ) ,
@@ -597,7 +598,6 @@ E0751: include_str!("./error_codes/E0751.md"),
597598 // used in argument position
598599 E0640 , // infer outlives requirements
599600// E0645, // trait aliases not finished
600- E0657 , // `impl Trait` can only capture lifetimes bound at the fn level
601601 E0667 , // `impl Trait` in projections
602602 E0687 , // in-band lifetimes cannot be used in `fn`/`Fn` syntax
603603 E0688 , // in-band lifetimes cannot be mixed with explicit lifetime binders
Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change @@ -12,3 +12,4 @@ LL | -> Box<for<'a> Id<impl Lt<'a>>>
1212
1313error: aborting due to 2 previous errors
1414
15+ For more information about this error, try `rustc --explain E0657`.
You can’t perform that action at this time.
0 commit comments