about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2019-11-26 09:24:39 +0100
committerRalf Jung <post@ralfj.de>2019-11-26 09:24:39 +0100
commit3e96ca2bf7f7c558623e372e7a9800ac752faa9c (patch)
tree1eb587ba287c8472ff59b5bf7ca3588cdcf1785d
parent3a8e1b63cfc472a3c4884f6a31ab2236d7dd2fb7 (diff)
downloadrust-3e96ca2bf7f7c558623e372e7a9800ac752faa9c.tar.gz
rust-3e96ca2bf7f7c558623e372e7a9800ac752faa9c.zip
abort on BoxMeUp misuse
-rw-r--r--src/libcore/panic.rs8
-rw-r--r--src/libstd/panicking.rs5
2 files changed, 11 insertions, 2 deletions
diff --git a/src/libcore/panic.rs b/src/libcore/panic.rs
index 0abc481f6e5..99b372d92c8 100644
--- a/src/libcore/panic.rs
+++ b/src/libcore/panic.rs
@@ -266,8 +266,16 @@ impl fmt::Display for Location<'_> {
 #[unstable(feature = "std_internals", issue = "0")]
 #[doc(hidden)]
 pub unsafe trait BoxMeUp {
+    /// Take full ownership of the contents.
     /// The return type is actually `Box<dyn Any + Send>`, but we cannot use `Box` in libcore.
+    ///
     /// After this method got called, only some dummy default value is left in `self`.
+    /// Calling this method twice, or calling `get` after calling this method, is an error.
+    ///
+    /// The argument is borrowed because the panic runtime (`__rust_start_panic`) only
+    /// gets a borrowed `dyn BoxMeUp`.
     fn take_box(&mut self) -> *mut (dyn Any + Send);
+
+    /// Just borrow the contents.
     fn get(&mut self) -> &(dyn Any + Send);
 }
diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs
index 31dcbc6a7cb..5ba5d89bb63 100644
--- a/src/libstd/panicking.rs
+++ b/src/libstd/panicking.rs
@@ -20,6 +20,7 @@ use crate::sys_common::rwlock::RWLock;
 use crate::sys_common::{thread_info, util};
 use crate::sys_common::backtrace::{self, RustBacktrace};
 use crate::thread;
+use crate::process;
 
 #[cfg(not(test))]
 use crate::io::set_panic;
@@ -414,7 +415,7 @@ pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u3
         fn take_box(&mut self) -> *mut (dyn Any + Send) {
             let data = match self.inner.take() {
                 Some(a) => Box::new(a) as Box<dyn Any + Send>,
-                None => Box::new(()), // this should never happen: we got called twice
+                None => process::abort(),
             };
             Box::into_raw(data)
         }
@@ -422,7 +423,7 @@ pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u3
         fn get(&mut self) -> &(dyn Any + Send) {
             match self.inner {
                 Some(ref a) => a,
-                None => &(),
+                None => process::abort(),
             }
         }
     }