rust-lang / rust-lang/rust

Detect need for partial `self` borrows in methods and suggest alternatives

Open
#147,108 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker A-diagnostics A-suggestion-diagnostics D-newcomer-roadblock T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

When encountering methods where getters on self are used which cause borrow errors like the following:

error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
  --> src/lib.rs:20:17
   |
19 |         let a = self.get_a();
   |                 ---- immutable borrow occurs here
20 |         let b = self.get_mut_b();
   |                 ^^^^^^^^^^^^^^^^ mutable borrow occurs here
21 |         println!("{a:?} {b:?}");
   |                    - immutable borrow later used here

error[E0502]: cannot borrow `self.b` as mutable because it is also borrowed as immutable
  --> src/lib.rs:29:9
   |
29 |         self.b.bar(self.get_a());
   |         ^^^^^^^---^----^^^^^^^^^
   |         |      |   |
   |         |      |   immutable borrow occurs here
   |         |      immutable borrow later used by call
   |         mutable borrow occurs here

error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
  --> src/lib.rs:35:9
   |
35 |         self.bar(self.get_a());
   |         ^^^^^---^----^^^^^^^^^
   |         |    |   |
   |         |    |   immutable borrow occurs here
   |         |    immutable borrow later used by call
   |         mutable borrow occurs here

error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
  --> src/lib.rs:37:9
   |
37 |         self.bar(&self.a);
   |         ^^^^^---^-------^
   |         |    |   |
   |         |    |   immutable borrow occurs here
   |         |    immutable borrow later used by call
   |         mutable borrow occurs here

we should explain that get_a and get_mut_b borrow the entirety of Self making accessing any other method or field simultaneously will never work. If we ever get partial self borrows, then we should suggest it here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Rust Playground reproducer and the E0502 diagnostics shown in the issue. Trace where these borrow errors are diagnosed and determine how the diagnostic should explain whole-self borrows; done means the current examples receive that explanation and partial-self-borrow support produces an appropriate suggestion.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.