Skip to content
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

Strange .cloned() error message mentioning iterators #74121

Open
enterprisey opened this issue Jul 7, 2020 · 4 comments
Open

Strange .cloned() error message mentioning iterators #74121

enterprisey opened this issue Jul 7, 2020 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@enterprisey
Copy link
Contributor

enterprisey commented Jul 7, 2020

I'm unsure why the error message for this code mentions iterators; there aren't any, I thought.

fn main() {
    Some(1).cloned();
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `cloned` found for enum `std::option::Option<{integer}>` in the current scope
   --> src/main.rs:2:13
    |
2   |   Some(1).cloned();
    |           ^^^^^^ method not found in `std::option::Option<{integer}>`
    |
    = note: the method `cloned` exists but the following trait bounds were not satisfied:
            `std::option::Option<{integer}>: std::iter::Iterator`
            which is required by `&mut std::option::Option<{integer}>: std::iter::Iterator`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

@estebank
Copy link
Contributor

estebank commented Jul 7, 2020

A method fn cloned(&self) exists in Iterator. The note is telling you that method that does exist is not callable on Option<_> because Option<_> doesn't implement Iterator. We can probably reword this to make that clearer.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 7, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 7, 2020
@arora-aman
Copy link
Member

arora-aman commented Jul 8, 2020

Option also has a cloned method. Based on the current usecase (not using an iterator) maybe something along the lines "cloned() defined for &i32 and not i32" along with the iterator lint might be better diagnostically.

@jazeved0
Copy link

The solution for the Option case is to use something like:

fn main(){
  Some(1).as_ref().cloned();
}

I encountered the same error, and while I understand the compiler error/message, it sticks out in a toolchain that normally has very helpful compiler error messages. I agree that the help note could be clearer for the case with Option (where the intention likely isn't to use Iterator at all)

@ghubertpalo
Copy link

I have got the same error using this test case:

struct Pika {
    a: Option<u32>,
}

impl Pika {
    pub fn get_a(&self) -> Option<u32> {
        self.a.cloned()
    }
}

fn main() {
    let my_pika = Pika { a: Some(12) };
    println!("{:?}", my_pika.get_a());
}

link to playground

This grants the following error message:

Compiling playground v0.0.1 (/playground)
error[[E0599]](https://doc.rust-lang.org/stable/error-index.html#E0599): `Option<u32>` is not an iterator
   --> src/main.rs:7:16
    |
7   |           self.a.cloned()
    |                  ^^^^^^ `Option<u32>` is not an iterator
    |
    = note: the following trait bounds were not satisfied:
            `Option<u32>: Iterator`
            which is required by `&mut Option<u32>: Iterator`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` due to previous error

In a more general way, this error message raises as soon as the cloned() method is called on a borrow of an Option:

let a = Some(12);
let b = &a;
let c = b.cloned(); // Iterator error

@estebank estebank added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-confusing Diagnostics: Confusing error or lint that should be reworked. labels May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants