Skip to content

Commit 0ab8e0e

Browse files
committed
Refactored code to access TLS only in case of panic
1 parent 60564d0 commit 0ab8e0e

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
@@ -232,10 +232,9 @@ fn default_hook(info: &PanicInfo) {
232232
pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<Any + Send>> {
233233
let mut slot = None;
234234
let mut f = Some(f);
235-
let ret = PANIC_COUNT.with(|s| {
236-
let prev = s.get();
237-
s.set(0);
235+
let ret;
238236

237+
{
239238
let mut to_run = || {
240239
slot = Some(f.take().unwrap()());
241240
};
@@ -248,18 +247,21 @@ pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<Any + Send>> {
248247
dataptr,
249248
&mut any_data,
250249
&mut any_vtable);
251-
s.set(prev);
252-
253250
if r == 0 {
254-
Ok(())
251+
ret = Ok(());
255252
} else {
256-
Err(mem::transmute(raw::TraitObject {
253+
PANIC_COUNT.with(|s| {
254+
let prev = s.get();
255+
s.set(prev - 1);
256+
});
257+
ret = Err(mem::transmute(raw::TraitObject {
257258
data: any_data as *mut _,
258259
vtable: any_vtable as *mut _,
259-
}))
260+
}));
260261
}
261-
});
262+
}
262263

264+
debug_assert!(PANIC_COUNT.with(|c| c.get() == 0));
263265
return ret.map(|()| {
264266
slot.take().unwrap()
265267
});

0 commit comments

Comments
 (0)