Skip to content

Commit eaccda0

Browse files
committed
core and std macros and panic internals use panic::Location::caller.
1 parent 1ed41b0 commit eaccda0

File tree

7 files changed

+35
-37
lines changed

7 files changed

+35
-37
lines changed

src/libcore/macros/mod.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
#[doc(include = "panic.md")]
22
#[macro_export]
3-
#[allow_internal_unstable(core_panic,
4-
// FIXME(anp, eddyb) `core_intrinsics` is used here to allow calling
5-
// the `caller_location` intrinsic, but once `#[track_caller]` is implemented,
6-
// `panicking::{panic, panic_fmt}` can use that instead of a `Location` argument.
7-
core_intrinsics,
8-
const_caller_location,
9-
)]
3+
#[allow_internal_unstable(core_panic, track_caller)]
104
#[stable(feature = "core", since = "1.6.0")]
115
macro_rules! panic {
126
() => (
137
$crate::panic!("explicit panic")
148
);
159
($msg:expr) => (
16-
$crate::panicking::panic($msg, $crate::intrinsics::caller_location())
10+
$crate::panicking::panic($msg)
1711
);
1812
($msg:expr,) => (
1913
$crate::panic!($msg)
2014
);
2115
($fmt:expr, $($arg:tt)+) => (
2216
$crate::panicking::panic_fmt(
2317
$crate::format_args!($fmt, $($arg)+),
24-
$crate::intrinsics::caller_location(),
18+
$crate::panic::Location::caller(),
2519
)
2620
);
2721
}

src/libcore/panicking.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ use crate::panic::{Location, PanicInfo};
3636
// never inline unless panic_immediate_abort to avoid code
3737
// bloat at the call sites as much as possible
3838
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
39+
#[track_caller]
3940
#[lang = "panic"] // needed by codegen for panic on overflow and other `Assert` MIR terminators
40-
pub fn panic(expr: &str, location: &Location<'_>) -> ! {
41+
pub fn panic(expr: &str) -> ! {
4142
if cfg!(feature = "panic_immediate_abort") {
4243
unsafe { super::intrinsics::abort() }
4344
}
@@ -48,7 +49,7 @@ pub fn panic(expr: &str, location: &Location<'_>) -> ! {
4849
// truncation and padding (even though none is used here). Using
4950
// Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
5051
// output binary, saving up to a few kilobytes.
51-
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), location)
52+
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), Location::caller())
5253
}
5354

5455
#[cold]

src/librustc_expand/build.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use syntax::ptr::P;
66
use syntax::source_map::{respan, Spanned};
77
use syntax::symbol::{kw, sym, Symbol};
88

9-
use rustc_span::{Pos, Span};
9+
use rustc_span::Span;
1010

1111
impl<'a> ExtCtxt<'a> {
1212
pub fn path(&self, span: Span, strs: Vec<ast::Ident>) -> ast::Path {
@@ -350,16 +350,10 @@ impl<'a> ExtCtxt<'a> {
350350
}
351351

352352
pub fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
353-
let loc = self.source_map().lookup_char_pos(span.lo());
354-
let expr_file = self.expr_str(span, Symbol::intern(&loc.file.name.to_string()));
355-
let expr_line = self.expr_u32(span, loc.line as u32);
356-
let expr_col = self.expr_u32(span, loc.col.to_usize() as u32 + 1);
357-
let expr_loc_tuple = self.expr_tuple(span, vec![expr_file, expr_line, expr_col]);
358-
let expr_loc_ptr = self.expr_addr_of(span, expr_loc_tuple);
359353
self.expr_call_global(
360354
span,
361355
[sym::std, sym::rt, sym::begin_panic].iter().map(|s| Ident::new(*s, span)).collect(),
362-
vec![self.expr_str(span, msg), expr_loc_ptr],
356+
vec![self.expr_str(span, msg)],
363357
)
364358
}
365359

src/librustc_mir/interpret/intrinsics.rs

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

src/libstd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@
306306
#![feature(thread_local)]
307307
#![feature(toowned_clone_into)]
308308
#![feature(trace_macros)]
309+
#![feature(track_caller)]
309310
#![feature(try_reserve)]
310311
#![feature(unboxed_closures)]
311312
#![feature(untagged_unions)]

src/libstd/macros.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! library. Each macro is available for use when linking against the standard
55
//! library.
66
7+
#[cfg(bootstrap)]
78
#[doc(include = "../libcore/macros/panic.md")]
89
#[macro_export]
910
#[stable(feature = "rust1", since = "1.0.0")]
@@ -19,8 +20,21 @@ macro_rules! panic {
1920
$crate::panic!($msg)
2021
});
2122
($fmt:expr, $($arg:tt)+) => ({
22-
$crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+),
23-
&($crate::file!(), $crate::line!(), $crate::column!()))
23+
$crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+))
24+
});
25+
}
26+
27+
#[cfg(not(bootstrap))]
28+
#[doc(include = "../libcore/macros/panic.md")]
29+
#[macro_export]
30+
#[stable(feature = "rust1", since = "1.0.0")]
31+
#[allow_internal_unstable(libstd_sys_internals)]
32+
macro_rules! panic {
33+
() => ({ $crate::panic!("explicit panic") });
34+
($msg:expr) => ({ $crate::rt::begin_panic($msg) });
35+
($msg:expr,) => ({ $crate::panic!($msg) });
36+
($fmt:expr, $($arg:tt)+) => ({
37+
$crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+))
2438
});
2539
}
2640

