Skip to content

Commit bd61639

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 5ba6db1 + b046470 commit bd61639

File tree

70 files changed

+139
-114
lines changed

Some content is hidden

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

70 files changed

+139
-114
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
@@ -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
);

compiler/rustc_const_eval/src/util/caller_location.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::hash_map::Entry;
2+
13
use rustc_hir::LangItem;
24
use rustc_middle::query::TyCtxtAt;
35
use rustc_middle::ty::layout::LayoutOf;
@@ -57,6 +59,11 @@ pub(crate) fn const_caller_location_provider(
5759
col: u32,
5860
) -> mir::ConstValue<'_> {
5961
trace!("const_caller_location: {}:{}:{}", file, line, col);
62+
let mut cache = tcx.caller_location_cache.lock();
63+
let entry = match cache.entry((file, line, col)) {
64+
Entry::Occupied(oe) => return *oe.get(),
65+
Entry::Vacant(ve) => ve,
66+
};
6067
let mut ecx = mk_eval_cx_to_read_const_val(
6168
tcx.tcx,
6269
tcx.span,
@@ -68,5 +75,7 @@ pub(crate) fn const_caller_location_provider(
6875
if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
6976
bug!("intern_const_alloc_recursive should not error in this case")
7077
}
71-
mir::ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr(), &tcx))
78+
let val = mir::ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr(), &tcx));
79+
entry.insert(val);
80+
val
7281
}

compiler/rustc_middle/src/ty/context.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::metadata::ModChild;
6666
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
6767
use crate::middle::{resolve_bound_vars, stability};
6868
use crate::mir::interpret::{self, Allocation, ConstAllocation};
69-
use crate::mir::{Body, Local, Place, PlaceElem, ProjectionKind, Promoted};
69+
use crate::mir::{Body, ConstValue, Local, Place, PlaceElem, ProjectionKind, Promoted};
7070
use crate::query::plumbing::QuerySystem;
7171
use crate::query::{IntoQueryParam, LocalCrate, Providers, TyCtxtAt};
7272
use crate::thir::Thir;
@@ -1297,6 +1297,8 @@ pub struct GlobalCtxt<'tcx> {
12971297
/// Stores memory for globals (statics/consts).
12981298
pub(crate) alloc_map: Lock<interpret::AllocMap<'tcx>>,
12991299

1300+
pub caller_location_cache: Lock<FxHashMap<(Symbol, u32, u32), ConstValue<'tcx>>>,
1301+
13001302
current_gcx: CurrentGcx,
13011303
}
13021304

@@ -1527,6 +1529,7 @@ impl<'tcx> TyCtxt<'tcx> {
15271529
canonical_param_env_cache: Default::default(),
15281530
data_layout,
15291531
alloc_map: Lock::new(interpret::AllocMap::new()),
1532+
caller_location_cache: Default::default(),
15301533
current_gcx,
15311534
}
15321535
}

