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

Add #[track_caller] for better panic messages for fallible locks using lock_api. #456

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SFBdragon
Copy link

I would like to write a RawMutex implementation that behaves like a mutable-borrow-only RefCell. This is fairly trivial, but the main difference is that RefCell leverages #[track_caller] and std::panic::Location::caller to provide great panic messages when debug_assertions are enabled, indicating where the prior borrow occurred when panicking. I'd like to replicate that, but the issue is that lock_api::Mutex's lock function doesn't have the #[track_caller] attribute which results in error messages saying "could not borrow when previously borrows here: [in the lock_api::Mutex::lock function]".

In general, if a RawMutex's lock implementation ever fails AND the writer of the RawMutex impl marked the lock function with #[track_caller], I think it's much more helpful for any panic to indicate where the user of the lock_api::Mutex called lock rather than where lock_api::Mutex called lock_api::RawMutex::lock. The latter is useless information, the former is very useful for... well, making an unsynchronized RefCell-like lock (the RawMutex is !Sync and the Guard is !Send).

This PR marks some of the lock functions in lock_api with #[track_caller] where they might reasonably panic for similar reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant