Skip to content

Commit

Permalink
Auto merge of #129658 - saethlin:spare-a-crumb, r=<try>
Browse files Browse the repository at this point in the history
Add some track_caller info to precondition panics

r? `@ghost`

Thought of this while looking at #129642 (comment)
  • Loading branch information
bors committed Aug 27, 2024
2 parents 600edc9 + 197c7b3 commit 0e77a71
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 5 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
&[],
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_codegen_ssa/src/size_of_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions library/core/src/slice/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions library/core/src/ub_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/extern/extern-types-field-offset.run.stderr
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion tests/ui/extern/extern-types-size_of_val.align.run.stderr
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion tests/ui/extern/extern-types-size_of_val.size.run.stderr
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 0e77a71

Please sign in to comment.