1
Fork 0

core and std macros and panic internals use panic::Location::caller.

This commit is contained in:
Adam Perry 2019-12-07 08:37:08 -08:00
parent 1ed41b0720
commit eaccda009f
7 changed files with 35 additions and 37 deletions

View file

@ -1,19 +1,13 @@
#[doc(include = "panic.md")] #[doc(include = "panic.md")]
#[macro_export] #[macro_export]
#[allow_internal_unstable(core_panic, #[allow_internal_unstable(core_panic, track_caller)]
// FIXME(anp, eddyb) `core_intrinsics` is used here to allow calling
// the `caller_location` intrinsic, but once `#[track_caller]` is implemented,
// `panicking::{panic, panic_fmt}` can use that instead of a `Location` argument.
core_intrinsics,
const_caller_location,
)]
#[stable(feature = "core", since = "1.6.0")] #[stable(feature = "core", since = "1.6.0")]
macro_rules! panic { macro_rules! panic {
() => ( () => (
$crate::panic!("explicit panic") $crate::panic!("explicit panic")
); );
($msg:expr) => ( ($msg:expr) => (
$crate::panicking::panic($msg, $crate::intrinsics::caller_location()) $crate::panicking::panic($msg)
); );
($msg:expr,) => ( ($msg:expr,) => (
$crate::panic!($msg) $crate::panic!($msg)
@ -21,7 +15,7 @@ macro_rules! panic {
($fmt:expr, $($arg:tt)+) => ( ($fmt:expr, $($arg:tt)+) => (
$crate::panicking::panic_fmt( $crate::panicking::panic_fmt(
$crate::format_args!($fmt, $($arg)+), $crate::format_args!($fmt, $($arg)+),
$crate::intrinsics::caller_location(), $crate::panic::Location::caller(),
) )
); );
} }

View file

@ -36,8 +36,9 @@ use crate::panic::{Location, PanicInfo};
// never inline unless panic_immediate_abort to avoid code // never inline unless panic_immediate_abort to avoid code
// bloat at the call sites as much as possible // bloat at the call sites as much as possible
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
#[track_caller]
#[lang = "panic"] // needed by codegen for panic on overflow and other `Assert` MIR terminators #[lang = "panic"] // needed by codegen for panic on overflow and other `Assert` MIR terminators
pub fn panic(expr: &str, location: &Location<'_>) -> ! { pub fn panic(expr: &str) -> ! {
if cfg!(feature = "panic_immediate_abort") { if cfg!(feature = "panic_immediate_abort") {
unsafe { super::intrinsics::abort() } unsafe { super::intrinsics::abort() }
} }
@ -48,7 +49,7 @@ pub fn panic(expr: &str, location: &Location<'_>) -> ! {
// truncation and padding (even though none is used here). Using // truncation and padding (even though none is used here). Using
// Arguments::new_v1 may allow the compiler to omit Formatter::pad from the // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
// output binary, saving up to a few kilobytes. // output binary, saving up to a few kilobytes.
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), location) panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), Location::caller())
} }
#[cold] #[cold]

View file

