From 4b3456735718f956a77ea86f00bf6150e26ed449 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Sun, 4 Aug 2024 16:11:17 +0200 Subject: [PATCH] Print location when using `expect_throw()` --- CHANGELOG.md | 1 + src/lib.rs | 96 ++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 75 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52e92864783..33f1f1e8cee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,6 +115,7 @@ * `UnwrapThrowExt for Result` now makes use of the required `Debug` bound to display the error as well. [#4035](https://github.com/rustwasm/wasm-bindgen/pull/4035) + [#4049](https://github.com/rustwasm/wasm-bindgen/pull/4049) * MSRV of CLI tools bumped to v1.76. This does not affect libraries like `wasm-bindgen`, `js-sys` and `web-sys`! [#4037](https://github.com/rustwasm/wasm-bindgen/pull/4037) diff --git a/src/lib.rs b/src/lib.rs index 4fe948ff4af..a691426994c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1351,14 +1351,15 @@ pub trait UnwrapThrowExt: Sized { )) { let loc = core::panic::Location::caller(); let msg = alloc::format!( - "`unwrap_throw` failed ({}:{}:{})", + "called `{}::unwrap_throw()` ({}:{}:{})", + core::any::type_name::(), loc.file(), loc.line(), loc.column() ); self.expect_throw(&msg) } else { - self.expect_throw("`unwrap_throw` failed") + self.expect_throw("called `unwrap_throw()`") } } @@ -1376,11 +1377,43 @@ pub trait UnwrapThrowExt: Sized { } impl UnwrapThrowExt for Option { + fn unwrap_throw(self) -> T { + const MSG: &str = "called `Option::unwrap_throw()` on a `None` value"; + + if cfg!(all(target_arch = "wasm32", target_os = "unknown")) { + if let Some(val) = self { + val + } else if cfg!(debug_assertions) { + let loc = core::panic::Location::caller(); + let msg = + alloc::format!("{} ({}:{}:{})", MSG, loc.file(), loc.line(), loc.column(),); + + throw_str(&msg) + } else { + throw_str(MSG) + } + } else { + self.expect(MSG) + } + } + fn expect_throw(self, message: &str) -> T { if cfg!(all(target_arch = "wasm32", target_os = "unknown")) { - match self { - Some(val) => val, - None => throw_str(message), + if let Some(val) = self { + val + } else if cfg!(debug_assertions) { + let loc = core::panic::Location::caller(); + let msg = alloc::format!( + "{} ({}:{}:{})", + message, + loc.file(), + loc.line(), + loc.column(), + ); + + throw_str(&msg) + } else { + throw_str(message) } } else { self.expect(message) @@ -1393,28 +1426,31 @@ where E: core::fmt::Debug, { fn unwrap_throw(self) -> T { - if cfg!(all( - debug_assertions, - target_arch = "wasm32", - target_os = "unknown" - )) { + const MSG: &str = "called `Result::unwrap_throw()` on an `Err` value"; + + if cfg!(all(target_arch = "wasm32", target_os = "unknown")) { match self { Ok(val) => val, Err(err) => { - let loc = core::panic::Location::caller(); - let msg = alloc::format!( - "`unwrap_throw` failed ({}:{}:{}): {:?}", - loc.file(), - loc.line(), - loc.column(), - err - ); - - throw_str(&msg) + if cfg!(debug_assertions) { + let loc = core::panic::Location::caller(); + let msg = alloc::format!( + "{} ({}:{}:{}): {:?}", + MSG, + loc.file(), + loc.line(), + loc.column(), + err + ); + + throw_str(&msg) + } else { + throw_str(MSG) + } } } } else { - self.expect("`unwrap_throw` failed") + self.expect(MSG) } } @@ -1422,7 +1458,23 @@ where if cfg!(all(target_arch = "wasm32", target_os = "unknown")) { match self { Ok(val) => val, - Err(_) => throw_str(message), + Err(err) => { + if cfg!(debug_assertions) { + let loc = core::panic::Location::caller(); + let msg = alloc::format!( + "{} ({}:{}:{}): {:?}", + message, + loc.file(), + loc.line(), + loc.column(), + err + ); + + throw_str(&msg) + } else { + throw_str(message) + } + } } } else { self.expect(message)