Closed
Description
Rustc does not seem to reason correctly about lifetimes of borrowed pointers in the environment of returned closures.
Please look at the following example, I'm not sure if it should work, but it would be nice if it did:
Consider this code that I want to insert into std::hashmap
/// Visit the values representing the intersection
fn intersection_iter<'a>(&'a self, other: &'a HashSet<T>)
-> FilterIterator<'a, &'a T, HashSetIterator<'a,T>> {
self.iter().filter(|elt| other.contains(*elt))
}
Rustc can not make sense of the lifetimes:
$ rustc --test std.rs
hashmap.rs:796:26: 796:54 error: cannot infer an appropriate lifetime due to conflicting requirements
hashmap.rs:796 self.iter().filter(|elt| other.contains(*elt))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
hashmap.rs:795:60: 797:5 note: first, the lifetime cannot outlive the block at 795:60...
hashmap.rs:795 -> FilterIterator<'a, &'a T, HashSetIterator<'a,T>> {
hashmap.rs:796 self.iter().filter(|elt| other.contains(*elt))
hashmap.rs:797 }
hashmap.rs:796:26: 796:54 note: ...due to the following expression
hashmap.rs:796 self.iter().filter(|elt| other.contains(*elt))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
hashmap.rs:795:60: 797:5 note: but, the lifetime must be valid for the lifetime &'a as defined on the block at 795:60...
hashmap.rs:795 -> FilterIterator<'a, &'a T, HashSetIterator<'a,T>> {
hashmap.rs:796 self.iter().filter(|elt| other.contains(*elt))
hashmap.rs:797 }
hashmap.rs:796:8: 797:5 note: ...due to the following expression
hashmap.rs:796 self.iter().filter(|elt| other.contains(*elt))
hashmap.rs:797 }
error: aborting due to previous error