Skip to content

Commit eaef110

Browse files
committed
format payload if possible instead of returning "Box<Any>"
1 parent 63f18e1 commit eaef110

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/libstd/panicking.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,11 @@ fn continue_panic_fmt(info: &PanicInfo) -> ! {
440440
PanicPayload { payload, msg, string: None }
441441
}
442442

443-
444443
fn fill(&mut self) -> Option<&mut String> {
445-
if let Some(msg) = self.msg.take() {
444+
if let Some(msg) = self.msg.cloned() {
446445
Some(self.string.get_or_insert_with(|| {
447446
let mut s = String::new();
448-
drop(s.write_fmt(*msg));
447+
drop(s.write_fmt(msg));
449448
s
450449
}))
451450
} else {
@@ -459,6 +458,10 @@ fn continue_panic_fmt(info: &PanicInfo) -> ! {
459458
if let Some(string) = self.fill() {
460459
let contents = mem::replace(string, String::new());
461460
Box::into_raw(Box::new(contents))
461+
} else if let Some(s) = self.payload.downcast_ref::<&str>() {
462+
Box::into_raw(Box::new(s.to_owned()))
463+
} else if let Some(s) = self.payload.downcast_ref::<String>() {
464+
Box::into_raw(Box::new(s.clone()))
462465
} else {
463466
// We can't go from &(Any+Send) to Box<Any+Send> so the payload is lost here
464467
struct NoPayload;
@@ -467,7 +470,11 @@ fn continue_panic_fmt(info: &PanicInfo) -> ! {
467470
}
468471

469472
fn get(&mut self) -> &(Any + Send) {
470-
self.payload
473+
if let Some(s) = self.fill() {
474+
s
475+
} else {
476+
self.payload
477+
}
471478
}
472479
}
473480
}

0 commit comments

Comments
 (0)