src/libstd/panicking.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,15 @@ pub fn panicking() -> bool {
313313
#[cold]
314314
// If panic_immediate_abort, inline the abort call,
315315
// otherwise avoid inlining because of it is cold path.
316+
#[cfg_attr(not(feature = "panic_immediate_abort"), track_caller)]
316317
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
317318
#[cfg_attr(feature = "panic_immediate_abort", inline)]
318-
pub fn begin_panic_fmt(msg: &fmt::Arguments<'_>, file_line_col: &(&'static str, u32, u32)) -> ! {
319+
pub fn begin_panic_fmt(msg: &fmt::Arguments<'_>) -> ! {
319320
if cfg!(feature = "panic_immediate_abort") {
320321
unsafe { intrinsics::abort() }
321322
}
322323

323-
// Just package everything into a `PanicInfo` and continue like libcore panics.
324-
let (file, line, col) = *file_line_col;
325-
let location = Location::internal_constructor(file, line, col);
326-
let info = PanicInfo::internal_constructor(Some(msg), &location);
324+
let info = PanicInfo::internal_constructor(Some(msg), Location::caller());
327325
begin_panic_handler(&info)
328326
}
329327

@@ -372,8 +370,7 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
372370

373371
let loc = info.location().unwrap(); // The current implementation always returns Some
374372
let msg = info.message().unwrap(); // The current implementation always returns Some
375-
let file_line_col = (loc.file(), loc.line(), loc.column());
376-
rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), &file_line_col);
373+
rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), loc);
377374
}
378375

379376
/// 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<'_>) -> ! {
386383
// bloat at the call sites as much as possible
387384
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
388385
#[cold]
389-
pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u32)) -> ! {
386+
#[track_caller]
387+
pub fn begin_panic<M: Any + Send>(msg: M, #[cfg(bootstrap)] _: &(&str, u32, u32)) -> ! {
390388
if cfg!(feature = "panic_immediate_abort") {
391389
unsafe { intrinsics::abort() }
392390
}
@@ -397,8 +395,7 @@ pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u3
397395
// we do start doing this, then we should propagate this allocation to
398396
// be performed in the parent of this thread instead of the thread that's
399397
// panicking.
400-
401-
rust_panic_with_hook(&mut PanicPayload::new(msg), None, file_line_col);
398+
rust_panic_with_hook(&mut PanicPayload::new(msg), None, Location::caller());
402399

403400
struct PanicPayload<A> {
404401
inner: Option<A>,
@@ -436,10 +433,8 @@ pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u3
436433
fn rust_panic_with_hook(
437434
payload: &mut dyn BoxMeUp,
438435
message: Option<&fmt::Arguments<'_>>,
439-
file_line_col: &(&str, u32, u32),
436+
location: &Location<'_>,
440437
) -> ! {
441-
let (file, line, col) = *file_line_col;
442-
443438
let panics = update_panic_count(1);
444439

445440
// If this is the third nested call (e.g., panics == 2, this is 0-indexed),
@@ -456,8 +451,7 @@ fn rust_panic_with_hook(
456451
}
457452

458453
unsafe {
459-
let location = Location::internal_constructor(file, line, col);
460-
let mut info = PanicInfo::internal_constructor(message, &location);
454+
let mut info = PanicInfo::internal_constructor(message, location);
461455
HOOK_LOCK.read();
462456
match HOOK {
463457
// Some platforms (like wasm) know that printing to stderr won't ever actually

0 commit comments

Comments
 (0)