Improve error message for opaque captures
This commit is contained in:
parent
74acabe9b0
commit
ffd30e0a69
25 changed files with 150 additions and 116 deletions
|
@ -1,57 +1,26 @@
|
|||
A lifetime bound on a trait implementation was captured at an incorrect place.
|
||||
An `impl Trait` captured a higher-ranked lifetime, which is not supported.
|
||||
|
||||
Currently, `impl Trait` types are only allowed to capture lifetimes from
|
||||
their parent items, and not from any `for<'a>` binders in scope.
|
||||
|
||||
Erroneous code example:
|
||||
|
||||
```compile_fail,E0657
|
||||
trait Id<T> {}
|
||||
trait Lt<'a> {}
|
||||
trait BorrowInto<'a> {
|
||||
type Target;
|
||||
|
||||
impl<'a> Lt<'a> for () {}
|
||||
impl<T> Id<T> for T {}
|
||||
|
||||
fn free_fn_capture_hrtb_in_impl_trait()
|
||||
-> Box<for<'a> Id<impl Lt<'a>>> // error!
|
||||
{
|
||||
Box::new(())
|
||||
fn borrow_into(&'a self) -> Self::Target;
|
||||
}
|
||||
|
||||
struct Foo;
|
||||
impl Foo {
|
||||
fn impl_fn_capture_hrtb_in_impl_trait()
|
||||
-> Box<for<'a> Id<impl Lt<'a>>> // error!
|
||||
{
|
||||
Box::new(())
|
||||
impl<'a> BorrowInto<'a> for () {
|
||||
type Target = &'a ();
|
||||
|
||||
fn borrow_into(&'a self) -> Self::Target {
|
||||
self
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Here, you have used the inappropriate lifetime in the `impl Trait`,
|
||||
The `impl Trait` can only capture lifetimes bound at the fn or impl
|
||||
level.
|
||||
|
||||
To fix this we have to define the lifetime at the function or impl
|
||||
level and use that lifetime in the `impl Trait`. For example you can
|
||||
define the lifetime at the function:
|
||||
|
||||
```
|
||||
trait Id<T> {}
|
||||
trait Lt<'a> {}
|
||||
|
||||
impl<'a> Lt<'a> for () {}
|
||||
impl<T> Id<T> for T {}
|
||||
|
||||
fn free_fn_capture_hrtb_in_impl_trait<'b>()
|
||||
-> Box<for<'a> Id<impl Lt<'b>>> // ok!
|
||||
{
|
||||
Box::new(())
|
||||
}
|
||||
|
||||
struct Foo;
|
||||
impl Foo {
|
||||
fn impl_fn_capture_hrtb_in_impl_trait<'b>()
|
||||
-> Box<for<'a> Id<impl Lt<'b>>> // ok!
|
||||
{
|
||||
Box::new(())
|
||||
}
|
||||
fn opaque() -> impl for<'a> BorrowInto<'a, Target = impl Sized + 'a> {
|
||||
()
|
||||
}
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue