From 18e2a95899fee2ee13f14036f1ea22b8ab48debd Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Tue, 27 Aug 2024 11:05:45 -0400 Subject: [PATCH 1/2] Add some track_caller info to precondition panics --- compiler/rustc_codegen_ssa/src/mir/block.rs | 4 +++- compiler/rustc_codegen_ssa/src/size_of_val.rs | 10 +++++++++- library/core/src/panicking.rs | 2 ++ tests/ui/extern/extern-types-field-offset.run.stderr | 2 +- .../extern/extern-types-size_of_val.align.run.stderr | 2 +- .../ui/extern/extern-types-size_of_val.size.run.stderr | 2 +- 6 files changed, 17 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index 817e2ca72ec12..83b221213ac7f 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -790,13 +790,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { let (fn_abi, llfn, instance) = common::build_langcall(bx, Some(source_info.span), LangItem::PanicNounwind); + let location = self.get_caller_location(bx, source_info).immediate(); + // Codegen the actual panic invoke/call. helper.do_call( self, bx, fn_abi, llfn, - &[msg.0, msg.1], + &[msg.0, msg.1, location], target.as_ref().map(|bb| (ReturnDest::Nothing, *bb)), unwind, &[], diff --git a/compiler/rustc_codegen_ssa/src/size_of_val.rs b/compiler/rustc_codegen_ssa/src/size_of_val.rs index 933904f984505..eb48c2181b397 100644 --- a/compiler/rustc_codegen_ssa/src/size_of_val.rs +++ b/compiler/rustc_codegen_ssa/src/size_of_val.rs @@ -72,12 +72,20 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( // (But we are in good company, this code is duplicated plenty of times.) let fn_ty = bx.fn_decl_backend_type(fn_abi); + let const_loc = bx.tcx().span_as_caller_location(rustc_span::DUMMY_SP); + let location = crate::mir::operand::OperandRef::from_const( + bx, + const_loc, + bx.tcx().caller_location_ty(), + ) + .immediate(); + bx.call( fn_ty, /* fn_attrs */ None, Some(fn_abi), llfn, - &[msg.0, msg.1], + &[msg.0, msg.1, location], None, None, ); diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 7affe63825719..78995cd0cae40 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -212,6 +212,7 @@ panic_const! { /// Like `panic`, but without unwinding and track_caller to reduce the impact on codesize on the caller. /// If you want `#[track_caller]` for nicer errors, call `panic_nounwind_fmt` directly. +#[track_caller] #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)] #[cfg_attr(feature = "panic_immediate_abort", inline)] #[lang = "panic_nounwind"] // needed by codegen for non-unwinding panics @@ -222,6 +223,7 @@ pub const fn panic_nounwind(expr: &'static str) -> ! { } /// Like `panic_nounwind`, but also inhibits showing a backtrace. +#[track_caller] #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)] #[cfg_attr(feature = "panic_immediate_abort", inline)] #[rustc_nounwind] diff --git a/tests/ui/extern/extern-types-field-offset.run.stderr b/tests/ui/extern/extern-types-field-offset.run.stderr index f14073989800b..5121ea7b07e3f 100644 --- a/tests/ui/extern/extern-types-field-offset.run.stderr +++ b/tests/ui/extern/extern-types-field-offset.run.stderr @@ -1,4 +1,4 @@ -thread 'main' panicked at core/src/panicking.rs:$LINE:$COL: +thread 'main' panicked at $DIR/extern-types-field-offset.rs:1:1: attempted to compute the size or alignment of extern type `Opaque` note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace thread caused non-unwinding panic. aborting. diff --git a/tests/ui/extern/extern-types-size_of_val.align.run.stderr b/tests/ui/extern/extern-types-size_of_val.align.run.stderr index faad1aa13faf6..a5740d351205c 100644 --- a/tests/ui/extern/extern-types-size_of_val.align.run.stderr +++ b/tests/ui/extern/extern-types-size_of_val.align.run.stderr @@ -1,4 +1,4 @@ -thread 'main' panicked at core/src/panicking.rs:$LINE:$COL: +thread 'main' panicked at $DIR/extern-types-size_of_val.rs:1:1: attempted to compute the size or alignment of extern type `A` note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace thread caused non-unwinding panic. aborting. diff --git a/tests/ui/extern/extern-types-size_of_val.size.run.stderr b/tests/ui/extern/extern-types-size_of_val.size.run.stderr index faad1aa13faf6..a5740d351205c 100644 --- a/tests/ui/extern/extern-types-size_of_val.size.run.stderr +++ b/tests/ui/extern/extern-types-size_of_val.size.run.stderr @@ -1,4 +1,4 @@ -thread 'main' panicked at core/src/panicking.rs:$LINE:$COL: +thread 'main' panicked at $DIR/extern-types-size_of_val.rs:1:1: attempted to compute the size or alignment of extern type `A` note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace thread caused non-unwinding panic. aborting. From 197c7b3a06597f236db56a76d6b5543694d2bc74 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Tue, 27 Aug 2024 15:55:10 -0400 Subject: [PATCH 2/2] Moar --- library/core/src/slice/raw.rs | 1 + library/core/src/ub_checks.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/library/core/src/slice/raw.rs b/library/core/src/slice/raw.rs index 85507eb8a7381..9b4e3efc4704c 100644 --- a/library/core/src/slice/raw.rs +++ b/library/core/src/slice/raw.rs @@ -120,6 +120,7 @@ use crate::{array, ptr, ub_checks}; #[rustc_const_stable(feature = "const_slice_from_raw_parts", since = "1.64.0")] #[must_use] #[rustc_diagnostic_item = "slice_from_raw_parts"] +#[track_caller] pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] { // SAFETY: the caller must uphold the safety contract for `from_raw_parts`. unsafe { diff --git a/library/core/src/ub_checks.rs b/library/core/src/ub_checks.rs index c1a8c34539e6c..c07069725f6e6 100644 --- a/library/core/src/ub_checks.rs +++ b/library/core/src/ub_checks.rs @@ -65,6 +65,7 @@ macro_rules! assert_unsafe_precondition { #[inline] #[rustc_nounwind] #[rustc_const_unstable(feature = "const_ub_checks", issue = "none")] + #[track_caller] const fn precondition_check($($name:$ty),*) { if !$e { ::core::panicking::panic_nounwind(