rust/src/libstd
Manish Goregaokar ae79c30d74
Rollup merge of #72445 - anp:stabilize-track-caller, r=oli-obk
Stabilize `#[track_caller]`.

# Stabilization Report

RFC: [2091]
Tracking issue: https://github.com/rust-lang/rust/issues/47809

## Summary

From the [rustc-dev-guide chapter][dev-guide]:

> Take this example program:

```rust
fn main() {
    let foo: Option<()> = None;
    foo.unwrap(); // this should produce a useful panic message!
}
```

> Prior to Rust 1.42, panics like this `unwrap()` printed a location in libcore:

```
$ rustc +1.41.0 example.rs; example.exe
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value',...core\macros\mod.rs:15:40
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
```

> As of 1.42, we get a much more helpful message:

```
$ rustc +1.42.0 example.rs; example.exe
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', example.rs:3:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```

> These error messages are achieved through a combination of changes to `panic!` internals to make use of `core::panic::Location::caller` and a number of `#[track_caller]` annotations in the standard library which propagate caller information.

The attribute adds an implicit caller location argument to the ABI of annotated functions, but does not affect the type or MIR of the function. We implement the feature entirely in codegen and in the const evaluator.

## Bottom Line

This PR stabilizes the use of `#[track_caller]` everywhere, including traits and extern blocks. It also stabilizes `core::panic::Location::caller`, although the use of that function in a const context remains gated by `#![feature(const_caller_location)]`.

The implementation for the feature already changed the output of panic messages for a number of std functions, as described in the [1.42 release announcement]. The attribute's use in `Index` and `IndexMut` traits is visible to users since 1.44.

## Tests

All of the tests for this feature live under [src/test/ui/rfc-2091-track-caller][tests] in the repo.

Noteworthy cases:

* [use of attr in std]
  * validates user-facing benefit of the feature
* [trait attribute inheritance]
  * covers subtle behavior designed during implementation and not RFC'd
* [const/codegen equivalence]
  * this was the result of a suspected edge case and investigation
* [diverging function support]
  * covers an unresolved question from the RFC
* [fn pointers and shims]
  * covers important potential sources of unsoundness

## Documentation

The rustc-dev-guide now has a chapter on [Implicit Caller Location][dev-guide].

I have an [open PR to the reference][attr-reference-pr] documenting the attribute.

The intrinsic's [wrapper] includes some examples as well.

## Implementation History

