This repository was archived by the owner on May 28, 2025. It is now read-only.
Commit 9b6339e
committed
Auto merge of rust-lang#82271 - Aaron1011:debug-refcell, r=m-ou-se
Add `debug-refcell` feature to libcore
See https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell/near/226273614
for some background discussion
This PR adds a new off-by-default feature `debug-refcell` to libcore.
When enabled, this feature stores additional debugging information in
`RefCell`. This information is included in the panic message when
`borrow()` or `borrow_mut()` panics, to make it easier to track down the
source of the issue.
Currently, we store the caller location for the earliest active borrow.
This has a number of advantages:
* There is only a constant amount of overhead per `RefCell`
* We don't need any heap memory, so it can easily be implemented in core
* Since we are storing the *earliest* active borrow, we don't need any
extra logic in the `Drop` implementation for `Ref` and `RefMut`
Limitations:
* We only store the caller location, not a full `Backtrace`. Until
we get support for `Backtrace` in libcore, this is the best tha we can
do.
* The captured location is only displayed when `borrow()` or
`borrow_mut()` panics. If a crate calls `try_borrow().unwrap()`
or `try_borrow_mut().unwrap()`, this extra information will be lost.
To make testing easier, I've enabled the `debug-refcell` feature by
default. I'm not sure how to write a test for this feature - we would
need to rebuild core from the test framework, and create a separate
sysroot.
Since this feature will be off-by-default, users will need to use
`xargo` or `cargo -Z build-std` to enable this feature. For users using
a prebuilt standard library, this feature will be disabled with zero
overhead.
I've created a simple test program:
```rust
use std::cell::RefCell;
fn main() {
let _ = std::panic::catch_unwind(|| {
let val = RefCell::new(true);
let _first = val.borrow();
let _second = val.borrow();
let _third = val.borrow_mut();
});
let _ = std::panic::catch_unwind(|| {
let val = RefCell::new(true);
let first = val.borrow_mut();
drop(first);
let _second = val.borrow_mut();
let _thid = val.borrow();
});
}
```
which produces the following output:
```
thread 'main' panicked at 'already borrowed: BorrowMutError at refcell_test.rs:6:26', refcell_test.rs:8:26
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'already mutably borrowed: BorrowError at refcell_test.rs:16:27', refcell_test.rs:18:25
```2 files changed
+76
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
575 | 575 | | |
576 | 576 | | |
577 | 577 | | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
578 | 584 | | |
579 | 585 | | |
580 | 586 | | |
581 | 587 | | |
582 | 588 | | |
583 | 589 | | |
584 | 590 | | |
| 591 | + | |
| 592 | + | |
585 | 593 | | |
586 | 594 | | |
587 | 595 | | |
588 | 596 | | |
589 | 597 | | |
590 | | - | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
591 | 604 | | |
592 | 605 | | |
593 | 606 | | |
| |||
602 | 615 | | |
603 | 616 | | |
604 | 617 | | |
| 618 | + | |
| 619 | + | |
605 | 620 | | |
606 | 621 | | |
607 | 622 | | |
608 | 623 | | |
609 | 624 | | |
610 | | - | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
611 | 631 | | |
612 | 632 | | |
613 | 633 | | |
| |||
658 | 678 | | |
659 | 679 | | |
660 | 680 | | |
661 | | - | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
662 | 687 | | |
663 | 688 | | |
664 | 689 | | |
| |||
823 | 848 | | |
824 | 849 | | |
825 | 850 | | |
| 851 | + | |
826 | 852 | | |
827 | 853 | | |
828 | | - | |
829 | | - | |
830 | | - | |
831 | | - | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
832 | 874 | | |
833 | 875 | | |
834 | 876 | | |
| |||
896 | 938 | | |
897 | 939 | | |
898 | 940 | | |
| 941 | + | |
899 | 942 | | |
900 | 943 | | |
901 | | - | |
902 | | - | |
903 | | - | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
904 | 960 | | |
905 | 961 | | |
906 | 962 | | |
| |||
1016 | 1072 | | |
1017 | 1073 | | |
1018 | 1074 | | |
1019 | | - | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
1020 | 1082 | | |
1021 | 1083 | | |
1022 | 1084 | | |
| |||
0 commit comments