1
Fork 0

avoid #[cfg] in favor of cfg!

This commit is contained in:
Ralf Jung 2019-09-16 16:37:44 +02:00
commit 49854c4f71
2 changed files with 7 additions and 13 deletions

View file

@ -17,8 +17,7 @@ use crate::ptr;
use crate::raw; use crate::raw;
use crate::sys::stdio::panic_output; use crate::sys::stdio::panic_output;
use crate::sys_common::rwlock::RWLock; use crate::sys_common::rwlock::RWLock;
use crate::sys_common::thread_info; use crate::sys_common::{thread_info, util, backtrace};
use crate::sys_common::util;
use crate::thread; use crate::thread;
#[cfg(not(test))] #[cfg(not(test))]
@ -157,20 +156,18 @@ pub fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send> {
} }
fn default_hook(info: &PanicInfo<'_>) { fn default_hook(info: &PanicInfo<'_>) {
#[cfg(feature = "backtrace")]
use crate::sys_common::{backtrace as backtrace_mod};
// If this is a double panic, make sure that we print a backtrace // If this is a double panic, make sure that we print a backtrace
// for this panic. Otherwise only print it if logging is enabled. // for this panic. Otherwise only print it if logging is enabled.
#[cfg(feature = "backtrace")] let log_backtrace = if cfg!(feature = "backtrace") {
let log_backtrace = {
let panics = update_panic_count(0); let panics = update_panic_count(0);
if panics >= 2 { if panics >= 2 {
Some(backtrace_rs::PrintFmt::Full) Some(backtrace_rs::PrintFmt::Full)
} else { } else {
backtrace_mod::log_enabled() backtrace::log_enabled()
} }
} else {
None
}; };
// The current implementation always returns `Some`. // The current implementation always returns `Some`.
@ -190,14 +187,13 @@ fn default_hook(info: &PanicInfo<'_>) {
let _ = writeln!(err, "thread '{}' panicked at '{}', {}", let _ = writeln!(err, "thread '{}' panicked at '{}', {}",
name, msg, location); name, msg, location);
#[cfg(feature = "backtrace")] if cfg!(feature = "backtrace") {
{
use crate::sync::atomic::{AtomicBool, Ordering}; use crate::sync::atomic::{AtomicBool, Ordering};
static FIRST_PANIC: AtomicBool = AtomicBool::new(true); static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
if let Some(format) = log_backtrace { if let Some(format) = log_backtrace {
let _ = backtrace_mod::print(err, format); let _ = backtrace::print(err, format);
} else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) { } else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) {
let _ = writeln!(err, "note: run with `RUST_BACKTRACE=1` \ let _ = writeln!(err, "note: run with `RUST_BACKTRACE=1` \
environment variable to display a backtrace."); environment variable to display a backtrace.");

View file

@ -33,7 +33,6 @@ pub fn lock() -> impl Drop {
} }
/// Prints the current backtrace. /// Prints the current backtrace.
#[cfg(feature = "backtrace")]
pub fn print(w: &mut dyn Write, format: PrintFmt) -> io::Result<()> { pub fn print(w: &mut dyn Write, format: PrintFmt) -> io::Result<()> {
// There are issues currently linking libbacktrace into tests, and in // There are issues currently linking libbacktrace into tests, and in
// general during libstd's own unit tests we're not testing this path. In // general during libstd's own unit tests we're not testing this path. In
@ -129,7 +128,6 @@ where
// For now logging is turned off by default, and this function checks to see // For now logging is turned off by default, and this function checks to see
// whether the magical environment variable is present to see if it's turned on. // whether the magical environment variable is present to see if it's turned on.
#[cfg(feature = "backtrace")]
pub fn log_enabled() -> Option<PrintFmt> { pub fn log_enabled() -> Option<PrintFmt> {
use crate::sync::atomic::{self, Ordering}; use crate::sync::atomic::{self, Ordering};