Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print location when using expect_throw() #4049

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
96 changes: 74 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,14 +1351,15 @@ pub trait UnwrapThrowExt<T>: Sized {
)) {
let loc = core::panic::Location::caller();
let msg = alloc::format!(
"`unwrap_throw` failed ({}:{}:{})",
"called `{}::unwrap_throw()` ({}:{}:{})",
core::any::type_name::<Self>(),
loc.file(),
loc.line(),
loc.column()
);
self.expect_throw(&msg)
} else {
self.expect_throw("`unwrap_throw` failed")
self.expect_throw("called `unwrap_throw()`")
}
}

Expand All @@ -1376,11 +1377,43 @@ pub trait UnwrapThrowExt<T>: Sized {
}

impl<T> UnwrapThrowExt<T> for Option<T> {
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)
Expand All @@ -1393,36 +1426,55 @@ 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)
}
}

fn expect_throw(self, message: &str) -> T {
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)
Expand Down