Impl Trait forces unnecessary lifetime bounds #80083
Labels
A-impl-trait
Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
A-lifetimes
Area: Lifetimes / regions
A-variance
Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)
It seems like impl Trait forces its own lifetime bound onto anything that it touches, even if that doesn't make sense.
Here I create an iterator like type that takes a function that produces T values. The implementation of the iterator doesn't really matter here. If I now create a function constructing that type, returning the actual type lets the borrow checker correctly understand that T doesn't require any lifetimes. However if I return impl Iterator instead, the compiler is apparently seeing that T is part of the impl Trait and therefore forces me to apply the same lifetime as the iterator itself to the elements T, even if that makes no sense. I'm guessing impl Trait is not properly respecting variance.
Playground
@HeroicKatora mentioned that a similar problem happens with trait objects as well: Playground
The text was updated successfully, but these errors were encountered: