rust/library/core/src
bors 9b6339e4b9 Auto merge of #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
```
2021-03-23 04:49:47 +00:00
..
alloc Fix const stability since versions. 2021-03-15 14:39:18 +00:00
array implement TrustedRandomAccess for array::IntoIter 2021-03-21 20:43:48 +01:00
char Stabilize assoc_char_funcs and assoc_char_consts 2021-03-19 20:35:08 -07:00
convert FloatToInit: Replacing round_unchecked_to --> to_int_unchecked 2021-03-02 12:38:22 -08:00
fmt Stabilize Arguments::as_str 2021-02-14 17:48:51 -05:00
future Use more std:: instead of core:: in docs for consistency, add more intra doc links 2020-12-02 00:41:53 +01:00
hash Rollup merge of #82434 - jyn514:hash, r=JohnTitor 2021-03-18 00:28:07 +01:00
iter Rollup merge of #83272 - kornelski:takedocs, r=dtolnay 2021-03-22 15:21:26 +01:00
macros Apply suggestions from code review 2021-03-18 21:15:19 +01:00
mem Auto merge of #83053 - oli-obk:const_stab_version, r=m-ou-se 2021-03-21 16:21:39 +00:00
num Auto merge of #82680 - jturner314:div_euclid-docs, r=JohnTitor 2021-03-22 09:37:50 +00:00
ops Rollup merge of #82683 - jturner314:int-div-rem-doc-panic, r=nikomatsakis 2021-03-22 15:21:24 +01:00
prelude Deprecate RustcEncodable and RustcDecodable. 2021-03-15 20:16:16 +01:00
ptr Rollup merge of #80771 - thomcc:nonnull-refmut, r=dtolnay 2021-03-22 02:20:24 +01:00
slice Rollup merge of #82771 - emilio:iter-mut-as-slice, r=m-ou-se 2021-03-22 02:20:30 +01:00
str SplitInclusive is public API 2021-03-22 09:07:52 +00:00
stream Remove Stream::next 2021-01-23 16:54:56 +01:00
sync Fix const stability since versions. 2021-03-15 14:39:18 +00:00
task stabilize the poll_map feature 2021-01-13 14:51:27 +10:00
unicode Add a check for ASCII characters in to_upper and to_lower 2021-02-26 11:39:36 -06:00
any.rs Fix use of bare trait objects everywhere 2021-03-18 02:18:58 +03:00
ascii.rs Fixed some intra-docs links in library/core 2020-09-18 07:49:29 +08:00
bool.rs Stabilise then 2020-11-22 13:45:14 +00:00
borrow.rs Fix borrow and deref 2021-03-03 11:23:29 +01:00
cell.rs Add debug-refcell feature to libcore 2021-03-22 12:39:54 -04:00
clone.rs Fix core tests 2021-03-03 11:22:49 +01:00
cmp.rs Added #[repr(transparent)] to core::cmp::Reverse 2021-02-08 12:07:59 +00:00
default.rs Add diagnostic item to Default trait 2021-03-04 10:14:48 -08:00
ffi.rs Convert primitives to use intra-doc links 2021-02-25 20:31:53 -05:00
hint.rs Fixed documentation error 2021-01-03 19:54:54 +02:00
internal_macros.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
intrinsics.rs Auto merge of #82122 - bstrie:dep4real, r=dtolnay 2021-03-17 19:39:03 +00:00
lazy.rs Capitalize safety comments 2020-09-08 22:26:44 -04:00
lib.rs core/std/alloc: stabilize or_patterns 2021-03-19 19:45:42 -05:00
marker.rs Auto merge of #77893 - petertodd:2020-impl-default-for-phantompinned, r=dtolnay 2020-11-23 07:00:30 +00:00
option.rs Rename Option::get_or_insert_default 2021-03-10 09:07:16 -06:00
panic.rs Implement new panic!() behaviour for Rust 2021. 2021-01-25 13:48:11 +01:00
panicking.rs Fix panic message of assert_failed_inner 2021-03-13 18:50:43 +08:00
pin.rs Remove many unnecessary manual link resolves from library 2020-12-31 11:54:32 -08:00
primitive.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
raw.rs Use intra-doc-links in core::{raw, ffi, pin} 2020-08-22 22:25:27 +02:00
result.rs Rollup merge of #82130 - jhpratt:const-option-result, r=RalfJung 2021-03-07 10:41:09 +09:00
time.rs Rollup merge of #81465 - joshtriplett:duration-formatting-documentation, r=m-ou-se 2021-03-14 13:07:27 +09:00
tuple.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
unit.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00