Skip to content

Commit 430ad76

Browse files
committed
undo payload in core::panic! changes
1 parent eb19361 commit 430ad76

File tree

3 files changed

+37
-126
lines changed

3 files changed

+37
-126
lines changed

src/libcore/macros.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
/// Entry point of thread panic, for details, see std::macros
12-
#[cfg(stage0)]
1312
#[macro_export]
1413
#[allow_internal_unstable]
1514
#[stable(feature = "core", since = "1.6.0")]
@@ -29,27 +28,6 @@ macro_rules! panic {
2928
});
3029
}
3130

32-
/// Entry point of thread panic, for details, see std::macros
33-
#[cfg(not(stage0))]
34-
#[macro_export]
35-
#[allow_internal_unstable]
36-
#[stable(feature = "core", since = "1.6.0")]
37-
macro_rules! panic {
38-
() => (
39-
panic!("explicit panic")
40-
);
41-
($msg:expr) => ({
42-
$crate::panicking::panic_payload($msg, &(file!(), line!(), __rust_unstable_column!()))
43-
});
44-
($msg:expr,) => (
45-
panic!($msg)
46-
);
47-
($fmt:expr, $($arg:tt)+) => ({
48-
$crate::panicking::panic_fmt(format_args!($fmt, $($arg)*),
49-
&(file!(), line!(), __rust_unstable_column!()))
50-
});
51-
}
52-
5331
/// Asserts that two expressions are equal to each other (using [`PartialEq`]).
5432
///
5533
/// On panic, this macro will print the values of the expressions with their

src/libcore/panicking.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,10 @@
3636
and related macros",
3737
issue = "0")]
3838

39-
#[cfg(not(stage0))]
40-
use any::Any;
4139
use fmt;
4240
#[cfg(not(stage0))]
4341
use panic::{Location, PanicInfo};
4442

