Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 18b046f

Browse files
authoredJun 16, 2020
Rollup merge of rust-lang#73373 - lzutao:bug-trackcaller, r=Amanieu
Use track caller for bug! macro
2 parents 5136489 + fe7456c commit 18b046f

File tree

6 files changed

+28
-39
lines changed

6 files changed

+28
-39
lines changed
 

‎src/libcore/panic.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ pub struct PanicInfo<'a> {
3939
impl<'a> PanicInfo<'a> {
4040
#[unstable(
4141
feature = "panic_internals",
42-
reason = "internal details of the implementation of the `panic!` \
43-
and related macros",
42+
reason = "internal details of the implementation of the `panic!` and related macros",
4443
issue = "none"
4544
)]
4645
#[doc(hidden)]
@@ -55,8 +54,7 @@ impl<'a> PanicInfo<'a> {
5554

5655
#[unstable(
5756
feature = "panic_internals",
58-
reason = "internal details of the implementation of the `panic!` \
59-
and related macros",
57+
reason = "internal details of the implementation of the `panic!` and related macros",
6058
issue = "none"
6159
)]
6260
#[doc(hidden)]
@@ -244,8 +242,7 @@ impl<'a> Location<'a> {
244242
impl<'a> Location<'a> {
245243
#![unstable(
246244
feature = "panic_internals",
247-
reason = "internal details of the implementation of the `panic!` \
248-
and related macros",
245+
reason = "internal details of the implementation of the `panic!` and related macros",
249246
issue = "none"
250247
)]
251248
#[doc(hidden)]

‎src/libcore/panicking.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
#![allow(dead_code, missing_docs)]
2323
#![unstable(
2424
feature = "core_panic",
25-
reason = "internal details of the implementation of the `panic!` \
26-
and related macros",
25+
reason = "internal details of the implementation of the `panic!` and related macros",
2726
issue = "none"
2827
)]
2928

‎src/librustc_middle/macros.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
#[macro_export]
22
macro_rules! bug {
3-
() => ( bug!("impossible case reached") );
4-
($($message:tt)*) => ({
5-
$crate::util::bug::bug_fmt(file!(), line!(), format_args!($($message)*))
6-
})
3+
() => ( $crate::bug!("impossible case reached") );
4+
($msg:expr) => ({ $crate::util::bug::bug_fmt(::std::format_args!($msg)) });
5+
($msg:expr,) => ({ $crate::bug!($msg) });
6+
($fmt:expr, $($arg:tt)+) => ({
7+
$crate::util::bug::bug_fmt(::std::format_args!($fmt, $($arg)+))
8+
});
79
}
810

911
#[macro_export]
1012
macro_rules! span_bug {
11-
($span:expr, $($message:tt)*) => ({
12-
$crate::util::bug::span_bug_fmt(file!(), line!(), $span, format_args!($($message)*))
13-
})
13+
($span:expr, $msg:expr) => ({ $crate::util::bug::span_bug_fmt($span, ::std::format_args!($msg)) });
14+
($span:expr, $msg:expr,) => ({ $crate::span_bug!($span, $msg) });
15+
($span:expr, $fmt:expr, $($arg:tt)+) => ({
16+
$crate::util::bug::span_bug_fmt($span, ::std::format_args!($fmt, $($arg)+))
17+
});
1418
}
1519

1620
///////////////////////////////////////////////////////////////////////////

‎src/librustc_middle/ty/context.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ fn validate_hir_id_for_typeck_tables(
189189
if hir_id.owner != hir_owner {
190190
ty::tls::with(|tcx| {
191191
bug!(
192-
"node {} with HirId::owner {:?} cannot be placed in \
193-
TypeckTables with hir_owner {:?}",
192+
"node {} with HirId::owner {:?} cannot be placed in TypeckTables with hir_owner {:?}",
194193
tcx.hir().node_to_string(hir_id),
195194
hir_id.owner,
196195
hir_owner

‎src/librustc_middle/util/bug.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,31 @@
33
use crate::ty::{tls, TyCtxt};
44
use rustc_span::{MultiSpan, Span};
55
use std::fmt;
6+
use std::panic::Location;
67

78
#[cold]
89
#[inline(never)]
9-
pub fn bug_fmt(file: &'static str, line: u32, args: fmt::Arguments<'_>) -> ! {
10+
#[track_caller]
11+
pub fn bug_fmt(args: fmt::Arguments<'_>) -> ! {
1012
// this wrapper mostly exists so I don't have to write a fully
1113
// qualified path of None::<Span> inside the bug!() macro definition
12-
opt_span_bug_fmt(file, line, None::<Span>, args);
14+
opt_span_bug_fmt(None::<Span>, args, Location::caller());
1315
}
1416

1517
#[cold]
1618
#[inline(never)]
17-
pub fn span_bug_fmt<S: Into<MultiSpan>>(
18-
file: &'static str,
19-
line: u32,
20-
span: S,
21-
args: fmt::Arguments<'_>,
22-
) -> ! {
23-
opt_span_bug_fmt(file, line, Some(span), args);
19+
#[track_caller]
20+
pub fn span_bug_fmt<S: Into<MultiSpan>>(span: S, args: fmt::Arguments<'_>) -> ! {
21+
opt_span_bug_fmt(Some(span), args, Location::caller());
2422
}
2523

2624
fn opt_span_bug_fmt<S: Into<MultiSpan>>(
27-
file: &'static str,
28-
line: u32,
2925
span: Option<S>,
3026
args: fmt::Arguments<'_>,
27+
location: &Location<'_>,
3128
) -> ! {
3229
tls::with_opt(move |tcx| {
33-
let msg = format!("{}:{}: {}", file, line, args);
30+
let msg = format!("{}: {}", location, args);
3431
match (tcx, span) {
3532
(Some(tcx), Some(span)) => tcx.sess.diagnostic().span_bug(span, &msg),
3633
(Some(tcx), None) => tcx.sess.diagnostic().bug(&msg),

‎src/libstd/panicking.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ fn default_hook(info: &PanicInfo<'_>) {
201201
if FIRST_PANIC.swap(false, Ordering::SeqCst) {
202202
let _ = writeln!(
203203
err,
204-
"note: run with `RUST_BACKTRACE=1` \
205-
environment variable to display a backtrace"
204+
"note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace"
206205
);
207206
}
208207
}
@@ -454,10 +453,7 @@ fn rust_panic_with_hook(
454453
// process real quickly as we don't want to try calling it again as it'll
455454
// probably just panic again.
456455
if panics > 2 {
457-
util::dumb_print(format_args!(
458-
"thread panicked while processing \
459-
panic. aborting.\n"
460-
));
456+
util::dumb_print(format_args!("thread panicked while processing panic. aborting.\n"));
461457
intrinsics::abort()
462458
}
463459

@@ -489,10 +485,7 @@ fn rust_panic_with_hook(
489485
// have limited options. Currently our preference is to
490486
// just abort. In the future we may consider resuming
491487
// unwinding or otherwise exiting the thread cleanly.
492-
util::dumb_print(format_args!(
493-
"thread panicked while panicking. \
494-
aborting.\n"
495-
));
488+
util::dumb_print(format_args!("thread panicked while panicking. aborting.\n"));
496489
intrinsics::abort()
497490
}
498491

0 commit comments

Comments
 (0)
Please sign in to comment.