`Mutex` and `RwLock` borrow suggestions could suggest `get_mut` if applicable
Open
Nobody has claimed this yet.
A-diagnostics
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
use std::sync::{Mutex, RwLock};
fn f(t: &mut (Mutex<Vec<()>>, RwLock<Vec<()>>))
{
t.0.len();
t.1.len();
}
Current output
error[E0599]: no method named `len` found for struct `Mutex` in the current scope
--> src/lib.rs:5:9
|
5 | t.0.len();
| ^^^ method not found in `Mutex<Vec<()>>`
|
note: the method `len` exists on the type `Vec<()>`
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2705:5
|
2705 | pub const fn len(&self) -> usize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.lock().unwrap()` to borrow the `Vec<()>`, blocking the current thread until it can be acquired
|
5 | t.0.lock().unwrap().len();
| ++++++++++++++++
error[E0599]: no method named `len` found for struct `RwLock` in the current scope
--> src/lib.rs:6:9
|
6 | t.1.len();
| ^^^ method not found in `RwLock<Vec<()>>`
|
note: the method `len` exists on the type `Vec<()>`
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2705:5
|
2705 | pub const fn len(&self) -> usize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.read().unwrap()` to borrow the `Vec<()>`, blocking the current thread until it can be acquired
|
6 | t.1.read().unwrap().len();
| ++++++++++++++++
For more information about this error, try `rustc --explain E0599`.
Desired output
error[E0599]: no method named `len` found for struct `Mutex` in the current scope
--> src/lib.rs:5:9
|
5 | t.0.len();
| ^^^ method not found in `Mutex<Vec<()>>`
|
note: the method `len` exists on the type `Vec<()>`
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2705:5
|
2705 | pub const fn len(&self) -> usize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.get_mut().unwrap()` to borrow the `Vec<()>`
|
5 | t.0.get_mut().unwrap().len();
| ++++++++++++++++
error[E0599]: no method named `len` found for struct `RwLock` in the current scope
--> src/lib.rs:6:9
|
6 | t.1.len();
| ^^^ method not found in `RwLock<Vec<()>>`
|
note: the method `len` exists on the type `Vec<()>`
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2705:5
|
2705 | pub const fn len(&self) -> usize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.get_mut().unwrap()` to borrow the `Vec<()>`
|
6 | t.1.get_mut().unwrap().len();
| ++++++++++++++++
For more information about this error, try `rustc --explain E0599`.
Rationale and extra context
If it is possible to call get_mut, there is no reason to call lock or read instead, as they just do the same thing* less efficiently in this context.
* Except if a guard is mem::forgetten, this will succeed instead of block indefinitely, but whatever.
Other cases
Rust Version
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7
Compiler returned: 0
Anything else?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the diagnostic with the Rust example in the issue and compare the current and desired suggestions for Mutex and RwLock. Trace the compiler diagnostic code that generates borrow suggestions, then add coverage showing that &mut access selects get_mut().unwrap() and that the resulting help text matches the desired output.
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