Skip to content

Commit 7a6af7e

Browse files
committed
Result's panics have #[track_caller].
1 parent 2e9d573 commit 7a6af7e

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/libcore/result.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ impl<T, E: fmt::Debug> Result<T, E> {
957957
/// x.unwrap(); // panics with `emergency failure`
958958
/// ```
959959
#[inline]
960+
#[track_caller]
960961
#[stable(feature = "rust1", since = "1.0.0")]
961962
pub fn unwrap(self) -> T {
962963
match self {
@@ -984,6 +985,7 @@ impl<T, E: fmt::Debug> Result<T, E> {
984985
/// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
985986
/// ```
986987
#[inline]
988+
#[track_caller]
987989
#[stable(feature = "result_expect", since = "1.4.0")]
988990
pub fn expect(self, msg: &str) -> T {
989991
match self {
@@ -1017,6 +1019,7 @@ impl<T: fmt::Debug, E> Result<T, E> {
10171019
/// assert_eq!(x.unwrap_err(), "emergency failure");
10181020
/// ```
10191021
#[inline]
1022+
#[track_caller]
10201023
#[stable(feature = "rust1", since = "1.0.0")]
10211024
pub fn unwrap_err(self) -> E {
10221025
match self {
@@ -1044,6 +1047,7 @@ impl<T: fmt::Debug, E> Result<T, E> {
10441047
/// x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10`
10451048
/// ```
10461049
#[inline]
1050+
#[track_caller]
10471051
#[stable(feature = "result_expect_err", since = "1.17.0")]
10481052
pub fn expect_err(self, msg: &str) -> E {
10491053
match self {
@@ -1188,6 +1192,7 @@ impl<T, E> Result<Option<T>, E> {
11881192
// This is a separate function to reduce the code size of the methods
11891193
#[inline(never)]
11901194
#[cold]
1195+
#[track_caller]
11911196
fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
11921197
panic!("{}: {:?}", msg, error)
11931198
}

src/test/ui/rfc-2091-track-caller/std-panic-locations.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@ fn main() {
2626
let yep: Option<()> = Some(());
2727
assert_panicked(|| yep.unwrap_none());
2828
assert_panicked(|| yep.expect_none(""));
29+
30+
let oops: Result<(), ()> = Err(());
31+
assert_panicked(|| oops.unwrap());
32+
assert_panicked(|| oops.expect(""));
33+
34+
let fine: Result<(), ()> = Ok(());
35+
assert_panicked(|| fine.unwrap_err());
36+
assert_panicked(|| fine.expect_err(""));
2937
}

0 commit comments

Comments
 (0)