Skip to content

Diagnostic for iterator referencing temporary with Drop may suggest an invalid semicolon #82462

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

Closed
sapir opened this issue Feb 24, 2021 · 0 comments · Fixed by #88214
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sapir
Copy link
Contributor

sapir commented Feb 24, 2021

Given the following code on rust nightly (current nightly on playground is 2021-02-22 a15f484):

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=286e1d81ecdd9df68dc94e9b1b8ead5f

struct DroppingSlice<'a>(&'a [i32]);

impl Drop for DroppingSlice<'_> {
    fn drop(&mut self) {
        println!("hi from slice");
    }
}

impl DroppingSlice<'_> {
    fn iter(&self) -> std::slice::Iter<'_, i32> {
        self.0.iter()
    }
}

fn main() {
    let mut v = vec![1, 2, 3, 4];
    for x in DroppingSlice(&*v).iter() {
        v.push(*x);
        break;
    }
}

The current output is:

error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
  --> src/main.rs:18:9
   |
17 |     for x in DroppingSlice(&*v).iter() {
   |              ------------------      - ... and the immutable borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `DroppingSlice`
   |              |               |
   |              |               immutable borrow occurs here
   |              a temporary with access to the immutable borrow is created here ...
18 |         v.push(*x);
   |         ^^^^^^^^^^ mutable borrow occurs here
   |
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
   |
17 |     for x in DroppingSlice(&*v).iter(); {
   |                                       ^

error: aborting due to previous error

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

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

The suggestion regarding the semicolon is incorrect.

Additionally, it seems a little odd to me that the first half of the sentence beginning "a temporary with access to the immutable borrow is created here ..." is displayed below the second half, "... and the immutable borrow might be used here, when that temporary is dropped and runs the Drop code for type DroppingSlice".

@sapir sapir added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 24, 2021
@jyn514 jyn514 added A-lifetimes Area: Lifetimes / regions D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Feb 24, 2021
@bors bors closed this as completed in 4e880f8 Sep 11, 2021
flip1995 pushed a commit to flip1995/rust that referenced this issue Sep 28, 2021
…p-temps-mut, r=nagisa

rustc: use more correct span data in for loop desugaring

Fixes rust-lang#82462

Before:

      help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
         |
      LL |     for x in DroppingSlice(&*v).iter(); {
         |                                       +

After:

      help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
         |
      LL |     };
         |      +

This seems like a reasonable fix: since the desugared "expr_drop_temps_mut" contains the entire desugared loop construct, its span should contain the entire loop construct as well.
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-lifetimes Area: Lifetimes / regions D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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