Repository navigation
Error for for _ in HashMap::new().iter().cloned() { } doesn't provide enough context #33941
Copy link
Copy link
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: DiagnosticsWorking group: Diagnostics
Description
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
Reacted by Matt, boluoyu, Ken Aoki, Andrew Sologor and Jonas Otto
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: DiagnosticsWorking group: Diagnostics
Activity
DoumanAsh commentedon May 29, 2016
I also think the error should be improved.
Especially because you bluntly show user API that cannot be compiled.
I'm ofc talking about HashMap's iter docs
estebank commentedon Sep 30, 2017
Current output:
There are two problems here that can be tackled independently:
The duplicated E0271 type mismatch errors can be fixed by modifyingDone.report_projection_errorto not emit the diagnostic immediately, but rather to modify InferCtxt to keep a hashset of(expected_type, found_type, Span)so that the same error isn't emitted multiple times. All the duplicated errors (from the second onwards) seem to be caused by the implicit handling of iterators. Bonus points for collecting all theobligations and emitting them all together in the same diagnostic, instead of just losing the latter ones (requires either finding a method that gets called always at the end, or adding one).The error itself is not very illuminating and should have extra information provided in order to be helpful. For the case in particular of
hash_map::Iter, it would be great if we could suggest the following, as that would actually do what the user expects (other than a missing type definition for theHashMap, on real code theHashMapwould have been created before so that it would have had a chance to be populated), but I don't know how we could accomplish that in a clean way:In general, it would probably be nice to have something along the lines of the following output on cases where type errors are propagated down a method call chain, but it's non-trivial to do and would still add little help here, so it should be out of scope of this issue:
deduplicate projection error (E0271) messages
Rollup merge of rust-lang#45952 - zackmdavis:singular_projection, r=e…
[-]`for _ in HashMap::new().iter().cloned() { }` produces 9 (near-)duplicate errors[/-][+]Error for `for _ in HashMap::new().iter().cloned() { }` doesn't provide enough context[/+]estebank commentedon Jan 22, 2020
Triage: no change.
yihuang commentedon Jun 14, 2020
I wonder is there handy utils to convert
(&K, &V)into(K, V)without writing a closure?Print type of every call in a method call chain
Rollup merge of rust-lang#105674 - estebank:iterator-chains, r=oli-obk
Rollup merge of rust-lang#105674 - estebank:iterator-chains, r=oli-obk
Rollup merge of rust-lang#105674 - estebank:iterator-chains, r=oli-obk
Rollup merge of rust-lang#105674 - estebank:iterator-chains, r=oli-obk