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

Misleading diagnostics for .iter() #68445

Closed
avandesa opened this issue Jan 22, 2020 · 1 comment · Fixed by #116990
Closed

Misleading diagnostics for .iter() #68445

avandesa opened this issue Jan 22, 2020 · 1 comment · Fixed by #116990
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

@avandesa
Copy link
Contributor

The following code:

fn main() {
    let iter = vec![1, 2, 3].iter().map(|x| x * x);

    for x in iter {
        println!("{}", x);
    }
}

Produces the following error:

error[E0716]: temporary value dropped while borrowed
 --> foo.rs:2:16
  |
2 |     let iter = vec![1, 2, 3].iter().map(|x| x * x);
  |                ^^^^^^^^^^^^^                      - temporary value is freed at the end of this statement
  |                |
  |                creates a temporary which is freed while still in use
3 | 
4 |     for x in iter {
  |              ---- borrow later used here
  |
  = note: consider using a `let` binding to create a longer lived value
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0716`.

Though the suggestion to make a let binding works, and is probably useful for other situations, it's not as elegant here:

fn main() {
    let vec = complicated_get_vec();
    let iter = vec.iter().map(|x| x * x);

    for x in iter {
        println!("{}", x);
    }
}

The better solution to the compile error is to replace vec![].iter() ... with vec![].into_iter(), and the compiler error message may be updated to reflect this.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 22, 2020
@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. labels Mar 15, 2023
@estebank
Copy link
Contributor

Current output:

error[E0716]: temporary value dropped while borrowed
 --> src/main.rs:2:16
  |
2 |     let iter = vec![1, 2, 3].iter().map(|x| x * x);
  |                ^^^^^^^^^^^^^                      - temporary value is freed at the end of this statement
  |                |
  |                creates a temporary value which is freed while still in use
3 |
4 |     for x in iter {
  |              ---- borrow later used here
  |
  = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using a `let` binding to create a longer lived value
  |
2 ~     let binding = vec![1, 2, 3];
3 ~     let iter = binding.iter().map(|x| x * x);
  |

We still don't look if we could suggest into_iter() instead.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 21, 2023
Mention `into_iter` on borrow errors suggestions when appropriate

If we encounter a borrow error on `vec![1, 2, 3].iter()`, suggest `into_iter`.

Fix rust-lang#68445.
@bors bors closed this as completed in 88bccf4 Oct 21, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 21, 2023
Rollup merge of rust-lang#116990 - estebank:issue-68445, r=cjgillot

Mention `into_iter` on borrow errors suggestions when appropriate

If we encounter a borrow error on `vec![1, 2, 3].iter()`, suggest `into_iter`.

Fix rust-lang#68445.
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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

Successfully merging a pull request may close this issue.

2 participants