From 08f779cb4b481be58eeb5ecc421f69503780e8b1 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 25 Nov 2019 12:16:08 +0100 Subject: [PATCH] better comment and rename BoxMeUp::box_me_up to take_box --- src/libcore/panic.rs | 4 +++- src/libpanic_unwind/lib.rs | 2 +- src/libstd/panicking.rs | 9 +++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/libcore/panic.rs b/src/libcore/panic.rs index cdd38449a1be..0abc481f6e53 100644 --- a/src/libcore/panic.rs +++ b/src/libcore/panic.rs @@ -266,6 +266,8 @@ impl fmt::Display for Location<'_> { #[unstable(feature = "std_internals", issue = "0")] #[doc(hidden)] pub unsafe trait BoxMeUp { - fn box_me_up(&mut self) -> *mut (dyn Any + Send); + /// The return type is actually `Box`, but we cannot use `Box` in libcore. + /// After this method got called, only some dummy default value is left in `self`. + fn take_box(&mut self) -> *mut (dyn Any + Send); fn get(&mut self) -> &(dyn Any + Send); } diff --git a/src/libpanic_unwind/lib.rs b/src/libpanic_unwind/lib.rs index d97a7a8a87d8..0c834e5c2a05 100644 --- a/src/libpanic_unwind/lib.rs +++ b/src/libpanic_unwind/lib.rs @@ -94,5 +94,5 @@ pub unsafe extern "C" fn __rust_maybe_catch_panic(f: fn(*mut u8), #[unwind(allowed)] pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 { let payload = payload as *mut &mut dyn BoxMeUp; - imp::panic(Box::from_raw((*payload).box_me_up())) + imp::panic(Box::from_raw((*payload).take_box())) } diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs index a0f6183d514e..a16eec45b9aa 100644 --- a/src/libstd/panicking.rs +++ b/src/libstd/panicking.rs @@ -340,6 +340,7 @@ fn panic_handler(info: &PanicInfo<'_>) -> ! { use crate::fmt::Write; let inner = self.inner; + // Lazily, the first time this gets called, run the actual string formatting. self.string.get_or_insert_with(|| { let mut s = String::new(); drop(s.write_fmt(*inner)); @@ -349,7 +350,7 @@ fn panic_handler(info: &PanicInfo<'_>) -> ! { } unsafe impl<'a> BoxMeUp for PanicPayload<'a> { - fn box_me_up(&mut self) -> *mut (dyn Any + Send) { + fn take_box(&mut self) -> *mut (dyn Any + Send) { let contents = mem::take(self.fill()); Box::into_raw(Box::new(contents)) } @@ -407,10 +408,10 @@ pub fn begin_panic(msg: M, file_line_col: &(&'static str, u32, u3 } unsafe impl BoxMeUp for PanicPayload { - fn box_me_up(&mut self) -> *mut (dyn Any + Send) { + fn take_box(&mut self) -> *mut (dyn Any + Send) { let data = match self.inner.take() { Some(a) => Box::new(a) as Box, - None => Box::new(()), + None => Box::new(()), // this should never happen: we got called twice }; Box::into_raw(data) } @@ -488,7 +489,7 @@ pub fn update_count_then_panic(msg: Box) -> ! { struct RewrapBox(Box); unsafe impl BoxMeUp for RewrapBox { - fn box_me_up(&mut self) -> *mut (dyn Any + Send) { + fn take_box(&mut self) -> *mut (dyn Any + Send) { Box::into_raw(mem::replace(&mut self.0, Box::new(()))) }