Skip to content

Error for for _ in HashMap::new().iter().cloned() { } doesn't provide enough context #33941

Closed
@niconii

Description

@niconii

Playpen

use std::collections::HashMap;

fn main() {
    for _ in HashMap::new().iter().cloned() { }
}

.cloned() failing is normal since the iterator's items are (&K, &V) and .cloned() expects &T, but the number of errors is strange.

error: type mismatch resolving `<std::collections::hash_map::Iter<'_, _, _> as std::iter::Iterator>::Item == &_`:
 expected tuple,
    found &-ptr [--explain E0271]
 --> <anon>:4:36
4 |>     for _ in HashMap::new().iter().cloned() { }
  |>                                    ^^^^^^

error: type mismatch resolving `<std::collections::hash_map::Iter<'_, _, _> as std::iter::Iterator>::Item == &_`:
 expected tuple,
    found &-ptr [--explain E0271]
 --> <anon>:4:5
4 |>     for _ in HashMap::new().iter().cloned() { }
  |>     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Cloned<std::collections::hash_map::Iter<'_, _, _>>`

error: type mismatch resolving `<std::collections::hash_map::Iter<'_, _, _> as std::iter::Iterator>::Item == &_`:
 expected tuple,
    found &-ptr [--explain E0271]
 --> <anon>:4:5
4 |>     for _ in HashMap::new().iter().cloned() { }
  |>     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Cloned<std::collections::hash_map::Iter<'_, _, _>>`

error: type mismatch resolving `<std::collections::hash_map::Iter<'_, _, _> as std::iter::Iterator>::Item == &_`:
 expected tuple,
    found &-ptr [--explain E0271]
 --> <anon>:4:5
4 |>     for _ in HashMap::new().iter().cloned() { }
  |>     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Cloned<std::collections::hash_map::Iter<'_, _, _>>`
note: required by `std::iter::IntoIterator::into_iter`

error: type mismatch resolving `<std::collections::hash_map::Iter<'_, _, _> as std::iter::Iterator>::Item == &_`:
 expected tuple,
    found &-ptr [--explain E0271]
 --> <anon>:4:5
4 |>     for _ in HashMap::new().iter().cloned() { }
  |>     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Cloned<std::collections::hash_map::Iter<'_, _, _>>`
note: required by `std::iter::IntoIterator::into_iter`

error: type mismatch resolving `<std::collections::hash_map::Iter<'_, _, _> as std::iter::Iterator>::Item == &_`:
 expected tuple,
    found &-ptr [--explain E0271]
 --> <anon>:4:5
4 |>     for _ in HashMap::new().iter().cloned() { }
  |>     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Cloned<std::collections::hash_map::Iter<'_, _, _>>`
note: required because of the requirements on the impl of `std::iter::IntoIterator` for `std::iter::Cloned<std::collections::hash_map::Iter<'_, _, _>>`

error: type mismatch resolving `<std::collections::hash_map::Iter<'_, _, _> as std::iter::Iterator>::Item == &_`:
 expected tuple,
    found &-ptr [--explain E0271]
 --> <anon>:4:5
4 |>     for _ in HashMap::new().iter().cloned() { }
  |>     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Cloned<std::collections::hash_map::Iter<'_, _, _>>`

error: type mismatch resolving `<std::collections::hash_map::Iter<'_, _, _> as std::iter::Iterator>::Item == &_`:
 expected tuple,
    found &-ptr [--explain E0271]
 --> <anon>:4:5
4 |>     for _ in HashMap::new().iter().cloned() { }
  |>     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Cloned<std::collections::hash_map::Iter<'_, _, _>>`
note: required by `std::iter::Iterator::next`

error: type mismatch resolving `<std::collections::hash_map::Iter<'_, _, _> as std::iter::Iterator>::Item == &_`:
 expected tuple,
    found &-ptr [--explain E0271]
 --> <anon>:4:5
4 |>     for _ in HashMap::new().iter().cloned() { }
  |>     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Cloned<std::collections::hash_map::Iter<'_, _, _>>`

error: aborting due to 9 previous errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions