-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Confusing compiler error when iterating over Option<IntoIterator> #57872
Copy link
Copy link
Open
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-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.P-lowLow priorityLow priorityT-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.
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-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.P-lowLow priorityLow priorityT-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.
Type
Fields
Give feedbackNo fields configured for issues without a type.
I recently wrote a type that implemented IntoIterator with a tuple as the Item type. I then tried to test this by using this iterator in a for loop. I got the following error:
This left me confused, because I had assumed that foo's type was MyIter. Why was the iterator returning itself, instead of the item type I had specified? I began to suspect my implementation of IntoIterator before I realized that foo was actually
Option<MyIter>. As it turns out, Option also implements IntoIterator (#27996), so the compiler thought I was trying to iterate over the Option instead of the enclosed type!It'd be nice if there were a note associated with the compiler error in cases where the iterator is type
Option<T : IntoIterator>. Something like this, maybe?Simple reproduction of the compiler error:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=4432a78517e24ea649a31c7246b34edd