From eaccda009f5891c67e554b31cfad4a52738f9b91 Mon Sep 17 00:00:00 2001 From: Adam Perry Date: Sat, 7 Dec 2019 08:37:08 -0800 Subject: core and std macros and panic internals use panic::Location::caller. --- src/libstd/lib.rs | 1 + src/libstd/macros.rs | 18 ++++++++++++++++-- src/libstd/panicking.rs | 24 +++++++++--------------- 3 files changed, 26 insertions(+), 17 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 930bf397bc4..23f82d7c211 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -306,6 +306,7 @@ #![feature(thread_local)] #![feature(toowned_clone_into)] #![feature(trace_macros)] +#![feature(track_caller)] #![feature(try_reserve)] #![feature(unboxed_closures)] #![feature(untagged_unions)] diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 11850a1b5fc..18fb0f87688 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -4,6 +4,7 @@ //! library. Each macro is available for use when linking against the standard //! library. +#[cfg(bootstrap)] #[doc(include = "../libcore/macros/panic.md")] #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] @@ -19,8 +20,21 @@ macro_rules! panic { $crate::panic!($msg) }); ($fmt:expr, $($arg:tt)+) => ({ - $crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+), - &($crate::file!(), $crate::line!(), $crate::column!())) + $crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+)) + }); +} + +#[cfg(not(bootstrap))] +#[doc(include = "../libcore/macros/panic.md")] +#[macro_export] +#[stable(feature = "rust1", since = "1.0.0")] +#[allow_internal_unstable(libstd_sys_internals)] +macro_rules! panic { + () => ({ $crate::panic!("explicit panic") }); + ($msg:expr) => ({ $crate::rt::begin_panic($msg) }); + ($msg:expr,) => ({ $crate::panic!($msg) }); + ($fmt:expr, $($arg:tt)+) => ({ + $crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+)) }); } diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs index 43230d7a2c7..e3ce7a33a6f 100644 --- a/src/libstd/panicking.rs +++ b/src/libstd/panicking.rs @@ -313,17 +313,15 @@ pub fn panicking() -> bool { #[cold] // If panic_immediate_abort, inline the abort call, // otherwise avoid inlining because of it is cold path. +#[cfg_attr(not(feature = "panic_immediate_abort"), track_caller)] #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] #[cfg_attr(feature = "panic_immediate_abort", inline)] -pub fn begin_panic_fmt(msg: &fmt::Arguments<'_>, file_line_col: &(&'static str, u32, u32)) -> ! { +pub fn begin_panic_fmt(msg: &fmt::Arguments<'_>) -> ! { if cfg!(feature = "panic_immediate_abort") { unsafe { intrinsics::abort() } } - // Just package everything into a `PanicInfo` and continue like libcore panics. - let (file, line, col) = *file_line_col; - let location = Location::internal_constructor(file, line, col); - let info = PanicInfo::internal_constructor(Some(msg), &location); + let info = PanicInfo::internal_constructor(Some(msg), Location::caller()); begin_panic_handler(&info) } @@ -372,8 +370,7 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! { let loc = info.location().unwrap(); // The current implementation always returns Some let msg = info.message().unwrap(); // The current implementation always returns Some - let file_line_col = (loc.file(), loc.line(), loc.column()); - rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), &file_line_col); + rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), loc); } /// This is the entry point of panicking for the non-format-string variants of @@ -386,7 +383,8 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! { // bloat at the call sites as much as possible #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] #[cold] -pub fn begin_panic(msg: M, file_line_col: &(&'static str, u32, u32)) -> ! { +#[track_caller] +pub fn begin_panic(msg: M, #[cfg(bootstrap)] _: &(&str, u32, u32)) -> ! { if cfg!(feature = "panic_immediate_abort") { unsafe { intrinsics::abort() } } @@ -397,8 +395,7 @@ pub fn begin_panic(msg: M, file_line_col: &(&'static str, u32, u3 // we do start doing this, then we should propagate this allocation to // be performed in the parent of this thread instead of the thread that's // panicking. - - rust_panic_with_hook(&mut PanicPayload::new(msg), None, file_line_col); + rust_panic_with_hook(&mut PanicPayload::new(msg), None, Location::caller()); struct PanicPayload { inner: Option, @@ -436,10 +433,8 @@ pub fn begin_panic(msg: M, file_line_col: &(&'static str, u32, u3 fn rust_panic_with_hook( payload: &mut dyn BoxMeUp, message: Option<&fmt::Arguments<'_>>, - file_line_col: &(&str, u32, u32), + location: &Location<'_>, ) -> ! { - let (file, line, col) = *file_line_col; - let panics = update_panic_count(1); // If this is the third nested call (e.g., panics == 2, this is 0-indexed), @@ -456,8 +451,7 @@ fn rust_panic_with_hook( } unsafe { - let location = Location::internal_constructor(file, line, col); - let mut info = PanicInfo::internal_constructor(message, &location); + let mut info = PanicInfo::internal_constructor(message, location); HOOK_LOCK.read(); match HOOK { // Some platforms (like wasm) know that printing to stderr won't ever actually -- cgit 1.4.1-3-g733a5 From b76a5be18f69b79ddad8a6b72faf8ae9f2bb5e6d Mon Sep 17 00:00:00 2001 From: Adam Perry Date: Sat, 4 Jan 2020 00:49:18 -0800 Subject: Clean up comments in panicking infra. --- src/libstd/panicking.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs index e3ce7a33a6f..599ccc809be 100644 --- a/src/libstd/panicking.rs +++ b/src/libstd/panicking.rs @@ -354,6 +354,9 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! { unsafe impl<'a> BoxMeUp for PanicPayload<'a> { fn take_box(&mut self) -> *mut (dyn Any + Send) { + // We do two allocations here, unfortunately. But (a) they're required with the current + // scheme, and (b) we don't handle panic + OOM properly anyway (see comment in + // begin_panic below). let contents = mem::take(self.fill()); Box::into_raw(Box::new(contents)) } @@ -363,11 +366,6 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! { } } - // We do two allocations here, unfortunately. But (a) they're - // required with the current scheme, and (b) we don't handle - // panic + OOM properly anyway (see comment in begin_panic - // below). - let loc = info.location().unwrap(); // The current implementation always returns Some let msg = info.message().unwrap(); // The current implementation always returns Some rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), loc); @@ -389,12 +387,6 @@ pub fn begin_panic(msg: M, #[cfg(bootstrap)] _: &(&str, u32, u32) unsafe { intrinsics::abort() } } - // Note that this should be the only allocation performed in this code path. - // Currently this means that panic!() on OOM will invoke this code path, - // but then again we're not really ready for panic on OOM anyway. If - // we do start doing this, then we should propagate this allocation to - // be performed in the parent of this thread instead of the thread that's - // panicking. rust_panic_with_hook(&mut PanicPayload::new(msg), None, Location::caller()); struct PanicPayload { @@ -409,6 +401,11 @@ pub fn begin_panic(msg: M, #[cfg(bootstrap)] _: &(&str, u32, u32) unsafe impl BoxMeUp for PanicPayload { fn take_box(&mut self) -> *mut (dyn Any + Send) { + // Note that this should be the only allocation performed in this code path. Currently + // this means that panic!() on OOM will invoke this code path, but then again we're not + // really ready for panic on OOM anyway. If we do start doing this, then we should + // propagate this allocation to be performed in the parent of this thread instead of the + // thread that's panicking. let data = match self.inner.take() { Some(a) => Box::new(a) as Box, None => process::abort(), -- cgit 1.4.1-3-g733a5