Skip to content

Commit c280752

Browse files
committed
Help optimize out backtraces when disabled
1 parent bd3cb52 commit c280752

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

library/std/src/panicking.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ fn default_hook(info: &PanicInfo<'_>) {
263263
// If this is a double panic, make sure that we print a backtrace
264264
// for this panic. Otherwise only print it if logging is enabled.
265265
let backtrace_env = if panic_count::get_count() >= 2 {
266-
RustBacktrace::Print(crate::backtrace_rs::PrintFmt::Full)
266+
backtrace::rust_backtrace_print_full()
267267
} else {
268268
backtrace::rust_backtrace_env()
269269
};

library/std/src/sys_common/backtrace.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,18 @@ pub enum RustBacktrace {
150150
RuntimeDisabled,
151151
}
152152

153+
// If the `backtrace` feature of this crate isn't enabled quickly return
154+
// `Disabled` so this can be constant propagated all over the place to
155+
// optimize away callers.
156+
#[cfg(not(feature = "backtrace"))]
157+
pub fn rust_backtrace_env() -> RustBacktrace {
158+
RustBacktrace::Disabled
159+
}
160+
153161
// For now logging is turned off by default, and this function checks to see
154162
// whether the magical environment variable is present to see if it's turned on.
163+
#[cfg(feature = "backtrace")]
155164
pub fn rust_backtrace_env() -> RustBacktrace {
156-
// If the `backtrace` feature of this crate isn't enabled quickly return
157-
// `None` so this can be constant propagated all over the place to turn
158-
// optimize away callers.
159-
if !cfg!(feature = "backtrace") {
160-
return RustBacktrace::Disabled;
161-
}
162-
163165
// Setting environment variables for Fuchsia components isn't a standard
164166
// or easily supported workflow. For now, always display backtraces.
165167
if cfg!(target_os = "fuchsia") {
@@ -189,6 +191,15 @@ pub fn rust_backtrace_env() -> RustBacktrace {
189191
format
190192
}
191193

194+
/// Setting for printing the full backtrace, unless backtraces are completely disabled
195+
pub(crate) fn rust_backtrace_print_full() -> RustBacktrace {
196+
if cfg!(feature = "backtrace") {
197+
RustBacktrace::Print(PrintFmt::Full)
198+
} else {
199+
RustBacktrace::Disabled
200+
}
201+
}
202+
192203
/// Prints the filename of the backtrace frame.
193204
///
194205
/// See also `output`.

0 commit comments

Comments
 (0)