Skip to content

Commit 47937f2

Browse files
committed
Add some track_caller info to precondition panics
1 parent 9649706 commit 47937f2

12 files changed

+29
-8
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -790,13 +790,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
790790
let (fn_abi, llfn, instance) =
791791
common::build_langcall(bx, Some(source_info.span), LangItem::PanicNounwind);
792792

793+
let location = self.get_caller_location(bx, source_info).immediate();
794+
793795
// Codegen the actual panic invoke/call.
794796
helper.do_call(
795797
self,
796798
bx,
797799
fn_abi,
798800
llfn,
799-
&[msg.0, msg.1],
801+
&[msg.0, msg.1, location],
800802
target.as_ref().map(|bb| (ReturnDest::Nothing, *bb)),
801803
unwind,
802804
&[],

compiler/rustc_codegen_ssa/src/size_of_val.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,20 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
7272
// (But we are in good company, this code is duplicated plenty of times.)
7373
let fn_ty = bx.fn_decl_backend_type(fn_abi);
7474

75+
let const_loc = bx.tcx().span_as_caller_location(rustc_span::DUMMY_SP);
76+
let location = crate::mir::operand::OperandRef::from_const(
77+
bx,
78+
const_loc,
79+
bx.tcx().caller_location_ty(),
80+
)
81+
.immediate();
82+
7583
bx.call(
7684
fn_ty,
7785
/* fn_attrs */ None,
7886
Some(fn_abi),
7987
llfn,
80-
&[msg.0, msg.1],
88+
&[msg.0, msg.1, location],
8189
None,
8290
None,
8391
);

library/core/src/panicking.rs

+2
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ panic_const! {
212212

213213
/// Like `panic`, but without unwinding and track_caller to reduce the impact on codesize on the caller.
214214
/// If you want `#[track_caller]` for nicer errors, call `panic_nounwind_fmt` directly.
215+
#[track_caller]
215216
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
216217
#[cfg_attr(feature = "panic_immediate_abort", inline)]
217218
#[lang = "panic_nounwind"] // needed by codegen for non-unwinding panics
@@ -222,6 +223,7 @@ pub const fn panic_nounwind(expr: &'static str) -> ! {
222223
}
223224

224225
/// Like `panic_nounwind`, but also inhibits showing a backtrace.
226+
#[track_caller]
225227
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
226228
#[cfg_attr(feature = "panic_immediate_abort", inline)]
227229
#[rustc_nounwind]

library/core/src/slice/index.rs

+6
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ unsafe impl<T> SliceIndex<[T]> for usize {
267267
}
268268

269269
#[inline]
270+
#[track_caller]
270271
unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
271272
assert_unsafe_precondition!(
272273
check_language_ub,
@@ -286,6 +287,7 @@ unsafe impl<T> SliceIndex<[T]> for usize {
286287
}
287288

288289
#[inline]
290+
#[track_caller]
289291
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T {
290292
assert_unsafe_precondition!(
291293
check_library_ub,
@@ -336,6 +338,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
336338
}
337339

338340
#[inline]
341+
#[track_caller]
339342
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
340343
assert_unsafe_precondition!(
341344
check_library_ub,
@@ -350,6 +353,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
350353
}
351354

352355
#[inline]
356+
#[track_caller]
353357
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
354358
assert_unsafe_precondition!(
355359
check_library_ub,
@@ -416,6 +420,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
416420
}
417421

418422
#[inline]
423+
#[track_caller]
419424
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
420425
assert_unsafe_precondition!(
421426
check_library_ub,
@@ -440,6 +445,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
440445
}
441446

442447
#[inline]
448+
#[track_caller]
443449
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
444450
assert_unsafe_precondition!(
445451
check_library_ub,

library/core/src/slice/raw.rs

+2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ use crate::{array, ptr, ub_checks};
120120
#[rustc_const_stable(feature = "const_slice_from_raw_parts", since = "1.64.0")]
121121
#[must_use]
122122
#[rustc_diagnostic_item = "slice_from_raw_parts"]
123+
#[track_caller]
123124
pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
124125
// SAFETY: the caller must uphold the safety contract for `from_raw_parts`.
125126
unsafe {
@@ -174,6 +175,7 @@ pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T]
174175
#[rustc_const_unstable(feature = "const_slice_from_raw_parts_mut", issue = "67456")]
175176
#[must_use]
176177
#[rustc_diagnostic_item = "slice_from_raw_parts_mut"]
178+
#[track_caller]
177179
pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
178180
// SAFETY: the caller must uphold the safety contract for `from_raw_parts_mut`.
179181
unsafe {

library/core/src/ub_checks.rs

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ macro_rules! assert_unsafe_precondition {
6565
#[inline]
6666
#[rustc_nounwind]
6767
#[rustc_const_unstable(feature = "const_ub_checks", issue = "none")]
68+
#[track_caller]
6869
const fn precondition_check($($name:$ty),*) {
6970
if !$e {
7071
::core::panicking::panic_nounwind(

tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range<usize>) ->
99
scope 1 (inlined core::slice::<impl [u32]>::get_unchecked_mut::<std::ops::Range<usize>>) {
1010
let mut _5: *mut [u32];
1111
let mut _11: *mut [u32];
12-
scope 2 (inlined <std::ops::Range<usize> as SliceIndex<[u32]>>::get_unchecked_mut) {
12+
scope 2 (inlined #[track_caller] <std::ops::Range<usize> as SliceIndex<[u32]>>::get_unchecked_mut) {
1313
let mut _6: usize;
1414
let _7: ();
1515
let _8: usize;

tests/mir-opt/pre-codegen/slice_index.slice_ptr_get_unchecked_range.PreCodegen.after.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn slice_ptr_get_unchecked_range(_1: *const [u32], _2: std::ops::Range<usize>) -
77
let mut _3: usize;
88
let mut _4: usize;
99
scope 1 (inlined std::ptr::const_ptr::<impl *const [u32]>::get_unchecked::<std::ops::Range<usize>>) {
10-
scope 2 (inlined <std::ops::Range<usize> as SliceIndex<[u32]>>::get_unchecked) {
10+
scope 2 (inlined #[track_caller] <std::ops::Range<usize> as SliceIndex<[u32]>>::get_unchecked) {
1111
let mut _5: usize;
1212
let _6: ();
1313
let _7: usize;

tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
4848
}
4949
}
5050
}
51-
scope 13 (inlined std::slice::from_raw_parts::<'_, u8>) {
51+
scope 13 (inlined #[track_caller] std::slice::from_raw_parts::<'_, u8>) {
5252
debug data => _5;
5353
debug len => _7;
5454
let _8: *const [u8];
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
thread 'main' panicked at core/src/panicking.rs:$LINE:$COL:
1+
thread 'main' panicked at $DIR/extern-types-field-offset.rs:1:1:
22
attempted to compute the size or alignment of extern type `Opaque`
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
44
thread caused non-unwinding panic. aborting.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
thread 'main' panicked at core/src/panicking.rs:$LINE:$COL:
1+
thread 'main' panicked at $DIR/extern-types-size_of_val.rs:1:1:
22
attempted to compute the size or alignment of extern type `A`
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
44
thread caused non-unwinding panic. aborting.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
thread 'main' panicked at core/src/panicking.rs:$LINE:$COL:
1+
thread 'main' panicked at $DIR/extern-types-size_of_val.rs:1:1:
22
attempted to compute the size or alignment of extern type `A`
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
44
thread caused non-unwinding panic. aborting.

0 commit comments

Comments
 (0)