Skip to content

Incorrect suggestion to add a semicolon in if let when trying to borrow a value more than once #85581

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
miDeb opened this issue May 22, 2021 · 3 comments · Fixed by #86343
Closed
Assignees
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. 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

@miDeb
Copy link

miDeb commented May 22, 2021

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ef9ffeffe1ceaef2201ebc972bfc11cd

use std::collections::BinaryHeap;

fn foo(heap: &mut BinaryHeap<i32>) {
    if let Some(value) = heap.peek_mut() {
        heap.pop();
    }
}

The current output is:

error[E0499]: cannot borrow `*heap` as mutable more than once at a time
 --> src/lib.rs:5:9
  |
4 |     if let Some(value) = heap.peek_mut() {
  |                          ---------------
  |                          |
  |                          first mutable borrow occurs here
  |                          a temporary with access to the first borrow is created here ...
5 |         heap.pop();
  |         ^^^^ second mutable borrow occurs here
6 |     }
7 | }
  | - ... and the first borrow might be used here, when that temporary is dropped and runs the destructor for type `Option<PeekMut<'_, i32>>`
  |
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
  |
6 |     };
  |      ^

Ideally the output should look like:
Without the "help" part, which is what it looks like when I actually add the semicolon (not sure if the rust compiler could also accept this code without an error)

@miDeb miDeb 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 May 22, 2021
@miDeb miDeb changed the title Incorrect suggestion to add a semicolon when Incorrect suggestion to add a semicolon in if let when trying to borrow a value more than once May 22, 2021
@miDeb
Copy link
Author

miDeb commented May 22, 2021

This also happens with a match, so feel free to change the title to something more appropriate.

@ForgottenGensym
Copy link

Just to clarify, this only happens when the first borrow is in the condition of the if let, and the second is in the body

@inquisitivecrystal
Copy link
Contributor

@rustbot label +A-borrow-checker +D-incorrect-suggestion

@rustbot rustbot added the A-borrow-checker Area: The borrow checker label Jun 5, 2021
@JohnTitor JohnTitor added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Jun 15, 2021
@JohnTitor JohnTitor self-assigned this Jun 15, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jun 16, 2021
Do not emit invalid suggestions on multiple mutable borrow errors

Fixes rust-lang#85581
@bors bors closed this as completed in afe70ee Jun 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. 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.

5 participants