rust-lang / rust-lang/rust

`debug_refcell` points into std source for `LocalKey::with_borrow`

Open
#148,588 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

use std::cell::RefCell;

thread_local! {
    static FOO: RefCell<()> = RefCell::new(());
}

fn main() {
    FOO.with_borrow(|_x| FOO.with_borrow_mut(|_y| {}))
}

Run with cargo +nightly run -Zbuild-std -Zbuild-std-features="debug_refcell"

I expect to get an error telling me where the first and second borrow occur in my code.
What actually happens is that I get two locations in the standard library:

thread 'main' (141310) panicked at /home/lucasholten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:752:38:
RefCell already borrowed; a previous borrow was at /home/lucasholten/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:719:34

It seems to me that LocalKey::with_borrow and LocalKey::with_borrow_mut should have #[track_caller] applied, at least when the debug_refcell feature is used.

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

Reproduce the example with cargo +nightly run -Zbuild-std -Zbuild-std-features="debug_refcell", then inspect library/std/src/thread/local.rs around lines 719 and 752. The issue is done when borrow diagnostics for LocalKey::with_borrow and with_borrow_mut point to the user call sites rather than only standard-library locations.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
developer-experience
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.