Skip to content

Commit 4c2b10c

Browse files
committed
Auto merge of rust-lang#129658 - saethlin:spare-a-crumb, r=<try>
Add some track_caller info to precondition panics r? `@ghost` Thought of this while looking at rust-lang#129642 (comment)
2 parents 5c0a6e6 + 5721464 commit 4c2b10c

File tree

93 files changed

+256
-368
lines changed

Some content is hidden

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

93 files changed

+256
-368
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

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

806+
let location = self.get_caller_location(bx, source_info).immediate();
807+
806808
// Codegen the actual panic invoke/call.
807809
helper.do_call(
808810
self,
809811
bx,
810812
fn_abi,
811813
llfn,
812-
&[msg.0, msg.1],
814+
&[msg.0, msg.1, location],
813815
target.as_ref().map(|bb| (ReturnDest::Nothing, *bb)),
814816
unwind,
815817
&[],

compiler/rustc_codegen_ssa/src/size_of_val.rs

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

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

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
@@ -4340,7 +4340,7 @@ pub const fn ptr_metadata<P: ptr::Pointee<Metadata = M> + ?Sized, M>(_ptr: *cons
43404340
#[rustc_allowed_through_unstable_modules]
43414341
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
43424342
#[inline(always)]
4343-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
4343+
#[track_caller]
43444344
#[rustc_diagnostic_item = "ptr_copy_nonoverlapping"]
43454345
pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
43464346
#[rustc_intrinsic_const_stable_indirect]
@@ -4444,7 +4444,7 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
44444444
#[rustc_allowed_through_unstable_modules]
44454445
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
44464446
#[inline(always)]
4447-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
4447+
#[track_caller]
44484448
#[rustc_diagnostic_item = "ptr_copy"]
44494449
pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
44504450
#[rustc_intrinsic_const_stable_indirect]
@@ -4527,7 +4527,7 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
45274527
#[rustc_allowed_through_unstable_modules]
45284528
#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
45294529
#[inline(always)]
4530-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
4530+
#[track_caller]
45314531
#[rustc_diagnostic_item = "ptr_write_bytes"]
45324532
pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
45334533
#[rustc_intrinsic_const_stable_indirect]

library/core/src/num/int_macros.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ macro_rules! int_impl {
509509
#[must_use = "this returns the result of the operation, \
510510
without modifying the original"]
511511
#[inline(always)]
512-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
512+
#[track_caller]
513513
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
514514
assert_unsafe_precondition!(
515515
check_language_ub,
@@ -659,7 +659,7 @@ macro_rules! int_impl {
659659
#[must_use = "this returns the result of the operation, \
660660
without modifying the original"]
661661
#[inline(always)]
662-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
662+
#[track_caller]
663663
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
664664
assert_unsafe_precondition!(
665665
check_language_ub,
@@ -809,7 +809,7 @@ macro_rules! int_impl {
809809
#[must_use = "this returns the result of the operation, \
810810
without modifying the original"]
811811
#[inline(always)]
812-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
812+
#[track_caller]
813813
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
814814
assert_unsafe_precondition!(
815815
check_language_ub,
@@ -1153,7 +1153,7 @@ macro_rules! int_impl {
11531153
#[must_use = "this returns the result of the operation, \
11541154
without modifying the original"]
11551155
#[inline(always)]
1156-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1156+
#[track_caller]
11571157
pub const unsafe fn unchecked_neg(self) -> Self {
11581158
assert_unsafe_precondition!(
11591159
check_language_ub,
@@ -1281,7 +1281,7 @@ macro_rules! int_impl {
12811281
#[must_use = "this returns the result of the operation, \
12821282
without modifying the original"]
12831283
#[inline(always)]
1284-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1284+
#[track_caller]
12851285
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
12861286
assert_unsafe_precondition!(
12871287
check_language_ub,
@@ -1402,7 +1402,7 @@ macro_rules! int_impl {
14021402
#[must_use = "this returns the result of the operation, \
14031403
without modifying the original"]
14041404
#[inline(always)]
1405-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1405+
#[track_caller]
14061406
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
14071407
assert_unsafe_precondition!(
14081408
check_language_ub,

library/core/src/num/nonzero.rs

+2
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ where
394394
#[rustc_const_stable(feature = "nonzero", since = "1.28.0")]
395395
#[must_use]
396396
#[inline]
397+
#[track_caller]
397398
pub const unsafe fn new_unchecked(n: T) -> Self {
398399
match Self::new(n) {
399400
Some(n) => n,
@@ -434,6 +435,7 @@ where
434435
#[unstable(feature = "nonzero_from_mut", issue = "106290")]
435436
#[must_use]
436437
#[inline]
438+
#[track_caller]
437439
pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self {
438440
match Self::from_mut(n) {
439441
Some(n) => n,

library/core/src/num/uint_macros.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ macro_rules! uint_impl {
556556
#[must_use = "this returns the result of the operation, \
557557
without modifying the original"]
558558
#[inline(always)]
559-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
559+
#[track_caller]
560560
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
561561
assert_unsafe_precondition!(
562562
check_language_ub,
@@ -746,7 +746,7 @@ macro_rules! uint_impl {
746746
#[must_use = "this returns the result of the operation, \
747747
without modifying the original"]
748748
#[inline(always)]
749-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
749+
#[track_caller]
750750
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
751751
assert_unsafe_precondition!(
752752
check_language_ub,
@@ -929,7 +929,7 @@ macro_rules! uint_impl {
929929
#[must_use = "this returns the result of the operation, \
930930
without modifying the original"]
931931
#[inline(always)]
932-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
932+
#[track_caller]
933933
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
934934
assert_unsafe_precondition!(
935935
check_language_ub,
@@ -1499,7 +1499,7 @@ macro_rules! uint_impl {
14991499
#[must_use = "this returns the result of the operation, \
15001500
without modifying the original"]
15011501
#[inline(always)]
1502-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1502+
#[track_caller]
15031503
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
15041504
assert_unsafe_precondition!(
15051505
check_language_ub,
@@ -1620,7 +1620,7 @@ macro_rules! uint_impl {
16201620
#[must_use = "this returns the result of the operation, \
16211621
without modifying the original"]
16221622
#[inline(always)]
1623-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1623+
#[track_caller]
16241624
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
16251625
assert_unsafe_precondition!(
16261626
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 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
@@ -72,6 +72,7 @@ impl Alignment {
7272
/// It must *not* be zero.
7373
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
7474
#[inline]
75+
#[track_caller]
7576
pub const unsafe fn new_unchecked(align: usize) -> Self {
7677
assert_unsafe_precondition!(
7778
check_language_ub,

library/core/src/ptr/const_ptr.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<T: ?Sized> *const T {
416416
#[must_use = "returns a new pointer rather than modifying its argument"]
417417
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
418418
#[inline(always)]
419-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
419+
#[track_caller]
420420
pub const unsafe fn offset(self, count: isize) -> *const T
421421
where
422422
T: Sized,
@@ -469,7 +469,7 @@ impl<T: ?Sized> *const T {
469469
#[inline(always)]
470470
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
471471
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
472-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
472+
#[track_caller]
473473
pub const unsafe fn byte_offset(self, count: isize) -> Self {
474474
// SAFETY: the caller must uphold the safety contract for `offset`.
475475
unsafe { self.cast::<u8>().offset(count).with_metadata_of(self) }
@@ -770,7 +770,7 @@ impl<T: ?Sized> *const T {
770770
#[unstable(feature = "ptr_sub_ptr", issue = "95892")]
771771
#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
772772
#[inline]
773-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
773+
#[track_caller]
774774
pub const unsafe fn sub_ptr(self, origin: *const T) -> usize
775775
where
776776
T: Sized,
@@ -815,7 +815,7 @@ impl<T: ?Sized> *const T {
815815
#[unstable(feature = "ptr_sub_ptr", issue = "95892")]
816816
#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
817817
#[inline]
818-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
818+
#[track_caller]
819819
pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: *const U) -> usize {
820820
// SAFETY: the caller must uphold the safety contract for `sub_ptr`.
821821
unsafe { self.cast::<u8>().sub_ptr(origin.cast::<u8>()) }
@@ -929,7 +929,7 @@ impl<T: ?Sized> *const T {
929929
#[must_use = "returns a new pointer rather than modifying its argument"]
930930
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
931931
#[inline(always)]
932-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
932+
#[track_caller]
933933
pub const unsafe fn add(self, count: usize) -> Self
934934
where
935935
T: Sized,
@@ -981,7 +981,7 @@ impl<T: ?Sized> *const T {
981981
#[inline(always)]
982982
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
983983
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
984-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
984+
#[track_caller]
985985
pub const unsafe fn byte_add(self, count: usize) -> Self {
986986
// SAFETY: the caller must uphold the safety contract for `add`.
987987
unsafe { self.cast::<u8>().add(count).with_metadata_of(self) }
@@ -1035,7 +1035,7 @@ impl<T: ?Sized> *const T {
10351035
#[must_use = "returns a new pointer rather than modifying its argument"]
10361036
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
10371037
#[inline(always)]
1038-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1038+
#[track_caller]
10391039
pub const unsafe fn sub(self, count: usize) -> Self
10401040
where
10411041
T: Sized,
@@ -1093,7 +1093,7 @@ impl<T: ?Sized> *const T {
10931093
#[inline(always)]
10941094
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
10951095
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
1096-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1096+
#[track_caller]
10971097
pub const unsafe fn byte_sub(self, count: usize) -> Self {
10981098
// SAFETY: the caller must uphold the safety contract for `sub`.
10991099
unsafe { self.cast::<u8>().sub(count).with_metadata_of(self) }
@@ -1266,7 +1266,7 @@ impl<T: ?Sized> *const T {
12661266
#[stable(feature = "pointer_methods", since = "1.26.0")]
12671267
#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
12681268
#[inline]
1269-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1269+
#[track_caller]
12701270
pub const unsafe fn read(self) -> T
12711271
where
12721272
T: Sized,
@@ -1287,7 +1287,7 @@ impl<T: ?Sized> *const T {
12871287
/// [`ptr::read_volatile`]: crate::ptr::read_volatile()
12881288
#[stable(feature = "pointer_methods", since = "1.26.0")]
12891289
#[inline]
1290-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1290+
#[track_caller]
12911291
pub unsafe fn read_volatile(self) -> T
12921292
where
12931293
T: Sized,
@@ -1307,7 +1307,7 @@ impl<T: ?Sized> *const T {
13071307
#[stable(feature = "pointer_methods", since = "1.26.0")]
13081308
#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
13091309
#[inline]
1310-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1310+
#[track_caller]
13111311
pub const unsafe fn read_unaligned(self) -> T
13121312
where
13131313
T: Sized,
@@ -1327,7 +1327,7 @@ impl<T: ?Sized> *const T {
13271327
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
13281328
#[stable(feature = "pointer_methods", since = "1.26.0")]
13291329
#[inline]
1330-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1330+
#[track_caller]
13311331
pub const unsafe fn copy_to(self, dest: *mut T, count: usize)
13321332
where
13331333
T: Sized,
@@ -1347,7 +1347,7 @@ impl<T: ?Sized> *const T {
13471347
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
13481348
#[stable(feature = "pointer_methods", since = "1.26.0")]
13491349
#[inline]
1350-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1350+
#[track_caller]
13511351
pub const unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize)
13521352
where
13531353
T: Sized,

0 commit comments

Comments
 (0)