45-
// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
46-
#[cfg(not(stage0))]
47-
#[allow(improper_ctypes)] // PanicInfo contains a trait object which is not FFI safe
48-
extern "Rust" {
49-
#[lang = "panic_impl"]
50-
fn panic_impl(pi: &PanicInfo) -> !;
51-
}
52-
53-
#[cfg(not(stage0))]
54-
#[cold] #[inline(never)]
55-
pub fn panic_payload<M>(msg: M, file_line_col: &(&'static str, u32, u32)) -> !
56-
where
57-
M: Any + Send,
58-
{
59-
let (file, line, col) = *file_line_col;
60-
let mut pi = PanicInfo::internal_constructor(
61-
None,
62-
Location::internal_constructor(file, line, col),
63-
);
64-
pi.set_payload(&msg);
65-
unsafe { panic_impl(&pi) }
66-
}
67-
6843
#[cold] #[inline(never)] // this is the slow path, always
6944
#[lang = "panic"]
7045
pub fn panic(expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
@@ -102,6 +77,13 @@ pub fn panic_fmt(fmt: fmt::Arguments, file_line_col: &(&'static str, u32, u32))
10277
#[cfg(not(stage0))]
10378
#[cold] #[inline(never)]
10479
pub fn panic_fmt(fmt: fmt::Arguments, file_line_col: &(&'static str, u32, u32)) -> ! {
80+
// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
81+
#[allow(improper_ctypes)] // PanicInfo contains a trait object which is not FFI safe
82+
extern "Rust" {
83+
#[lang = "panic_impl"]
84+
fn panic_impl(pi: &PanicInfo) -> !;
85+
}
86+
10587
let (file, line, col) = *file_line_col;
10688
let pi = PanicInfo::internal_constructor(
10789
Some(&fmt),

src/libstd/panicking.rs

Lines changed: 30 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ fn default_hook(info: &PanicInfo) {
186186

187187
let location = info.location().unwrap(); // The current implementation always returns Some
188188

189-
let msg = match info.payload().downcast_ref::<&str>() {
189+
let msg = match info.payload().downcast_ref::<&'static str>() {
190190
Some(s) => *s,
191191
None => match info.payload().downcast_ref::<String>() {
192192
Some(s) => &s[..],
@@ -351,44 +351,45 @@ pub fn rust_begin_panic(info: &PanicInfo) -> ! {
351351
#[inline(never)] #[cold]
352352
pub fn begin_panic_fmt(msg: &fmt::Arguments,
353353
file_line_col: &(&'static str, u32, u32)) -> ! {
354-
use fmt::Write;
355-
356354
// We do two allocations here, unfortunately. But (a) they're
357355
// required with the current scheme, and (b) we don't handle
358356
// panic + OOM properly anyway (see comment in begin_panic
359357
// below).
360358

361359
rust_panic_with_hook(&mut PanicPayload::new(msg), Some(msg), file_line_col);
360+
}
361+
362+
// NOTE(stage0) move into `continue_panic_fmt` on next stage0 update
363+
struct PanicPayload<'a> {
364+
inner: &'a fmt::Arguments<'a>,
365+
string: Option<String>,
366+
}
362367

363-
struct PanicPayload<'a> {
364-
inner: &'a fmt::Arguments<'a>,
365-
string: Option<String>,
368+
impl<'a> PanicPayload<'a> {
369+
fn new(inner: &'a fmt::Arguments<'a>) -> PanicPayload<'a> {
370+
PanicPayload { inner, string: None }
366371
}
367372

368-
impl<'a> PanicPayload<'a> {
369-
fn new(inner: &'a fmt::Arguments<'a>) -> PanicPayload<'a> {
370-
PanicPayload { inner, string: None }
371-
}
373+
fn fill(&mut self) -> &mut String {
374+
use fmt::Write;
372375

373-
fn fill(&mut self) -> &mut String {
374-
let inner = self.inner;
375-
self.string.get_or_insert_with(|| {
376-
let mut s = String::new();
377-
drop(s.write_fmt(*inner));
378-
s
379-
})
380-
}
376+
let inner = self.inner;
377+
self.string.get_or_insert_with(|| {
378+
let mut s = String::new();
379+
drop(s.write_fmt(*inner));
380+
s
381+
})
381382
}
383+
}
382384

383-
unsafe impl<'a> BoxMeUp for PanicPayload<'a> {
384-
fn box_me_up(&mut self) -> *mut (Any + Send) {
385-
let contents = mem::replace(self.fill(), String::new());
386-
Box::into_raw(Box::new(contents))
387-
}
385+
unsafe impl<'a> BoxMeUp for PanicPayload<'a> {
386+
fn box_me_up(&mut self) -> *mut (Any + Send) {
387+
let contents = mem::replace(self.fill(), String::new());
388+
Box::into_raw(Box::new(contents))
389+
}
388390

389-
fn get(&mut self) -> &(Any + Send) {
390-
self.fill()
391-
}
391+
fn get(&mut self) -> &(Any + Send) {
392+
self.fill()
392393
}
393394
}
394395

@@ -415,76 +416,26 @@ pub fn begin_panic_fmt(msg: &fmt::Arguments,
415416

416417
#[cfg(not(stage0))]
417418
fn continue_panic_fmt(info: &PanicInfo) -> ! {
418-
use fmt::Write;
419-
420419
// We do two allocations here, unfortunately. But (a) they're
421420
// required with the current scheme, and (b) we don't handle
422421
// panic + OOM properly anyway (see comment in begin_panic
423422
// below).
424423

425424
let loc = info.location().unwrap(); // The current implementation always returns Some
425+
let msg = info.message().unwrap(); // The current implementation always returns Some
426426
let file_line_col = (loc.file(), loc.line(), loc.column());
427427
rust_panic_with_hook(
428-
&mut PanicPayload::new(info.payload(), info.message()),
428+
&mut PanicPayload::new(msg),
429429
info.message(),
430430
&file_line_col);
431-
432-
struct PanicPayload<'a> {
433-
payload: &'a (Any + Send),
434-
msg: Option<&'a fmt::Arguments<'a>>,
435-
string: Option<String>,
436-
}
437-
438-
impl<'a> PanicPayload<'a> {
439-
fn new(payload: &'a (Any + Send), msg: Option<&'a fmt::Arguments<'a>>) -> PanicPayload<'a> {
440-
PanicPayload { payload, msg, string: None }
441-
}
442-
443-
fn fill(&mut self) -> Option<&mut String> {
444-
if let Some(msg) = self.msg.cloned() {
445-
Some(self.string.get_or_insert_with(|| {
446-
let mut s = String::new();
447-
drop(s.write_fmt(msg));
448-
s
449-
}))
450-
} else {
451-
None
452-
}
453-
}
454-
}
455-
456-
unsafe impl<'a> BoxMeUp for PanicPayload<'a> {
457-
fn box_me_up(&mut self) -> *mut (Any + Send) {
458-
if let Some(string) = self.fill() {
459-
let contents = mem::replace(string, String::new());
460-
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()))
465-
} else {
466-
// We can't go from &(Any+Send) to Box<Any+Send> so the payload is lost here
467-
struct NoPayload;
468-
Box::into_raw(Box::new(NoPayload))
469-
}
470-
}
471-
472-
fn get(&mut self) -> &(Any + Send) {
473-
if let Some(s) = self.fill() {
474-
s
475-
} else {
476-
self.payload
477-
}
478-
}
479-
}
480431
}
481432

482433
/// This is the entry point of panicking for panic!() and assert!().
483434
#[unstable(feature = "libstd_sys_internals",
484435
reason = "used by the panic! macro",
485436
issue = "0")]
486437
#[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible
487-
pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&str, u32, u32)) -> ! {
438+
pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u32)) -> ! {
488439
// Note that this should be the only allocation performed in this code path.
489440
// Currently this means that panic!() on OOM will invoke this code path,
490441
// but then again we're not really ready for panic on OOM anyway. If

0 commit comments

Comments
 (0)