Skip to content

Commit 221000a

Browse files
committed
Refactored code to access TLS only in case of panic
1 parent ae77410 commit 221000a

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/libstd/panicking.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,9 @@ fn default_hook(info: &PanicInfo) {
242242
pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<Any + Send>> {
243243
let mut slot = None;
244244
let mut f = Some(f);
245-
let ret = PANIC_COUNT.with(|s| {
246-
let prev = s.get();
247-
s.set(0);
245+
let ret;
248246

247+
{
249248
let mut to_run = || {
250249
slot = Some(f.take().unwrap()());
251250
};
@@ -258,18 +257,21 @@ pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<Any + Send>> {
258257
dataptr,
259258
&mut any_data,
260259
&mut any_vtable);
261-
s.set(prev);
262-
263260
if r == 0 {
264-
Ok(())
261+
ret = Ok(());
265262
} else {
266-
Err(mem::transmute(raw::TraitObject {
263+
PANIC_COUNT.with(|s| {
264+
let prev = s.get();
265+
s.set(prev - 1);
266+
});
267+
ret = Err(mem::transmute(raw::TraitObject {
267268
data: any_data as *mut _,
268269
vtable: any_vtable as *mut _,
269-
}))
270+
}));
270271
}
271-
});
272+
}
272273

274+
debug_assert!(PANIC_COUNT.with(|c| c.get() == 0));
273275
return ret.map(|()| {
274276
slot.take().unwrap()
275277
});

0 commit comments

Comments
 (0)