Skip to content

Commit e8769dd

Browse files
committed
Add some track_caller info to precondition panics
1 parent 43a2e9d commit e8769dd

File tree

63 files changed

+136
-192
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+136
-192
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

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

870+
let location = self.get_caller_location(bx, source_info).immediate();
871+
870872
// Codegen the actual panic invoke/call.
871873
helper.do_call(
872874
self,
873875
bx,
874876
fn_abi,
875877
llfn,
876-
&[msg.0, msg.1],
878+
&[msg.0, msg.1, location],
877879
target.as_ref().map(|bb| (ReturnDest::Nothing, *bb)),
878880
unwind,
879881
&[],

compiler/rustc_codegen_ssa/src/size_of_val.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,20 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
6969
// duplicated plenty of times.)
7070
let fn_ty = bx.fn_decl_backend_type(fn_abi);
7171

72+
let const_loc = bx.tcx().span_as_caller_location(rustc_span::DUMMY_SP);
73+
let location = crate::mir::operand::OperandRef::from_const(
74+
bx,
75+
const_loc,
76+
bx.tcx().caller_location_ty(),
77+
)
78+
.immediate();
79+
7280
bx.call(
7381
fn_ty,
7482
/* fn_attrs */ None,
7583
Some(fn_abi),
7684
llfn,
77-
&[msg.0, msg.1],
85+
&[msg.0, msg.1, location],
7886
None,
7987
None,
8088
);

library/core/src/alloc/layout.rs

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl Layout {
126126
#[rustc_const_stable(feature = "const_alloc_layout_unchecked", since = "1.36.0")]
127127
#[must_use]
128128
#[inline]
129+
#[track_caller]
129130
pub const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
130131
assert_unsafe_precondition!(
131132
check_library_ub,

library/core/src/ascii/ascii_char.rs

+1
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ impl AsciiChar {
503503
/// something useful. It might be tightened before stabilization.)
504504
#[unstable(feature = "ascii_char", issue = "110998")]
505505
#[inline]
506+
#[track_caller]
506507
pub const unsafe fn digit_unchecked(d: u8) -> Self {
507508
assert_unsafe_precondition!(
508509
check_language_ub,

library/core/src/char/convert.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub(super) const fn from_u32(i: u32) -> Option<char> {
2121
/// Converts a `u32` to a `char`, ignoring validity. See [`char::from_u32_unchecked`].
2222
#[inline]
2323
#[must_use]
24+
#[track_caller]
2425
pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
2526
// SAFETY: the caller must guarantee that `i` is a valid char value.
2627
unsafe {

library/core/src/hint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ use crate::{intrinsics, ub_checks};
9797
#[inline]
9898
#[stable(feature = "unreachable", since = "1.27.0")]
9999
#[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
100-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
100+
#[track_caller]
101101
pub const unsafe fn unreachable_unchecked() -> ! {
102102
ub_checks::assert_unsafe_precondition!(
103103
check_language_ub,

library/core/src/intrinsics/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3725,7 +3725,7 @@ pub const fn ptr_metadata<P: ptr::Pointee<Metadata = M> + ?Sized, M>(ptr: *const
37253725
#[rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead"]
37263726
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
37273727
#[inline(always)]
3728-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
3728+
#[track_caller]
37293729
#[rustc_diagnostic_item = "ptr_copy_nonoverlapping"]
37303730
pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
37313731
#[rustc_intrinsic_const_stable_indirect]
@@ -3828,7 +3828,7 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
38283828
#[rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead"]
38293829
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
38303830
#[inline(always)]
3831-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
3831+
#[track_caller]
38323832
#[rustc_diagnostic_item = "ptr_copy"]
38333833
pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
38343834
#[rustc_intrinsic_const_stable_indirect]
@@ -3908,7 +3908,7 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
39083908
#[rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead"]
39093909
#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
39103910
#[inline(always)]
3911-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
3911+
#[track_caller]
39123912
#[rustc_diagnostic_item = "ptr_write_bytes"]
39133913
pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
39143914
#[rustc_intrinsic_const_stable_indirect]

library/core/src/num/int_macros.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ macro_rules! int_impl {
555555
#[must_use = "this returns the result of the operation, \
556556
without modifying the original"]
557557
#[inline(always)]
558-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
558+
#[track_caller]
559559
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
560560
assert_unsafe_precondition!(
561561
check_language_ub,
@@ -705,7 +705,7 @@ macro_rules! int_impl {
705705
#[must_use = "this returns the result of the operation, \
706706
without modifying the original"]
707707
#[inline(always)]
708-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
708+
#[track_caller]
709709
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
710710
assert_unsafe_precondition!(
711711
check_language_ub,
@@ -855,7 +855,7 @@ macro_rules! int_impl {
855855
#[must_use = "this returns the result of the operation, \
856856
without modifying the original"]
857857
#[inline(always)]
858-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
858+
#[track_caller]
859859
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
860860
assert_unsafe_precondition!(
861861
check_language_ub,
@@ -1199,7 +1199,7 @@ macro_rules! int_impl {
11991199
#[must_use = "this returns the result of the operation, \
12001200
without modifying the original"]
12011201
#[inline(always)]
1202-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1202+
#[track_caller]
12031203
pub const unsafe fn unchecked_neg(self) -> Self {
12041204
assert_unsafe_precondition!(
12051205
check_language_ub,
@@ -1327,7 +1327,7 @@ macro_rules! int_impl {
13271327
#[must_use = "this returns the result of the operation, \
13281328
without modifying the original"]
13291329
#[inline(always)]
1330-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1330+
#[track_caller]
13311331
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
13321332
assert_unsafe_precondition!(
13331333
check_language_ub,
@@ -1448,7 +1448,7 @@ macro_rules! int_impl {
14481448
#[must_use = "this returns the result of the operation, \
14491449
without modifying the original"]
14501450
#[inline(always)]
1451-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1451+
#[track_caller]
14521452
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
14531453
assert_unsafe_precondition!(
14541454
check_language_ub,

library/core/src/num/nonzero.rs

+2
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ where
388388
#[rustc_const_stable(feature = "nonzero", since = "1.28.0")]
389389
#[must_use]
390390
#[inline]
391+
#[track_caller]
391392
pub const unsafe fn new_unchecked(n: T) -> Self {
392393
match Self::new(n) {
393394
Some(n) => n,
@@ -428,6 +429,7 @@ where
428429
#[unstable(feature = "nonzero_from_mut", issue = "106290")]
429430
#[must_use]
430431
#[inline]
432+
#[track_caller]
431433
pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self {
432434
match Self::from_mut(n) {
433435
Some(n) => n,

library/core/src/num/uint_macros.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ macro_rules! uint_impl {
601601
#[must_use = "this returns the result of the operation, \
602602
without modifying the original"]
603603
#[inline(always)]
604-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
604+
#[track_caller]
605605
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
606606
assert_unsafe_precondition!(
607607
check_language_ub,
@@ -791,7 +791,7 @@ macro_rules! uint_impl {
791791
#[must_use = "this returns the result of the operation, \
792792
without modifying the original"]
793793
#[inline(always)]
794-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
794+
#[track_caller]
795795
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
796796
assert_unsafe_precondition!(
797797
check_language_ub,
@@ -974,7 +974,7 @@ macro_rules! uint_impl {
974974
#[must_use = "this returns the result of the operation, \
975975
without modifying the original"]
976976
#[inline(always)]
977-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
977+
#[track_caller]
978978
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
979979
assert_unsafe_precondition!(
980980
check_language_ub,
@@ -1588,7 +1588,7 @@ macro_rules! uint_impl {
15881588
#[must_use = "this returns the result of the operation, \
15891589
without modifying the original"]
15901590
#[inline(always)]
1591-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1591+
#[track_caller]
15921592
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
15931593
assert_unsafe_precondition!(
15941594
check_language_ub,
@@ -1709,7 +1709,7 @@ macro_rules! uint_impl {
17091709
#[must_use = "this returns the result of the operation, \
17101710
without modifying the original"]
17111711
#[inline(always)]
1712-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1712+
#[track_caller]
17131713
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
17141714
assert_unsafe_precondition!(
17151715
check_language_ub,

library/core/src/ops/index_range.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl IndexRange {
1818
/// # Safety
1919
/// - `start <= end`
2020
#[inline]
21+
#[track_caller]
2122
pub(crate) const unsafe fn new_unchecked(start: usize, end: usize) -> Self {
2223
ub_checks::assert_unsafe_precondition!(
2324
check_library_ub,

library/core/src/panicking.rs

+2
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ panic_const! {
209209

210210
/// Like `panic`, but without unwinding and track_caller to reduce the impact on codesize on the caller.
211211
/// If you want `#[track_caller]` for nicer errors, call `panic_nounwind_fmt` directly.
212+
#[track_caller]
212213
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
213214
#[cfg_attr(feature = "panic_immediate_abort", inline)]
214215
#[lang = "panic_nounwind"] // needed by codegen for non-unwinding panics
@@ -219,6 +220,7 @@ pub const fn panic_nounwind(expr: &'static str) -> ! {
219220
}
220221

221222
/// Like `panic_nounwind`, but also inhibits showing a backtrace.
223+
#[track_caller]
222224
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
223225
#[cfg_attr(feature = "panic_immediate_abort", inline)]
224226
#[rustc_nounwind]

library/core/src/ptr/alignment.rs

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ impl Alignment {
7373
/// It must *not* be zero.
7474
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
7575
#[inline]
76+
#[track_caller]
7677
pub const unsafe fn new_unchecked(align: usize) -> Self {
7778
assert_unsafe_precondition!(
7879
check_language_ub,

library/core/src/ptr/const_ptr.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ impl<T: ?Sized> *const T {
415415
#[must_use = "returns a new pointer rather than modifying its argument"]
416416
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
417417
#[inline(always)]
418-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
418+
#[track_caller]
419419
pub const unsafe fn offset(self, count: isize) -> *const T
420420
where
421421
T: Sized,
@@ -468,7 +468,7 @@ impl<T: ?Sized> *const T {
468468
#[inline(always)]
469469
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
470470
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
471-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
471+
#[track_caller]
472472
pub const unsafe fn byte_offset(self, count: isize) -> Self {
473473
// SAFETY: the caller must uphold the safety contract for `offset`.
474474
unsafe { self.cast::<u8>().offset(count).with_metadata_of(self) }
@@ -766,7 +766,7 @@ impl<T: ?Sized> *const T {
766766
#[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
767767
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
768768
#[inline]
769-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
769+
#[track_caller]
770770
pub const unsafe fn offset_from_unsigned(self, origin: *const T) -> usize
771771
where
772772
T: Sized,
@@ -811,7 +811,7 @@ impl<T: ?Sized> *const T {
811811
#[stable(feature = "ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
812812
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "CURRENT_RUSTC_VERSION")]
813813
#[inline]
814-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
814+
#[track_caller]
815815
pub const unsafe fn byte_offset_from_unsigned<U: ?Sized>(self, origin: *const U) -> usize {
816816
// SAFETY: the caller must uphold the safety contract for `sub_ptr`.
817817
unsafe { self.cast::<u8>().offset_from_unsigned(origin.cast::<u8>()) }
@@ -925,7 +925,7 @@ impl<T: ?Sized> *const T {
925925
#[must_use = "returns a new pointer rather than modifying its argument"]
926926
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
927927
#[inline(always)]
928-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
928+
#[track_caller]
929929
pub const unsafe fn add(self, count: usize) -> Self
930930
where
931931
T: Sized,
@@ -977,7 +977,7 @@ impl<T: ?Sized> *const T {
977977
#[inline(always)]
978978
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
979979
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
980-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
980+
#[track_caller]
981981
pub const unsafe fn byte_add(self, count: usize) -> Self {
982982
// SAFETY: the caller must uphold the safety contract for `add`.
983983
unsafe { self.cast::<u8>().add(count).with_metadata_of(self) }
@@ -1031,7 +1031,7 @@ impl<T: ?Sized> *const T {
10311031
#[must_use = "returns a new pointer rather than modifying its argument"]
10321032
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
10331033
#[inline(always)]
1034-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1034+
#[track_caller]
10351035
pub const unsafe fn sub(self, count: usize) -> Self
10361036
where
10371037
T: Sized,
@@ -1089,7 +1089,7 @@ impl<T: ?Sized> *const T {
10891089
#[inline(always)]
10901090
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
10911091
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
1092-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1092+
#[track_caller]
10931093
pub const unsafe fn byte_sub(self, count: usize) -> Self {
10941094
// SAFETY: the caller must uphold the safety contract for `sub`.
10951095
unsafe { self.cast::<u8>().sub(count).with_metadata_of(self) }
@@ -1262,7 +1262,7 @@ impl<T: ?Sized> *const T {
12621262
#[stable(feature = "pointer_methods", since = "1.26.0")]
12631263
#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
12641264
#[inline]
1265-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1265+
#[track_caller]
12661266
pub const unsafe fn read(self) -> T
12671267
where
12681268
T: Sized,
@@ -1283,7 +1283,7 @@ impl<T: ?Sized> *const T {
12831283
/// [`ptr::read_volatile`]: crate::ptr::read_volatile()
12841284
#[stable(feature = "pointer_methods", since = "1.26.0")]
12851285
#[inline]
1286-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1286+
#[track_caller]
12871287
pub unsafe fn read_volatile(self) -> T
12881288
where
12891289
T: Sized,
@@ -1303,7 +1303,7 @@ impl<T: ?Sized> *const T {
13031303
#[stable(feature = "pointer_methods", since = "1.26.0")]
13041304
#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
13051305
#[inline]
1306-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1306+
#[track_caller]
13071307
pub const unsafe fn read_unaligned(self) -> T
13081308
where
13091309
T: Sized,
@@ -1323,7 +1323,7 @@ impl<T: ?Sized> *const T {
13231323
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
13241324
#[stable(feature = "pointer_methods", since = "1.26.0")]
13251325
#[inline]
1326-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1326+
#[track_caller]
13271327
pub const unsafe fn copy_to(self, dest: *mut T, count: usize)
13281328
where
13291329
T: Sized,
@@ -1343,7 +1343,7 @@ impl<T: ?Sized> *const T {
13431343
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
13441344
#[stable(feature = "pointer_methods", since = "1.26.0")]
13451345
#[inline]
1346-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1346+
#[track_caller]
13471347
pub const unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize)
13481348
where
13491349
T: Sized,

0 commit comments

Comments
 (0)