Skip to content

Commit 2308e61

Browse files
daxpeddaLiamolucko
andauthored
Revise #[track_caller] condition on UnwrapThrowExt (#4042)
Fixes missing `cfg(debug_assertions)` condition in `impl UnwrapThrowExt for Result`. Remove wrong `cfg(feature = "std")` condition on `UnwrapThrowExt::unwrap_throw()`s default implementation. Co-authored-by: Liam Murphy <43807659+Liamolucko@users.noreply.github.com>
1 parent 4f37343 commit 2308e61

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@
163163
* Implement a more reliable way to detect the stack pointer.
164164
[#4036](https://github.com/rustwasm/wasm-bindgen/pull/4036)
165165

166+
* `#[track_caller]` is now always applied on `UnwrapThrowExt` methods when not targetting `wasm32-unknown-unknown`.
167+
[#4042](https://github.com/rustwasm/wasm-bindgen/pull/4042)
168+
166169
--------------------------------------------------------------------------------
167170

168171
## [0.2.92](https://github.com/rustwasm/wasm-bindgen/compare/0.2.91...0.2.92)

src/lib.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,9 +1337,24 @@ pub fn anyref_heap_live_count() -> u32 {
13371337
pub trait UnwrapThrowExt<T>: Sized {
13381338
/// Unwrap this `Option` or `Result`, but instead of panicking on failure,
13391339
/// throw an exception to JavaScript.
1340-
#[cfg_attr(debug_assertions, track_caller)]
1340+
#[cfg_attr(
1341+
any(
1342+
debug_assertions,
1343+
not(all(
1344+
target_arch = "wasm32",
1345+
not(any(target_os = "emscripten", target_os = "wasi"))
1346+
))
1347+
),
1348+
track_caller
1349+
)]
13411350
fn unwrap_throw(self) -> T {
1342-
if cfg!(all(debug_assertions, feature = "std")) {
1351+
if cfg!(all(
1352+
debug_assertions,
1353+
all(
1354+
target_arch = "wasm32",
1355+
not(any(target_os = "emscripten", target_os = "wasi"))
1356+
)
1357+
)) {
13431358
let loc = core::panic::Location::caller();
13441359
let msg = alloc::format!(
13451360
"`unwrap_throw` failed ({}:{}:{})",
@@ -1356,12 +1371,20 @@ pub trait UnwrapThrowExt<T>: Sized {
13561371
/// Unwrap this container's `T` value, or throw an error to JS with the
13571372
/// given message if the `T` value is unavailable (e.g. an `Option<T>` is
13581373
/// `None`).
1359-
#[cfg_attr(debug_assertions, track_caller)]
1374+
#[cfg_attr(
1375+
any(
1376+
debug_assertions,
1377+
not(all(
1378+
target_arch = "wasm32",
1379+
not(any(target_os = "emscripten", target_os = "wasi"))
1380+
))
1381+
),
1382+
track_caller
1383+
)]
13601384
fn expect_throw(self, message: &str) -> T;
13611385
}
13621386

13631387
impl<T> UnwrapThrowExt<T> for Option<T> {
1364-
#[cfg_attr(debug_assertions, track_caller)]
13651388
fn expect_throw(self, message: &str) -> T {
13661389
if cfg!(all(
13671390
target_arch = "wasm32",
@@ -1381,9 +1404,9 @@ impl<T, E> UnwrapThrowExt<T> for Result<T, E>
13811404
where
13821405
E: core::fmt::Debug,
13831406
{
1384-
#[cfg_attr(debug_assertions, track_caller)]
13851407
fn unwrap_throw(self) -> T {
13861408
if cfg!(all(
1409+
debug_assertions,
13871410
target_arch = "wasm32",
13881411
not(any(target_os = "emscripten", target_os = "wasi"))
13891412
)) {
@@ -1403,11 +1426,10 @@ where
14031426
}
14041427
}
14051428
} else {
1406-
self.unwrap()
1429+
self.expect("`unwrap_throw` failed")
14071430
}
14081431
}
14091432

1410-
#[cfg_attr(debug_assertions, track_caller)]
14111433
fn expect_throw(self, message: &str) -> T {
14121434
if cfg!(all(
14131435
target_arch = "wasm32",

0 commit comments

Comments
 (0)