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

move recommendation with closure and invalid lifetime is inserted at the wrong place #113087

Closed
sosthene-nitrokey opened this issue Jun 27, 2023 · 0 comments · Fixed by #113137
Closed
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

@sosthene-nitrokey
Copy link

sosthene-nitrokey commented Jun 27, 2023

Code

#[derive(Default)]
struct A {}

struct B<'a> {
    a: &'a mut A,
}

struct C<'a, 'b> {
    b: &'b mut B<'a>,
}

fn some_fn<'a>(b: &mut B<'a>, f: impl FnOnce(&C<'a, '_>)) {}

fn main() {
    let some_closure = |_fs| {};

    for _ in [1] {
        let mut a = Default::default();
        let mut b = B { a: &mut a };
        some_fn(&mut b, |c| {
            some_closure(c);
        });
    }
}

Current output

error[E0373]: closure may outlive the current block, but it borrows `a`, which is owned by the current block
  --> src/main.rs:19:28
   |
19 |         let mut b = B { a: &mut a };
   |                            ^^^^^^
   |                            |
   |                            `a` is borrowed here
   |                            may outlive borrowed value `a`
   |
note: block requires argument type to outlive `'1`
  --> src/main.rs:21:13
   |
21 |             some_closure(c);
   |             ^^^^^^^^^^^^
help: to force the closure to take ownership of `a` (and any other referenced variables), use the `move` keyword
   |
19 |         let mut b = B { a: move &mut a };
   |                            ++++

Desired output

move should not be recommended as it does not solve any issue. This is the case even if put at the closure definition since the closure does not capture anything.

Rationale and extra context

The recommendation for the move is wrong.

Other cases

No response

Anything else?

Playground link.

Rust version:

Nightly version: 1.72.0-nightly

(2023-06-21 065a1f5df9c2f1d93269)
@sosthene-nitrokey sosthene-nitrokey 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 Jun 27, 2023
@estebank estebank added the D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. label Jun 27, 2023
@bors bors closed this as completed in 7e1869f Jun 29, 2023
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
Development

Successfully merging a pull request may close this issue.

2 participants