Open
Description
This code should work:
use std::collections::{BTreeMap, HashMap};
fn foo<'a, K, V, I: 'a + IntoIterator<Item = (&'a K, &'a V)>>(i: I) {}
fn main() {
let map = HashMap::<u32, u32>::new();
foo(&map);
let map = BTreeMap::<u32, u32>::new();
foo(&map);
}
Because, even though we don't assign 'a
lifetime to K
and V
, since we have &'a K
&'a V
in the signature, that lifetime should be implicitly assigned to them instead. Currently that does not compile and we get this error below:
Compiling playground v0.0.1 (/playground)
error[E0309]: the parameter type `K` may not live long enough
--> src/main.rs:3:39
|
3 | fn foo<'a, K, V, I: 'a + IntoIterator<Item = (&'a K, &'a V)>>(i: I) {}
| - ^^^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'a K` does not outlive the data it points at
| |
| help: consider adding an explicit lifetime bound...: `K: 'a`
error[E0309]: the parameter type `V` may not live long enough
--> src/main.rs:3:39
|
3 | fn foo<'a, K, V, I: 'a + IntoIterator<Item = (&'a K, &'a V)>>(i: I) {}
| - ^^^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'a V` does not outlive the data it points at
| |
| help: consider adding an explicit lifetime bound...: `V: 'a`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0309`.
error: could not compile `playground`.
Despite the example, this doesn't seem like a problem for IntoIterator
only. It looks like there is a fundamental problem instead. This was separated from the example in #74034 and thanks @nbdd0121 for pointing that out in #75203.
Meta
rustc --version --verbose
:
rustc 1.45.0 (5c1f21c3b 2020-07-13)
binary: rustc
commit-hash: 5c1f21c3b82297671ad3ae1e8c942d2ca92e84f2
commit-date: 2020-07-13
host: x86_64-unknown-linux-gnu
release: 1.45.0
LLVM version: 10.0