Open
Description
As far as I can tell the following code shouldn't cause a lifetime error but it does. If Iter::Item
is changed to &'a ()
instead of PhantomData<&'a ()>
the error goes away.
use core::marker::PhantomData;
fn g<A: Iterator, B: Iterator>(a: A, b: B) -> A
where
A::Item: PartialEq<B::Item>,
{
todo!()
}
pub struct Iter<'a>(&'a ());
impl<'a> Iterator for Iter<'a> {
type Item = PhantomData<&'a ()>;
fn next(&mut self) -> Option<Self::Item> {
todo!()
}
}
pub fn f<'a, B: AsRef<()>>(a: &'a (), b: B) -> &'a () {
g(Iter(a), Iter(b.as_ref())).0
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0309]: the parameter type `B` may not live long enough
--> src/lib.rs:20:21
|
19 | pub fn f<'a, B: AsRef<()>>(a: &'a (), b: B) -> &'a () {
| -- help: consider adding an explicit lifetime bound...: `B: 'a +`
20 | g(Iter(a), Iter(b.as_ref())).0
| ^ ...so that the type `B` is not borrowed for too long
error[E0309]: the parameter type `B` may not live long enough
--> src/lib.rs:20:23
|
19 | pub fn f<'a, B: AsRef<()>>(a: &'a (), b: B) -> &'a () {
| -- help: consider adding an explicit lifetime bound...: `B: 'a +`
20 | g(Iter(a), Iter(b.as_ref())).0
| ^^^^^^ ...so that the reference type `&B` does not outlive the data it points at
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0309`.
error: could not compile `playground`
To learn more, run the command again with --verbose.