Skip to content

Commit fe7456c

Browse files
committed
Use track caller for bug! macro
1 parent 64a6de2 commit fe7456c

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

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/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),

0 commit comments

Comments
 (0)