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 Sep 4, 2024
2 parents 4ac7bcb + d0c6fc5 commit a67bafe
Show file tree
Hide file tree
Showing 7 changed files with 20 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
1 change: 1 addition & 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 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.
4 changes: 4 additions & 0 deletions tests/ui/print_type_sizes/niche-filling.stdout
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
print-type-size type: `std::panic::Location<'_>`: 24 bytes, alignment: 8 bytes
print-type-size field `.file`: 16 bytes
print-type-size field `.line`: 4 bytes
print-type-size field `.col`: 4 bytes
print-type-size type: `IndirectNonZero`: 12 bytes, alignment: 4 bytes
print-type-size field `.nested`: 8 bytes
print-type-size field `.post`: 2 bytes
Expand Down

0 comments on commit a67bafe

Please sign in to comment.