@ -6,7 +6,7 @@ use syntax::ptr::P;
use syntax::source_map::{respan, Spanned}; use syntax::source_map::{respan, Spanned};
use syntax::symbol::{kw, sym, Symbol}; use syntax::symbol::{kw, sym, Symbol};
use rustc_span::{Pos, Span}; use rustc_span::Span;
impl<'a> ExtCtxt<'a> { impl<'a> ExtCtxt<'a> {
pub fn path(&self, span: Span, strs: Vec<ast::Ident>) -> ast::Path { pub fn path(&self, span: Span, strs: Vec<ast::Ident>) -> ast::Path {
@ -350,16 +350,10 @@ impl<'a> ExtCtxt<'a> {
} }
pub fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> { pub fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
let loc = self.source_map().lookup_char_pos(span.lo());
let expr_file = self.expr_str(span, Symbol::intern(&loc.file.name.to_string()));
let expr_line = self.expr_u32(span, loc.line as u32);
let expr_col = self.expr_u32(span, loc.col.to_usize() as u32 + 1);
let expr_loc_tuple = self.expr_tuple(span, vec![expr_file, expr_line, expr_col]);
let expr_loc_ptr = self.expr_addr_of(span, expr_loc_tuple);
self.expr_call_global( self.expr_call_global(
span, span,
[sym::std, sym::rt, sym::begin_panic].iter().map(|s| Ident::new(*s, span)).collect(), [sym::std, sym::rt, sym::begin_panic].iter().map(|s| Ident::new(*s, span)).collect(),
vec![self.expr_str(span, msg), expr_loc_ptr], vec![self.expr_str(span, msg)],
) )
} }

View file

@ -392,7 +392,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
throw_panic!(Panic { msg, file, line, col }) throw_panic!(Panic { msg, file, line, col })
} else if Some(def_id) == self.tcx.lang_items().begin_panic_fn() { } else if Some(def_id) == self.tcx.lang_items().begin_panic_fn() {
assert!(args.len() == 2); assert!(args.len() == 2);
// &'static str, &(&'static str, u32, u32) // &'static str, &core::panic::Location { &'static str, u32, u32 }
let msg = args[0]; let msg = args[0];
let place = self.deref_operand(args[1])?; let place = self.deref_operand(args[1])?;
let (file, line, col) = ( let (file, line, col) = (

View file

@ -306,6 +306,7 @@
#![feature(thread_local)] #![feature(thread_local)]
#![feature(toowned_clone_into)] #![feature(toowned_clone_into)]
#![feature(trace_macros)] #![feature(trace_macros)]
#![feature(track_caller)]
#![feature(try_reserve)] #![feature(try_reserve)]
#![feature(unboxed_closures)] #![feature(unboxed_closures)]
#![feature(untagged_unions)] #![feature(untagged_unions)]

View file

@ -4,6 +4,7 @@
//! library. Each macro is available for use when linking against the standard //! library. Each macro is available for use when linking against the standard
//! library. //! library.
#[cfg(bootstrap)]
#[doc(include = "../libcore/macros/panic.md")] #[doc(include = "../libcore/macros/panic.md")]
#[macro_export] #[macro_export]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
@ -19,8 +20,21 @@ macro_rules! panic {
$crate::panic!($msg) $crate::panic!($msg)
}); });
($fmt:expr, $($arg:tt)+) => ({ ($fmt:expr, $($arg:tt)+) => ({
$crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+), $crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+))
&($crate::file!(), $crate::line!(), $crate::column!())) });
}
#[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)+))
}); });
} }

View file

@ -313,17 +313,15 @@ pub fn panicking() -> bool {
#[cold] #[cold]
// If panic_immediate_abort, inline the abort call, // If panic_immediate_abort, inline the abort call,
// otherwise avoid inlining because of it is cold path. // 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(not(feature = "panic_immediate_abort"), inline(never))]
#[cfg_attr(feature = "panic_immediate_abort", inline)] #[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") { if cfg!(feature = "panic_immediate_abort") {
unsafe { intrinsics::abort() } unsafe { intrinsics::abort() }
} }
// Just package everything into a `PanicInfo` and continue like libcore panics. let info = PanicInfo::internal_constructor(Some(msg), Location::caller());
let (file, line, col) = *file_line_col;
let location = Location::internal_constructor(file, line, col);
let info = PanicInfo::internal_constructor(Some(msg), &location);
begin_panic_handler(&info) 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 loc = info.location().unwrap(); // The current implementation always returns Some
let msg = info.message().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(), loc);
rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), &file_line_col);
} }
/// This is the entry point of panicking for the non-format-string variants of /// 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 // bloat at the call sites as much as possible
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
#[cold] #[cold]
pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u32)) -> ! { #[track_caller]
pub fn begin_panic<M: Any + Send>(msg: M, #[cfg(bootstrap)] _: &(&str, u32, u32)) -> ! {
if cfg!(feature = "panic_immediate_abort") { if cfg!(feature = "panic_immediate_abort") {
unsafe { intrinsics::abort() } unsafe { intrinsics::abort() }
} }
@ -397,8 +395,7 @@ pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u3
// we do start doing this, then we should propagate this allocation to // 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 // be performed in the parent of this thread instead of the thread that's
// panicking. // panicking.
rust_panic_with_hook(&mut PanicPayload::new(msg), None, Location::caller());
rust_panic_with_hook(&mut PanicPayload::new(msg), None, file_line_col);
struct PanicPayload<A> { struct PanicPayload<A> {
inner: Option<A>, inner: Option<A>,
@ -436,10 +433,8 @@ pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u3
fn rust_panic_with_hook( fn rust_panic_with_hook(
payload: &mut dyn BoxMeUp, payload: &mut dyn BoxMeUp,
message: Option<&fmt::Arguments<'_>>, 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); let panics = update_panic_count(1);
// If this is the third nested call (e.g., panics == 2, this is 0-indexed), // 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 { 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(); HOOK_LOCK.read();
match HOOK { match HOOK {
// Some platforms (like wasm) know that printing to stderr won't ever actually // Some platforms (like wasm) know that printing to stderr won't ever actually