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

E0499 error shouldn't suggest adding a semicolon when there already is one #133941

Closed
josephcsible opened this issue Dec 5, 2024 · 1 comment · Fixed by #136402
Closed

E0499 error shouldn't suggest adding a semicolon when there already is one #133941

josephcsible opened this issue Dec 5, 2024 · 1 comment · Fixed by #136402
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@josephcsible
Copy link

josephcsible commented Dec 5, 2024

Code

use std::marker::PhantomData;

struct Bar<'a>(PhantomData<&'a mut i32>);

impl<'a> Drop for Bar<'a> {
    fn drop(&mut self) {}
}

struct Foo();

impl Foo {
    fn f(&mut self) -> Option<Bar<'_>> {
        None
    }
    
    fn g(&mut self) {}
}

fn main() {
    let mut foo = Foo();
    while let Some(_) = foo.f() {
        foo.g();
    };
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0499]: cannot borrow `foo` as mutable more than once at a time
  --> src/main.rs:22:9
   |
21 |     while let Some(_) = foo.f() {
   |                         -------
   |                         |
   |                         first mutable borrow occurs here
   |                         a temporary with access to the first borrow is created here ...
22 |         foo.g();
   |         ^^^ second mutable borrow occurs here
23 |     };
   |     - ... and the first borrow might be used here, when that temporary is dropped and runs the destructor for type `Option<Bar<'_>>`
   |
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
   |
23 |     };;
   |      +

For more information about this error, try `rustc --explain E0499`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

Not have the help section since there already is a semicolon

Rationale and extra context

No response

Other cases

Rust Version

Tested on the Rust Playground with both stable 1.83.0 and 1.85.0-nightly 2024-12-04 acabb52 (sorry for the lack of the exact command you wanted, but see rust-lang/rust-playground#1043)

Anything else?

No response

@josephcsible josephcsible 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 Dec 5, 2024
@workingjubilee workingjubilee added D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. and removed D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Dec 6, 2024
@josephcsible
Copy link
Author

Not sure if it's in-scope of this issue or should be a separate one, but would it be feasible for it to suggest this?

    loop {
        let Some(_) = foo.f() else { break };
        foo.g();
    };

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 2, 2025
…tor, r=compiler-errors

diagnostics: fix borrowck suggestions for if/while let conditionals

This code detects the case where one of the borrows is inside the let init expr while the other end is not. If that happens, we don't want to suggest adding a semicolon, because it won't work.

Fixes rust-lang#133941
@bors bors closed this as completed in e066208 Feb 2, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 2, 2025
Rollup merge of rust-lang#136402 - notriddle:notriddle/let-expr-detector, r=compiler-errors

diagnostics: fix borrowck suggestions for if/while let conditionals

This code detects the case where one of the borrows is inside the let init expr while the other end is not. If that happens, we don't want to suggest adding a semicolon, because it won't work.

Fixes rust-lang#133941
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 D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
2 participants