Rollup merge of #73373 - lzutao:bug-trackcaller, r=Amanieu

Use track caller for bug! macro
This commit is contained in:
Dylan DPC 2020-06-16 15:08:42 +02:00 committed by GitHub
commit 94105c2da3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 39 deletions

View file

@ -39,8 +39,7 @@ pub struct PanicInfo<'a> {
impl<'a> PanicInfo<'a> { impl<'a> PanicInfo<'a> {
#[unstable( #[unstable(
feature = "panic_internals", feature = "panic_internals",
reason = "internal details of the implementation of the `panic!` \ reason = "internal details of the implementation of the `panic!` and related macros",
and related macros",
issue = "none" issue = "none"
)] )]
#[doc(hidden)] #[doc(hidden)]
@ -55,8 +54,7 @@ impl<'a> PanicInfo<'a> {
#[unstable( #[unstable(
feature = "panic_internals", feature = "panic_internals",
reason = "internal details of the implementation of the `panic!` \ reason = "internal details of the implementation of the `panic!` and related macros",
and related macros",
issue = "none" issue = "none"
)] )]
#[doc(hidden)] #[doc(hidden)]
@ -244,8 +242,7 @@ impl<'a> Location<'a> {
impl<'a> Location<'a> { impl<'a> Location<'a> {
#![unstable( #![unstable(
feature = "panic_internals", feature = "panic_internals",
reason = "internal details of the implementation of the `panic!` \ reason = "internal details of the implementation of the `panic!` and related macros",
and related macros",
issue = "none" issue = "none"
)] )]
#[doc(hidden)] #[doc(hidden)]

View file

@ -22,8 +22,7 @@
#![allow(dead_code, missing_docs)] #![allow(dead_code, missing_docs)]
#![unstable( #![unstable(
feature = "core_panic", feature = "core_panic",
reason = "internal details of the implementation of the `panic!` \ reason = "internal details of the implementation of the `panic!` and related macros",
and related macros",
issue = "none" issue = "none"
)] )]

View file

