-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Minor tweaks to refcell logging #142216
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
base: master
Are you sure you want to change the base?
Minor tweaks to refcell logging #142216
Conversation
nealsid
commented
Jun 8, 2025
- Fix the logging when debug_refcell is enabled so the location is output as a string rather than a sequence of bytes
- Avoid logging an empty struct when debug_refcell is disabled
- Make the panic error message consistent with the compiler borrow-checking error messages
Thank you for the PR! The wording isn't quite accurate because let cell = RefCell::new(0u32);
let b = (cell.borrow_mut(), cell.borrow_mut());
drop(b); also prints "already borrowed: BorrowMutError" rather than the more accurate "already mutably borrowed: BorrowMutError", but it still works. So I think the original wording of "already (mutably) borrowed (here: {err:?})" is probably fine to keep. We could alternatively add track mutably/immutably in the error type, but I don't expect that to be worth it since the
Mind commenting the before and after here? |
@@ -787,16 +787,24 @@ impl Display for BorrowMutError { | |||
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] | |||
#[track_caller] | |||
#[cold] | |||
fn panic_already_borrowed(err: BorrowMutError) -> ! { | |||
panic!("already borrowed: {:?}", err) | |||
fn panic_already_borrowed(_err: BorrowMutError) -> ! { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit, this can be _err
-> err
since it is used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_err is used in one code path (when debug_refcell is enabled) but not the other, the reason being is that when debug_refcell is disabled, BorrowError
/BorrowMutError
are empty structs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, but whether or not something gets used on all branches doesn't affect liveliness - one is enough. It would be different if the comptime #[cfg(...)]
was used rather than the runtime cfg!()
(semantically runtime at least, even though it's gone with the most basic optimizations) but there won't be any dead_code
warnings here.
@rustbot author for the above |
Reminder, once the PR becomes ready for a review, use |
|
The panic output after my proposed change (I haven't reverted the first logging change yet, this is just meant to show the change in
|
Sounds good, thanks! I'll revert that part of the logging. Your example code made me think of another potential issue:
In this case, the |
It does implement Opened #142279 to track that.
It would be nice, but there isn't an easy way to do this is there? We would need to store multiple locations since we can't tell which will get released last, which would mean keeping some kind of location collection rather than just incrementing/decrementing the |
- Fix the logging when debug_refcell is enabled so the location is output as a string rather than a sequence of bytes - Avoid logging an empty struct when debug_refcell is disabled - Make the panic error message consistent with the compiler borrow-checking error messages