diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs index 57a4c3df70a..4473e79a294 100644 --- a/src/libstd/panicking.rs +++ b/src/libstd/panicking.rs @@ -242,10 +242,9 @@ fn default_hook(info: &PanicInfo) { pub unsafe fn try R>(f: F) -> Result> { let mut slot = None; let mut f = Some(f); - let ret = PANIC_COUNT.with(|s| { - let prev = s.get(); - s.set(0); + let ret; + { let mut to_run = || { slot = Some(f.take().unwrap()()); }; @@ -258,18 +257,21 @@ pub unsafe fn try R>(f: F) -> Result> { dataptr, &mut any_data, &mut any_vtable); - s.set(prev); - if r == 0 { - Ok(()) + ret = Ok(()); } else { - Err(mem::transmute(raw::TraitObject { + PANIC_COUNT.with(|s| { + let prev = s.get(); + s.set(prev - 1); + }); + ret = Err(mem::transmute(raw::TraitObject { data: any_data as *mut _, vtable: any_vtable as *mut _, - })) + })); } - }); + } + debug_assert!(PANIC_COUNT.with(|c| c.get() == 0)); return ret.map(|()| { slot.take().unwrap() });