library/core/src/alloc/layout.rs

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl Layout {
128128
#[must_use]
129129
#[inline]
130130
#[rustc_allow_const_fn_unstable(ptr_alignment_type)]
131+
#[track_caller]
131132
pub const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
132133
assert_unsafe_precondition!(
133134
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.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3288,7 +3288,7 @@ pub const fn ptr_metadata<P: ptr::Pointee<Metadata = M> + ?Sized, M>(_ptr: *cons
32883288
#[rustc_allowed_through_unstable_modules]
32893289
#[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
32903290
#[inline(always)]
3291-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
3291+
#[track_caller]
32923292
#[rustc_diagnostic_item = "ptr_copy_nonoverlapping"]
32933293
pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
32943294
extern "rust-intrinsic" {
@@ -3390,7 +3390,7 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
33903390
#[rustc_allowed_through_unstable_modules]
33913391
#[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
33923392
#[inline(always)]
3393-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
3393+
#[track_caller]
33943394
#[rustc_diagnostic_item = "ptr_copy"]
33953395
pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
33963396
extern "rust-intrinsic" {
@@ -3470,7 +3470,7 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
34703470
#[rustc_allowed_through_unstable_modules]
34713471
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
34723472
#[inline(always)]
3473-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
3473+
#[track_caller]
34743474
#[rustc_diagnostic_item = "ptr_write_bytes"]
34753475
pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
34763476
extern "rust-intrinsic" {

library/core/src/num/int_macros.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ macro_rules! int_impl {
510510
#[must_use = "this returns the result of the operation, \
511511
without modifying the original"]
512512
#[inline(always)]
513-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
513+
#[track_caller]
514514
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
515515
assert_unsafe_precondition!(
516516
check_language_ub,
@@ -662,7 +662,7 @@ macro_rules! int_impl {
662662
#[must_use = "this returns the result of the operation, \
663663
without modifying the original"]
664664
#[inline(always)]
665-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
665+
#[track_caller]
666666
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
667667
assert_unsafe_precondition!(
668668
check_language_ub,
@@ -814,7 +814,7 @@ macro_rules! int_impl {
814814
#[must_use = "this returns the result of the operation, \
815815
without modifying the original"]
816816
#[inline(always)]
817-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
817+
#[track_caller]
818818
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
819819
assert_unsafe_precondition!(
820820
check_language_ub,
@@ -1163,7 +1163,7 @@ macro_rules! int_impl {
11631163
without modifying the original"]
11641164
#[rustc_const_unstable(feature = "unchecked_neg", issue = "85122")]
11651165
#[inline(always)]
1166-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1166+
#[track_caller]
11671167
pub const unsafe fn unchecked_neg(self) -> Self {
11681168
assert_unsafe_precondition!(
11691169
check_language_ub,
@@ -1296,7 +1296,7 @@ macro_rules! int_impl {
12961296
without modifying the original"]
12971297
#[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
12981298
#[inline(always)]
1299-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1299+
#[track_caller]
13001300
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
13011301
assert_unsafe_precondition!(
13021302
check_language_ub,
@@ -1422,7 +1422,7 @@ macro_rules! int_impl {
14221422
without modifying the original"]
14231423
#[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
14241424
#[inline(always)]
1425-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1425+
#[track_caller]
14261426
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
14271427
assert_unsafe_precondition!(
14281428
check_language_ub,

library/core/src/num/nonzero.rs

+2
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ where
364364
#[rustc_const_stable(feature = "nonzero", since = "1.28.0")]
365365
#[must_use]
366366
#[inline]
367+
#[track_caller]
367368
pub const unsafe fn new_unchecked(n: T) -> Self {
368369
match Self::new(n) {
369370
Some(n) => n,
@@ -404,6 +405,7 @@ where
404405
#[unstable(feature = "nonzero_from_mut", issue = "106290")]
405406
#[must_use]
406407
#[inline]
408+
#[track_caller]
407409
pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self {
408410
match Self::from_mut(n) {
409411
Some(n) => n,

library/core/src/num/uint_macros.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ macro_rules! uint_impl {
557557
#[must_use = "this returns the result of the operation, \
558558
without modifying the original"]
559559
#[inline(always)]
560-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
560+
#[track_caller]
561561
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
562562
assert_unsafe_precondition!(
563563
check_language_ub,
@@ -749,7 +749,7 @@ macro_rules! uint_impl {
749749
#[must_use = "this returns the result of the operation, \
750750
without modifying the original"]
751751
#[inline(always)]
752-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
752+
#[track_caller]
753753
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
754754
assert_unsafe_precondition!(
755755
check_language_ub,
@@ -906,7 +906,7 @@ macro_rules! uint_impl {
906906
#[must_use = "this returns the result of the operation, \
907907
without modifying the original"]
908908
#[inline(always)]
909-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
909+
#[track_caller]
910910
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
911911
assert_unsafe_precondition!(
912912
check_language_ub,
@@ -1485,7 +1485,7 @@ macro_rules! uint_impl {
14851485
without modifying the original"]
14861486
#[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
14871487
#[inline(always)]
1488-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1488+
#[track_caller]
14891489
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
14901490
assert_unsafe_precondition!(
14911491
check_language_ub,
@@ -1611,7 +1611,7 @@ macro_rules! uint_impl {
16111611
without modifying the original"]
16121612
#[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
16131613
#[inline(always)]
1614-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1614+
#[track_caller]
16151615
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
16161616
assert_unsafe_precondition!(
16171617
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
@@ -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/ptr/alignment.rs

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ impl Alignment {
7575
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
7676
#[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")]
7777
#[inline]
78+
#[track_caller]
7879
pub const unsafe fn new_unchecked(align: usize) -> Self {
7980
assert_unsafe_precondition!(
8081
check_language_ub,

library/core/src/ptr/const_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ impl<T: ?Sized> *const T {
715715
#[unstable(feature = "ptr_sub_ptr", issue = "95892")]
716716
#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
717717
#[inline]
718-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
718+
#[track_caller]
719719
pub const unsafe fn sub_ptr(self, origin: *const T) -> usize
720720
where
721721
T: Sized,

library/core/src/ptr/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,7 @@ pub const unsafe fn swap<T>(x: *mut T, y: *mut T) {
11381138
#[stable(feature = "swap_nonoverlapping", since = "1.27.0")]
11391139
#[rustc_const_unstable(feature = "const_swap", issue = "83163")]
11401140
#[rustc_diagnostic_item = "ptr_swap_nonoverlapping"]
1141+
#[track_caller]
11411142
pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
11421143
#[allow(unused)]
11431144
macro_rules! attempt_swap_as_chunks {
@@ -1268,6 +1269,7 @@ const unsafe fn swap_nonoverlapping_simple_untyped<T>(x: *mut T, y: *mut T, coun
12681269
#[stable(feature = "rust1", since = "1.0.0")]
12691270
#[rustc_const_unstable(feature = "const_replace", issue = "83164")]
12701271
#[rustc_diagnostic_item = "ptr_replace"]
1272+
#[track_caller]
12711273
pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
12721274
// SAFETY: the caller must guarantee that `dst` is valid to be
12731275
// cast to a mutable reference (valid for writes, aligned, initialized),
@@ -1394,7 +1396,7 @@ pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
13941396
#[inline]
13951397
#[stable(feature = "rust1", since = "1.0.0")]
13961398
#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
1397-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1399+
#[track_caller]
13981400
#[rustc_diagnostic_item = "ptr_read"]
13991401
pub const unsafe fn read<T>(src: *const T) -> T {
14001402
// It would be semantically correct to implement this via `copy_nonoverlapping`
@@ -1620,7 +1622,7 @@ pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
16201622
#[stable(feature = "rust1", since = "1.0.0")]
16211623
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
16221624
#[rustc_diagnostic_item = "ptr_write"]
1623-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1625+
#[track_caller]
16241626
pub const unsafe fn write<T>(dst: *mut T, src: T) {
16251627
// Semantically, it would be fine for this to be implemented as a
16261628
// `copy_nonoverlapping` and appropriate drop suppression of `src`.
@@ -1802,7 +1804,7 @@ pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
18021804
/// ```
18031805
#[inline]
18041806
#[stable(feature = "volatile", since = "1.9.0")]
1805-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1807+
#[track_caller]
18061808
#[rustc_diagnostic_item = "ptr_read_volatile"]
18071809
pub unsafe fn read_volatile<T>(src: *const T) -> T {
18081810
// SAFETY: the caller must uphold the safety contract for `volatile_load`.
@@ -1882,7 +1884,7 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
18821884
#[inline]
18831885
#[stable(feature = "volatile", since = "1.9.0")]
18841886
#[rustc_diagnostic_item = "ptr_write_volatile"]
1885-
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1887+
#[track_caller]
18861888
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
18871889
// SAFETY: the caller must uphold the safety contract for `volatile_store`.
18881890
unsafe {

library/core/src/ptr/non_null.rs

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ impl<T: ?Sized> NonNull<T> {
192192
#[stable(feature = "nonnull", since = "1.25.0")]
193193
#[rustc_const_stable(feature = "const_nonnull_new_unchecked", since = "1.25.0")]
194194
#[inline]
195+
#[track_caller]
195196
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
196197
// SAFETY: the caller must guarantee that `ptr` is non-null.
197198
unsafe {

0 commit comments

Comments
 (0)