diff options
| author | Adam Perry <adam.n.perry@gmail.com> | 2019-12-07 08:37:08 -0800 |
|---|---|---|
| committer | Adam Perry <adam.n.perry@gmail.com> | 2020-01-04 10:02:17 -0800 |
| commit | eaccda009f5891c67e554b31cfad4a52738f9b91 (patch) | |
| tree | 8bfa146ae2a2e7210357d4956aab42213d8216ff | |
| parent | 1ed41b072093fe7cccd232f9a2964c5fb6ab9f60 (diff) | |
| download | rust-eaccda009f5891c67e554b31cfad4a52738f9b91.tar.gz rust-eaccda009f5891c67e554b31cfad4a52738f9b91.zip | |
core and std macros and panic internals use panic::Location::caller.
| -rw-r--r-- | src/libcore/macros/mod.rs | 12 | ||||
| -rw-r--r-- | src/libcore/panicking.rs | 5 | ||||
| -rw-r--r-- | src/librustc_expand/build.rs | 10 | ||||
| -rw-r--r-- | src/librustc_mir/interpret/intrinsics.rs | 2 | ||||
| -rw-r--r-- | src/libstd/lib.rs | 1 | ||||
| -rw-r--r-- | src/libstd/macros.rs | 18 | ||||
| -rw-r--r-- | src/libstd/panicking.rs | 24 |
7 files changed, 35 insertions, 37 deletions
diff --git a/src/libcore/macros/mod.rs b/src/libcore/macros/mod.rs index 9a52823a454..0eb9e194236 100644 --- a/src/libcore/macros/mod.rs +++ b/src/libcore/macros/mod.rs @@ -1,19 +1,13 @@ #[doc(include = "panic.md")] #[macro_export] -#[allow_internal_unstable(core_panic, - // 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, -)] +#[allow_internal_unstable(core_panic, track_caller)] #[stable(feature = "core", since = "1.6.0")] macro_rules! panic { () => ( $crate::panic!("explicit panic") ); ($msg:expr) => ( - $crate::panicking::panic($msg, $crate::intrinsics::caller_location()) + $crate::panicking::panic($msg) ); ($msg:expr,) => ( $crate::panic!($msg) @@ -21,7 +15,7 @@ macro_rules! panic { ($fmt:expr, $($arg:tt)+) => ( $crate::panicking::panic_fmt( $crate::format_args!($fmt, $($arg)+), - $crate::intrinsics::caller_location(), + $crate::panic::Location::caller(), ) ); } diff --git a/src/libcore/panicking.rs b/src/libcore/panicking.rs index 7ebb72e3ce7..61b764f2d62 100644 --- a/src/libcore/panicking.rs +++ b/src/libcore/panicking.rs @@ -36,8 +36,9 @@ use crate::panic::{Location, PanicInfo}; // never inline unless panic_immediate_abort to avoid code // bloat at the call sites as much as possible #[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 -pub fn panic(expr: &str, location: &Location<'_>) -> ! { +pub fn panic(expr: &str) -> ! { if cfg!(feature = "panic_immediate_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 // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the // 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] diff --git a/src/librustc_expand/build.rs b/src/librustc_expand/build.rs index 3375efe41f1..868f6203001 100644 --- a/src/librustc_expand/build.rs +++ b/src/librustc_expand/build.rs @@ -6,7 +6,7 @@ use syntax::ptr::P; use syntax::source_map::{respan, Spanned}; use syntax::symbol::{kw, sym, Symbol}; -use rustc_span::{Pos, Span}; +use rustc_span::Span; impl<'a> ExtCtxt<'a> { 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> { - 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( span, [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)], ) } diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs index cc38cbbac8d..256f7f2ab38 100644 --- a/src/librustc_mir/interpret/intrinsics.rs +++ b/src/librustc_mir/interpret/intrinsics.rs @@ -392,7 +392,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { throw_panic!(Panic { msg, file, line, col }) } else if Some(def_id) == self.tcx.lang_items().begin_panic_fn() { assert!(args.len() == 2); - // &'static str, &(&'static str, u32, u32) + // &'static str, &core::panic::Location { &'static str, u32, u32 } let msg = args[0]; let place = self.deref_operand(args[1])?; let (file, line, col) = ( 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<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") { 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 // 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<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( 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 |
