Skip to content

Commit 3e96ca2

Browse files
committed
abort on BoxMeUp misuse
1 parent 3a8e1b6 commit 3e96ca2

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/libcore/panic.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,16 @@ impl fmt::Display for Location<'_> {
266266
#[unstable(feature = "std_internals", issue = "0")]
267267
#[doc(hidden)]
268268
pub unsafe trait BoxMeUp {
269+
/// Take full ownership of the contents.
269270
/// The return type is actually `Box<dyn Any + Send>`, but we cannot use `Box` in libcore.
271+
///
270272
/// After this method got called, only some dummy default value is left in `self`.
273+
/// Calling this method twice, or calling `get` after calling this method, is an error.
274+
///
275+
/// The argument is borrowed because the panic runtime (`__rust_start_panic`) only
276+
/// gets a borrowed `dyn BoxMeUp`.
271277
fn take_box(&mut self) -> *mut (dyn Any + Send);
278+
279+
/// Just borrow the contents.
272280
fn get(&mut self) -> &(dyn Any + Send);
273281
}

src/libstd/panicking.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::sys_common::rwlock::RWLock;
2020
use crate::sys_common::{thread_info, util};
2121
use crate::sys_common::backtrace::{self, RustBacktrace};
2222
use crate::thread;
23+
use crate::process;
2324

2425
#[cfg(not(test))]
2526
use crate::io::set_panic;
@@ -414,15 +415,15 @@ pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u3
414415
fn take_box(&mut self) -> *mut (dyn Any + Send) {
415416
let data = match self.inner.take() {
416417
Some(a) => Box::new(a) as Box<dyn Any + Send>,
417-
None => Box::new(()), // this should never happen: we got called twice
418+
None => process::abort(),
418419
};
419420
Box::into_raw(data)
420421
}
421422

422423
fn get(&mut self) -> &(dyn Any + Send) {
423424
match self.inner {
424425
Some(ref a) => a,
425-
None => &(),
426+
None => process::abort(),
426427
}
427428
}
428429
}

0 commit comments

Comments
 (0)