@ -1,16 +1,20 @@
#[macro_export] #[macro_export]
macro_rules! bug { macro_rules! bug {
() => ( bug!("impossible case reached") ); () => ( $crate::bug!("impossible case reached") );
($($message:tt)*) => ({ ($msg:expr) => ({ $crate::util::bug::bug_fmt(::std::format_args!($msg)) });
$crate::util::bug::bug_fmt(file!(), line!(), format_args!($($message)*)) ($msg:expr,) => ({ $crate::bug!($msg) });
}) ($fmt:expr, $($arg:tt)+) => ({
$crate::util::bug::bug_fmt(::std::format_args!($fmt, $($arg)+))
});
} }
#[macro_export] #[macro_export]
macro_rules! span_bug { macro_rules! span_bug {
($span:expr, $($message:tt)*) => ({ ($span:expr, $msg:expr) => ({ $crate::util::bug::span_bug_fmt($span, ::std::format_args!($msg)) });
$crate::util::bug::span_bug_fmt(file!(), line!(), $span, format_args!($($message)*)) ($span:expr, $msg:expr,) => ({ $crate::span_bug!($span, $msg) });
}) ($span:expr, $fmt:expr, $($arg:tt)+) => ({
$crate::util::bug::span_bug_fmt($span, ::std::format_args!($fmt, $($arg)+))
});
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////

View file

@ -189,8 +189,7 @@ fn validate_hir_id_for_typeck_tables(
if hir_id.owner != hir_owner { if hir_id.owner != hir_owner {
ty::tls::with(|tcx| { ty::tls::with(|tcx| {
bug!( bug!(
"node {} with HirId::owner {:?} cannot be placed in \ "node {} with HirId::owner {:?} cannot be placed in TypeckTables with hir_owner {:?}",
TypeckTables with hir_owner {:?}",
tcx.hir().node_to_string(hir_id), tcx.hir().node_to_string(hir_id),
hir_id.owner, hir_id.owner,
hir_owner hir_owner

View file

@ -3,34 +3,31 @@
use crate::ty::{tls, TyCtxt}; use crate::ty::{tls, TyCtxt};
use rustc_span::{MultiSpan, Span}; use rustc_span::{MultiSpan, Span};
use std::fmt; use std::fmt;
use std::panic::Location;
#[cold] #[cold]
#[inline(never)] #[inline(never)]
pub fn bug_fmt(file: &'static str, line: u32, args: fmt::Arguments<'_>) -> ! { #[track_caller]
pub fn bug_fmt(args: fmt::Arguments<'_>) -> ! {
// this wrapper mostly exists so I don't have to write a fully // this wrapper mostly exists so I don't have to write a fully
// qualified path of None::<Span> inside the bug!() macro definition // qualified path of None::<Span> inside the bug!() macro definition
opt_span_bug_fmt(file, line, None::<Span>, args); opt_span_bug_fmt(None::<Span>, args, Location::caller());
} }
#[cold] #[cold]
#[inline(never)] #[inline(never)]
pub fn span_bug_fmt<S: Into<MultiSpan>>( #[track_caller]
file: &'static str, pub fn span_bug_fmt<S: Into<MultiSpan>>(span: S, args: fmt::Arguments<'_>) -> ! {
line: u32, opt_span_bug_fmt(Some(span), args, Location::caller());
span: S,
args: fmt::Arguments<'_>,
) -> ! {
opt_span_bug_fmt(file, line, Some(span), args);
} }
fn opt_span_bug_fmt<S: Into<MultiSpan>>( fn opt_span_bug_fmt<S: Into<MultiSpan>>(
file: &'static str,
line: u32,
span: Option<S>, span: Option<S>,
args: fmt::Arguments<'_>, args: fmt::Arguments<'_>,
location: &Location<'_>,
) -> ! { ) -> ! {
tls::with_opt(move |tcx| { tls::with_opt(move |tcx| {
let msg = format!("{}:{}: {}", file, line, args); let msg = format!("{}: {}", location, args);
match (tcx, span) { match (tcx, span) {
(Some(tcx), Some(span)) => tcx.sess.diagnostic().span_bug(span, &msg), (Some(tcx), Some(span)) => tcx.sess.diagnostic().span_bug(span, &msg),
(Some(tcx), None) => tcx.sess.diagnostic().bug(&msg), (Some(tcx), None) => tcx.sess.diagnostic().bug(&msg),

View file

@ -201,8 +201,7 @@ fn default_hook(info: &PanicInfo<'_>) {
if FIRST_PANIC.swap(false, Ordering::SeqCst) { if FIRST_PANIC.swap(false, Ordering::SeqCst) {
let _ = writeln!( let _ = writeln!(
err, err,
"note: run with `RUST_BACKTRACE=1` \ "note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace"
environment variable to display a backtrace"
); );
} }
} }
@ -454,10 +453,7 @@ fn rust_panic_with_hook(
// process real quickly as we don't want to try calling it again as it'll // process real quickly as we don't want to try calling it again as it'll
// probably just panic again. // probably just panic again.
if panics > 2 { if panics > 2 {
util::dumb_print(format_args!( util::dumb_print(format_args!("thread panicked while processing panic. aborting.\n"));
"thread panicked while processing \
panic. aborting.\n"
));
intrinsics::abort() intrinsics::abort()
} }
@ -489,10 +485,7 @@ fn rust_panic_with_hook(
// have limited options. Currently our preference is to // have limited options. Currently our preference is to
// just abort. In the future we may consider resuming // just abort. In the future we may consider resuming
// unwinding or otherwise exiting the thread cleanly. // unwinding or otherwise exiting the thread cleanly.
util::dumb_print(format_args!( util::dumb_print(format_args!("thread panicked while panicking. aborting.\n"));
"thread panicked while panicking. \
aborting.\n"
));
intrinsics::abort() intrinsics::abort()
} }