Skip to content

Commit

Permalink
Auto merge of rust-lang#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 rust-lang#129642 (comment)
  • Loading branch information
bors committed Aug 27, 2024
2 parents 600edc9 + 18e2a95 commit 7798f9b
Show file tree
Hide file tree
Showing 6 changed files with 17 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
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 7798f9b

Please sign in to comment.