* 2019-10-02: [`#[track_caller]` feature gate (RFC 2091 1/N) #65037](https://github.com/rust-lang/rust/pull/65037)
  * Picked up the patch that @ayosec had started on the feature gate.
* 2019-10-13: [Add `Instance::resolve_for_fn_ptr` (RFC 2091 #2/N) #65182](https://github.com/rust-lang/rust/pull/65182)
* 2019-10-20: ~~[WIP Add MIR argument for #[track_caller] (RFC 2091 3/N) #65258](https://github.com/rust-lang/rust/pull/65258)~~
  * Abandoned approach to send location as a MIR argument.
* 2019-10-28: [`std::panic::Location` is a lang_item, add `core::intrinsics::caller_location` (RFC 2091 3/N) #65664](https://github.com/rust-lang/rust/pull/65664)
* 2019-12-07: [Implement #[track_caller] attribute. (RFC 2091 4/N) #65881](https://github.com/rust-lang/rust/pull/65881)
* 2020-01-04: [libstd uses `core::panic::Location` where possible. #67137](https://github.com/rust-lang/rust/pull/67137)
* 2020-01-08: [`Option::{expect,unwrap}` and `Result::{expect, expect_err, unwrap, unwrap_err}` have `#[track_caller]` #67887](https://github.com/rust-lang/rust/pull/67887)
* 2020-01-20: [Fix #[track_caller] and function pointers #68302](https://github.com/rust-lang/rust/pull/68302) (fixed #68178)
* 2020-03-23: [#[track_caller] in traits #69251](https://github.com/rust-lang/rust/pull/69251)
* 2020-03-24: [#[track_caller] on core::ops::{Index, IndexMut}. #70234](https://github.com/rust-lang/rust/pull/70234)
* 2020-04-08 [Support `#[track_caller]` on functions in `extern "Rust" { ... }` #70916](https://github.com/rust-lang/rust/pull/70916)

## Unresolveds

### From the RFC

> Currently the RFC simply prohibit applying #[track_caller] to trait methods as a future-proofing
> measure.

**Resolved.** See the dev-guide documentation and the tests section above.

> Diverging functions should be supported.

**Resolved.** See the tests section above.

> The closure foo::{{closure}} should inherit most attributes applied to the function foo, ...

**Resolved.** This unknown was related to specifics of the implementation which were made irrelevant by the final implementation.

### Binary Size

I [instrumented track_caller to use custom sections][measure-size] in a local build and discovered relatively minor binary size usage for the feature overall. I'm leaving the issue open to discuss whether we want to upstream custom section support.

There's an [open issue to discuss mitigation strategies][mitigate-size]. Some decisions remain about the "right" strategies to reduce size without overly constraining the compiler implementation. I'd be excited to see someone carry that work forward but my opinion is that we shouldn't block stabilization on implementing compiler flags for redaction.

### Specialization

There's an [open issue][specialization] on the semantics of the attribute in specialization chains. I'm inclined to move forward with stabilization without an exact resolution here given that specialization is itself unstable, but I also think it should be an easy question to resolve.

### Location only points to the start of a call span

https://github.com/rust-lang/rust/issues/69977 was resolved by https://github.com/rust-lang/rust/pull/73182, and the next step should probably be to [extend `Location` with a notion of the end of a call](https://github.com/rust-lang/rust/issues/73554).

### Regression of std's panic messages

#70963 should be resolved by serializing span hygeine to crate metadata: https://github.com/rust-lang/rust/issues/68686.

[2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md
[dev-guide]: https://rustc-dev-guide.rust-lang.org/codegen/implicit-caller-location.html
[specialization]: https://github.com/rust-lang/rust/issues/70293
[measure-size]: https://github.com/rust-lang/rust/issues/70579
[mitigate-size]: https://github.com/rust-lang/rust/issues/70580
[attr-reference-pr]: https://github.com/rust-lang/reference/pull/742
[wrapper]: https://doc.rust-lang.org/nightly/core/panic/struct.Location.html#method.caller
[tests]: https://github.com/rust-lang/rust/tree/master/src/test/ui/rfc-2091-track-caller
[const/codegen equivalence]: https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2091-track-caller/caller-location-fnptr-rt-ctfe-equiv.rs
[diverging function support]: https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2091-track-caller/diverging-caller-location.rs
[use of attr in std]: https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2091-track-caller/std-panic-locations.rs
[fn pointers and shims]: https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2091-track-caller/tracked-fn-ptr-with-arg.rs
[trait attribute inheritance]: https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2091-track-caller/tracked-trait-impls.rs
[1.42 release announcement]: https://blog.rust-lang.org/2020/03/12/Rust-1.42.html#useful-line-numbers-in-option-and-result-panic-messages
2020-07-01 07:42:33 -07:00
..
benches Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
collections Remove an old comment from HashMap::extend_reserve 2020-05-29 17:05:17 -07:00
ffi Rollup merge of #73139 - poliorcetics:cstring-from-vec-with-nul, r=dtolnay 2020-06-15 12:01:09 +02:00
io Rollup merge of #73826 - cjrh:cjrh-patch-1, r=jonas-schievink 2020-06-28 08:30:34 -07:00
net Rollup merge of #72369 - Lucretiel:socketaddr-parse, r=dtolnay 2020-07-01 07:42:32 -07:00
os Remove unused crate imports in 2018 edition crates 2020-06-23 05:01:20 +02:00
prelude Update the doc for std::prelude, removing the "technical part" section 2020-06-19 21:19:17 +02:00
sync Rollup merge of #73104 - poliorcetics:explicit-mutex-drop-example, r=dtolnay 2020-06-15 12:01:07 +02:00
sys Rename the lint to clashing_extern_declarations. 2020-06-28 10:11:29 +10:00
sys_common Migrate to numeric associated consts 2020-06-10 01:35:47 +00:00
tests Enable ARM TME (Transactional Memory Extensions) 2020-05-21 23:20:57 +02:00
thread Migrate to numeric associated consts 2020-06-10 01:35:47 +00:00
alloc.rs abort_internal is safe 2020-05-17 23:38:31 +02:00
ascii.rs Bump rustfmt to most recently shipped 2020-04-25 09:25:33 -04:00
backtrace.rs remove redundant returns (clippy::needless_return) 2020-03-20 20:23:03 +01:00
build.rs Add illumos triple 2020-04-14 20:36:07 +00:00
Cargo.toml Ensure std benchmarks get tested. 2020-06-18 09:11:15 -07:00
env.rs Remove recommendation for unmaintained crate 2020-05-01 20:51:20 -06:00
error.rs Impl Error for Infallible 2020-04-28 11:22:42 +02:00
f32.rs Rollup merge of #72486 - Ralith:asinh-fix, r=dtolnay 2020-06-19 08:55:59 +02:00
f64.rs Rollup merge of #72486 - Ralith:asinh-fix, r=dtolnay 2020-06-19 08:55:59 +02:00
fs.rs Rollup merge of #73809 - robyoung:docs/add-links-to-DirEntry-metadata, r=hanna-kruppe 2020-06-27 22:30:06 -07:00
future.rs Add core::future::IntoFuture 2020-05-22 10:55:01 +02:00
keyword_docs.rs Rollup merge of #73718 - poliorcetics:super-keyword, r=shepmaster 2020-06-26 13:57:41 -07:00
lib.rs Stabilize #[track_caller]. 2020-06-30 22:22:32 -07:00
macros.rs Update macros.rs: fix documentation typo. 2020-03-04 14:18:31 -05:00
memchr.rs libstd => 2018 2019-02-28 04:06:15 +09:00
num.rs Migrate to numeric associated consts 2020-06-10 01:35:47 +00:00
panic.rs Require issue = "none" over issue = "0" in unstable attributes 2019-12-21 13:16:18 +02:00
panicking.rs Rollup merge of #72617 - eduardosm:panicking, r=Amanieu 2020-06-25 18:00:02 -07:00
path.rs Added the parapgrah to path::Path::is_file too 2020-06-27 22:59:47 +02:00
primitive_docs.rs Do not use "nil" to refer to () 2020-04-05 14:30:13 +02:00
process.rs Implement Sync for `process::Command on unix and vxworks 2020-05-22 18:33:12 +02:00
rt.rs Add a fast path for std:🧵:panicking. 2020-05-26 17:46:10 +02:00
time.rs Rollup merge of #73171 - tblah:riscv-qemu-test, r=pietroalbini 2020-06-20 16:39:51 +02:00