Skip to content

Inconsistency between &HashMap and &BTreeMap implementation of IntoIterator #74034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SkiFire13 opened this issue Jul 4, 2020 · 1 comment · Fixed by #75203
Closed

Inconsistency between &HashMap and &BTreeMap implementation of IntoIterator #74034

SkiFire13 opened this issue Jul 4, 2020 · 1 comment · Fixed by #75203
Labels
A-collections Area: `std::collections` A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@SkiFire13
Copy link
Contributor

The implementation of IntoIterator for &'a BTreeMap<K, V> and &'a HashMap<K, V, S> differ in the lifetime bounds of K and V

impl<'a, K: 'a, V: 'a> IntoIterator for &'a BTreeMap<K, V> { \* ... *\ }
impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S> { \* ... *\ }

I believe those additional bounds should be unnecessary since they're implied by the fact that exists a reference of lifetime 'a to the whole map. However, quite interestingly, they appear to change how the compiler resolves trait bounds in some cases. For example in the following code:

trait Map
where
    for<'a> &'a Self: IntoIterator<Item = (&'a Self::Key, &'a Self::Value)>,
{
    type Key;
    type Value;
}

// Ok, compiles
impl<K, V> Map for HashMap<K, V> {
    type Key = K;
    type Value = V;
}

// Not ok, doesn't compiles, with rather cryptic error
impl<K, V> Map for BTreeMap<K, V> {
    type Key = K;
    type Value = V;
}

I would expect it to compile, or at least throw the same error for both the BTreeMap and HashMap implementation.
However it throws errors only for the BTreeMap implementation.

Also note that error E0311 isn't even present in the Rust Compiler Error Index.

error[E0311]: the parameter type `K` may not live long enough
  --> src/lib.rs:18:12
   |
18 | impl<K, V> Map for BTreeMap<K, V> {
   |            ^^^
   |
   = help: consider adding an explicit lifetime bound for `K`
   = note: the parameter type `K` must be valid for any other region...
note: ...so that the type `K` will meet its required lifetime bounds
  --> src/lib.rs:18:12
   |
18 | impl<K, V> Map for BTreeMap<K, V> {
   |            ^^^

error[E0311]: the parameter type `V` may not live long enough
  --> src/lib.rs:18:12
   |
18 | impl<K, V> Map for BTreeMap<K, V> {
   |            ^^^
   |
   = help: consider adding an explicit lifetime bound for `V`
   = note: the parameter type `V` must be valid for any other region...
note: ...so that the type `V` will meet its required lifetime bounds
  --> src/lib.rs:18:12
   |
18 | impl<K, V> Map for BTreeMap<K, V> {
   |            ^^^

error: aborting due to 2 previous errors

Playground link

@SkiFire13 SkiFire13 added the C-bug Category: This is a bug. label Jul 4, 2020
@jonas-schievink jonas-schievink added A-collections Area: `std::collections` A-lifetimes Area: Lifetimes / regions T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jul 4, 2020
@SkiFire13
Copy link
Contributor Author

Found a more minimal test case:

fn foo<'a, K, V, I: 'a + IntoIterator<Item = (&'a K, &'a V)>>(i: I) {}

fn bar() {
    let map = HashMap::<u32, u32>::new();
    foo(&map); // Ok
    
    let map = BTreeMap::<u32, u32>::new();
    foo(&map); // Error
}

The error it gives is pretty much the same:

error[E0309]: the parameter type `K` may not live long enough
 --> src/lib.rs:3:39
  |
3 | fn foo<'a, K, V, I: 'a + IntoIterator<Item = (&'a K, &'a V)>>(i: I) {}
  |            -                          ^^^^^^^^^^^^^^^^^^^^^
  |            |
  |            help: consider adding an explicit lifetime bound...: `K: 'a`
  |
note: ...so that the reference type `&'a K` does not outlive the data it points at
 --> src/lib.rs:3:39
  |
3 | fn foo<'a, K, V, I: 'a + IntoIterator<Item = (&'a K, &'a V)>>(i: I) {}
  |                                       ^^^^^^^^^^^^^^^^^^^^^

error[E0309]: the parameter type `V` may not live long enough
 --> src/lib.rs:3:39
  |
3 | fn foo<'a, K, V, I: 'a + IntoIterator<Item = (&'a K, &'a V)>>(i: I) {}
  |               -                       ^^^^^^^^^^^^^^^^^^^^^
  |               |
  |               help: consider adding an explicit lifetime bound...: `V: 'a`
  |
note: ...so that the reference type `&'a V` does not outlive the data it points at
 --> src/lib.rs:3:39
  |
3 | fn foo<'a, K, V, I: 'a + IntoIterator<Item = (&'a K, &'a V)>>(i: I) {}
  |                                       ^^^^^^^^^^^^^^^^^^^^^

Playground link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collections` A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants