Skip to content

Commit 543e77f

Browse files
Make sure std::panic::Location::caller() gets optimized away.
1 parent 7531d2f commit 543e77f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

library/core/src/panic/location.rs

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl<'a> Location<'a> {
8383
#[stable(feature = "track_caller", since = "1.46.0")]
8484
#[rustc_const_unstable(feature = "const_caller_location", issue = "76156")]
8585
#[track_caller]
86+
#[inline(always)] // Mark as inline so that the call gets optimized away.
8687
pub const fn caller() -> &'static Location<'static> {
8788
crate::intrinsics::caller_location()
8889
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This test makes sure that calls to std::panic::Location::caller()
2+
// don't result in an actual function call. The caller location is
3+
// known at compile time so the call can always be optimized away.
4+
5+
// compile-flags: -Copt-level=2
6+
7+
#![crate_type = "lib"]
8+
#![feature(bench_black_box)]
9+
10+
// The first check makes sure that the caller location is used at all,
11+
// i.e. that std::hint::black_box() works.
12+
// CHECK: %0 = alloca %"core::panic::location::Location"*
13+
14+
// This check makes sure that no call to `std::panic::Location::caller()`
15+
// is emitted. The sequence of characters is valid for both v0 and legacy
16+
// mangling.
17+
// CHECK-NOT: call {{.*}}8Location6caller
18+
19+
// CHECK: call void asm sideeffect {{.*}}(%"core::panic::location::Location"** nonnull %0)
20+
21+
#[track_caller]
22+
fn foo() {
23+
std::hint::black_box(std::panic::Location::caller());
24+
}
25+
26+
pub fn bar() {
27+
foo();
28+
}

0 commit comments

Comments
 (0)