Skip to content

Commit cce055d

Browse files
authored
Rollup merge of #67137 - anp:tracked-panic-internals, r=eddyb
libstd uses `core::panic::Location` where possible. cc @eddyb
2 parents cd8377d + 27b25eb commit cce055d

File tree

17 files changed

+90
-94
lines changed

17 files changed

+90
-94
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::ast::{self, AttrVec, BlockCheckMode, Expr, Ident, PatKind, UnOp};
66
use syntax::attr;
77
use syntax::ptr::P;
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/const_eval/machine.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
177177

178178
fn find_mir_or_eval_fn(
179179
ecx: &mut InterpCx<'mir, 'tcx, Self>,
180+
span: Span,
180181
instance: ty::Instance<'tcx>,
181182
args: &[OpTy<'tcx>],
182183
ret: Option<(PlaceTy<'tcx>, mir::BasicBlock)>,
@@ -199,7 +200,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
199200
// Some functions we support even if they are non-const -- but avoid testing
200201
// that for const fn! We certainly do *not* want to actually call the fn
201202
// though, so be sure we return here.
202-
return if ecx.hook_panic_fn(instance, args, ret)? {
203+
return if ecx.hook_panic_fn(span, instance, args)? {
203204
Ok(None)
204205
} else {
205206
throw_unsup_format!("calling non-const function `{}`", instance)

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -366,47 +366,21 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
366366
/// Returns `true` if an intercept happened.
367367
pub fn hook_panic_fn(
368368
&mut self,
369+
span: Span,
369370
instance: ty::Instance<'tcx>,
370371
args: &[OpTy<'tcx, M::PointerTag>],
371-
_ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
372372
) -> InterpResult<'tcx, bool> {
373373
let def_id = instance.def_id();
374-
if Some(def_id) == self.tcx.lang_items().panic_fn() {
375-
// &'static str, &core::panic::Location { &'static str, u32, u32 }
376-
assert!(args.len() == 2);
374+
if Some(def_id) == self.tcx.lang_items().panic_fn()
375+
|| Some(def_id) == self.tcx.lang_items().begin_panic_fn()
376+
{
377+
// &'static str
378+
assert!(args.len() == 1);
377379

378380
let msg_place = self.deref_operand(args[0])?;
379381
let msg = Symbol::intern(self.read_str(msg_place)?);
380-
381-
let location = self.deref_operand(args[1])?;
382-
let (file, line, col) = (
383-
self.mplace_field(location, 0)?,
384-
self.mplace_field(location, 1)?,
385-
self.mplace_field(location, 2)?,
386-
);
387-
388-
let file_place = self.deref_operand(file.into())?;
389-
let file = Symbol::intern(self.read_str(file_place)?);
390-
let line = self.read_scalar(line.into())?.to_u32()?;
391-
let col = self.read_scalar(col.into())?.to_u32()?;
392-
throw_panic!(Panic { msg, file, line, col })
393-
} else if Some(def_id) == self.tcx.lang_items().begin_panic_fn() {
394-
assert!(args.len() == 2);
395-
// &'static str, &(&'static str, u32, u32)
396-
let msg = args[0];
397-
let place = self.deref_operand(args[1])?;
398-
let (file, line, col) = (
399-
self.mplace_field(place, 0)?,
400-
self.mplace_field(place, 1)?,
401-
self.mplace_field(place, 2)?,
402-
);
403-
404-
let msg_place = self.deref_operand(msg.into())?;
405-
let msg = Symbol::intern(self.read_str(msg_place)?);
406-
let file_place = self.deref_operand(file.into())?;
407-
let file = Symbol::intern(self.read_str(file_place)?);
408-
let line = self.read_scalar(line.into())?.to_u32()?;
409-
let col = self.read_scalar(col.into())?.to_u32()?;
382+
let span = self.find_closest_untracked_caller_location().unwrap_or(span);
383+
let (file, line, col) = self.location_triple_for_span(span);
410384
throw_panic!(Panic { msg, file, line, col })
411385
} else {
412386
return Ok(false);

src/librustc_mir/interpret/intrinsics/caller_location.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use crate::interpret::{
99
};
1010

1111
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
12-
/// Walks up the callstack from the intrinsic's callsite, searching for the first frame which is
13-
/// not `#[track_caller]`.
12+
/// Walks up the callstack from the intrinsic's callsite, searching for the first callsite in a
13+
/// frame which is not `#[track_caller]`. If the first frame found lacks `#[track_caller]`, then
14+
/// `None` is returned and the callsite of the function invocation itself should be used.
1415
crate fn find_closest_untracked_caller_location(&self) -> Option<Span> {
1516
let mut caller_span = None;
1617
for next_caller in self.stack.iter().rev() {
@@ -54,9 +55,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
5455
}
5556

5657
pub fn alloc_caller_location_for_span(&mut self, span: Span) -> MPlaceTy<'tcx, M::PointerTag> {
58+
let (file, line, column) = self.location_triple_for_span(span);
59+
self.alloc_caller_location(file, line, column)
60+
}
61+
62+
pub(super) fn location_triple_for_span(&self, span: Span) -> (Symbol, u32, u32) {
5763
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
5864
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
59-
self.alloc_caller_location(
65+
(
6066
Symbol::intern(&caller.file.name.to_string()),
6167
caller.line as u32,
6268
caller.col_display as u32 + 1,

src/librustc_mir/interpret/machine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
139139
/// was used.
140140
fn find_mir_or_eval_fn(
141141
ecx: &mut InterpCx<'mir, 'tcx, Self>,
142+
span: Span,
142143
instance: ty::Instance<'tcx>,
143144
args: &[OpTy<'tcx, Self::PointerTag>],
144145
ret: Option<(PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,

src/librustc_mir/interpret/terminator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
238238
| ty::InstanceDef::CloneShim(..)
239239
| ty::InstanceDef::Item(_) => {
240240
// We need MIR for this fn
241-
let body = match M::find_mir_or_eval_fn(self, instance, args, ret, unwind)? {
241+
let body = match M::find_mir_or_eval_fn(self, span, instance, args, ret, unwind)? {
242242
Some(body) => body,
243243
None => return Ok(()),
244244
};

src/librustc_mir/transform/const_prop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
125125

126126
fn find_mir_or_eval_fn(
127127
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
128+
_span: Span,
128129
_instance: ty::Instance<'tcx>,
129130
_args: &[OpTy<'tcx>],
130131
_ret: Option<(PlaceTy<'tcx>, BasicBlock)>,

src/libstd/lib.rs

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

0 commit comments

Comments
 (0)