Open
Description
While trying to make the lifetimes work in a -> impl Iterator
method, I ran into a weird error. Minimal test case:
trait Trait<'a> {}
struct Struct<'a, T>(&'a T);
impl<'a, T: Trait<'a>> Struct<'a, T> {
pub fn method<'b>(&self)
where
T: Trait<'b>,
{
}
}
As far as I can tell, this should be valid code. However, it gives this bizarre error message:
error[E0283]: type annotations needed
--> src/lib.rs:5:13
|
1 | trait Trait<'a> {}
| --------------- required by this bound in `Trait`
...
5 | impl<'a, T: Trait<'a>> Struct<'a, T> {
| ^^^^^^^^^ cannot infer type for type parameter `T`
|
= note: cannot satisfy `T: Trait<'a>`
This occurs on both stable and nightly.
More details
The error still occurs if we replace one lifetime with 'static
- either
trait Trait<'a> {}
struct Struct<T>(T);
impl<'a, T: Trait<'a>> Struct<T> {
pub fn method(&self)
where
T: Trait<'static>,
{
}
}
or
trait Trait<'a> {}
struct Struct<T>(T);
impl<T: Trait<'static>> Struct<T> {
pub fn method<'b>(&self)
where
T: Trait<'b>,
{
}
}
But it doesn't occur if either bound is removed, or if the same lifetime is used in both bounds, or if the bounds differ by a type parameter rather than a lifetime parameter.
On stable, the error message is even less helpful if the trait has an associated type:
trait Trait<'a> {
type A;
}
struct Struct<'a, T>(&'a T);
impl<'a, T: Trait<'a>> Struct<'a, T> {
pub fn method<'b>(&self)
where
T: Trait<'b>,
{
}
}
error[E0282]: type annotations needed
--> src/lib.rs:7:13
|
7 | impl<'a, T: Trait<'a>> Struct<'a, T> {
| ^^^^^^^^^ cannot infer type
But on nightly, the error is the same either way.