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

Provide help on closures capturing self causing borrow checker errors #105761

Closed
estebank opened this issue Dec 15, 2022 · 2 comments · Fixed by #106641
Closed

Provide help on closures capturing self causing borrow checker errors #105761

estebank opened this issue Dec 15, 2022 · 2 comments · Fixed by #106641
Assignees
Labels
A-borrow-checker Area: The borrow checker A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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

@estebank
Copy link
Contributor

estebank commented Dec 15, 2022

Given a closure that immutably captures self, you can easily cause a lifetime error

struct S;
impl S {
    fn foo(&mut self) {
        let x = || {
            self.bar();
        };
        self.qux();
        x();
    }
    fn bar(&self) {}
    fn qux(&mut self) {}
}
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
 --> src/main.rs:7:9
  |
4 |         let x = || {
  |                 -- immutable borrow occurs here
5 |             self.bar();
  |             ---- first borrow occurs due to use of `*self` in closure
6 |         };
7 |         self.qux();
  |         ^^^^^^^^^^ mutable borrow occurs here
8 |         x();
  |         - immutable borrow later used here

The usual solution here is to explicitly pass self into the closure as an this: &Self argument to side-step the issue. We should suggest doing so.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-closures Area: Closures (`|…| { … }`) A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Dec 15, 2022
@chenyukang
Copy link
Member

@rustbot claim

@estebank
Copy link
Contributor Author

estebank commented Jan 8, 2023

This is a duplicate of #45634

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 13, 2023
…est-this, r=estebank

Provide help on closures capturing self causing borrow checker errors

Fixes rust-lang#105761

r? `@estebank`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 13, 2023
…est-this, r=estebank

Provide help on closures capturing self causing borrow checker errors

Fixes rust-lang#105761

r? ``@estebank``
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 13, 2023
…est-this, r=estebank

Provide help on closures capturing self causing borrow checker errors

Fixes rust-lang#105761

r? ```@estebank```
@bors bors closed this as completed in 57b371a Jan 14, 2023
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-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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