-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Miscellaneous RefCell cleanups #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?
Miscellaneous RefCell cleanups #142216
Conversation
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? |
library/core/src/cell.rs
Outdated
@@ -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 |
c7ff921
to
380f569
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment was marked as resolved.
This comment was marked as resolved.
I seem to have hosed my repo, sorry about that! Please ignore. |
Ah, I missed that, thanks!
Agreed on it not being worth it to track mutably/immutably in the error type. However, I still think the error messages are a bit cryptic, even if the audience is developers (or, in the case of enabling How about the following? Without debug_refcell enabled:
With debug_refcell enabled:
|
@tgross35 In my testing I've noticed that the |
Yeah, I don't see an easy way to do that, and even if we did store a location collection, it would be out of date after borrows were dropped. |
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.
Couple small requests then I think we can get this in!
Comment @rustbot review
when it's ready for a review (so I don't miss it in my queue)
more information to Display implementation for BorrowError/BorrowMutError - The BorrowError/BorrowMutError Debug implementations do not print anything differently from what the derived implementation does, so we don't need it. - This change also adds the location field of BorrowError/BorrowMutError to the the Display output when it is present, rewords the error message, and uses the Display trait for outputting the error message instead of Debug.
It's actually used as a counter so update the name to reflect that.
@rustbot review |
Thank you! @bors r+ rollup |
Miscellaneous RefCell cleanups - Clarify `RefCell` error messages when borrow rules are broken - Remove `Debug` impl for `BorrowError`/`BorrowMutError` since `#derive(Debug)` provides identical functionality - Rename `BorrowFlag` to `BorrowCounter`
Rollup of 11 pull requests Successful merges: - #140809 (Reduce special casing for the panic runtime) - #141608 (Add support for repetition to `proc_macro::quote`) - #141864 (Handle win32 separator for cygwin paths) - #142216 (Miscellaneous RefCell cleanups) - #142517 (Windows: Use anonymous pipes in Command) - #142570 (Reject union default field values) - #142584 (Handle same-crate macro for borrowck semicolon suggestion) - #142585 (Update books) - #142586 (Fold unnecessary `visit_struct_field_def` in AstValidator) - #142595 (Revert overeager warning for misuse of `--print native-static-libs`) - #142598 (Set elf e_flags on ppc64 targets according to abi) r? `@ghost` `@rustbot` modify labels: rollup
Miscellaneous RefCell cleanups - Clarify `RefCell` error messages when borrow rules are broken - Remove `Debug` impl for `BorrowError`/`BorrowMutError` since `#derive(Debug)` provides identical functionality - Rename `BorrowFlag` to `BorrowCounter`
Rollup of 13 pull requests Successful merges: - #138538 (Make performance description of String::{insert,insert_str,remove} more precise) - #141946 (std: refactor explanation of `NonNull`) - #142216 (Miscellaneous RefCell cleanups) - #142542 (Manually invalidate caches in SimplifyCfg.) - #142563 (Refine run-make test ignores due to unpredictable `i686-pc-windows-gnu` unwind mechanism) - #142570 (Reject union default field values) - #142584 (Handle same-crate macro for borrowck semicolon suggestion) - #142585 (Update books) - #142586 (Fold unnecessary `visit_struct_field_def` in AstValidator) - #142587 (Make sure to propagate result from `visit_expr_fields`) - #142595 (Revert overeager warning for misuse of `--print native-static-libs`) - #142598 (Set elf e_flags on ppc64 targets according to abi) - #142601 (Add a comment to `FORMAT_VERSION`.) r? `@ghost` `@rustbot` modify labels: rollup <!-- homu-ignore:start --> [Create a similar rollup](https://bors.rust-lang.org/queue/rust?prs=138538,141946,142216,142542,142563,142570,142584,142585,142586,142587,142595,142598,142601) <!-- homu-ignore:end --> try-job: dist-apple-various
Miscellaneous RefCell cleanups - Clarify `RefCell` error messages when borrow rules are broken - Remove `Debug` impl for `BorrowError`/`BorrowMutError` since `#derive(Debug)` provides identical functionality - Rename `BorrowFlag` to `BorrowCounter`
Rollup of 10 pull requests Successful merges: - #138538 (Make performance description of String::{insert,insert_str,remove} more precise) - #141946 (std: refactor explanation of `NonNull`) - #142216 (Miscellaneous RefCell cleanups) - #142371 (avoid `&mut P<T>` in `visit_expr` etc methods) - #142377 (Try unremapping compiler sources) - #142517 (Windows: Use anonymous pipes in Command) - #142542 (Manually invalidate caches in SimplifyCfg.) - #142563 (Refine run-make test ignores due to unpredictable `i686-pc-windows-gnu` unwind mechanism) - #142570 (Reject union default field values) - #142584 (Handle same-crate macro for borrowck semicolon suggestion) r? `@ghost` `@rustbot` modify labels: rollup
RefCell
error messages when borrow rules are brokenDebug
impl forBorrowError
/BorrowMutError
since#derive(Debug)
provides identical functionalityBorrowFlag
toBorrowCounter