From 030b1ed7f709539f5ca422758e9fe00d173aee76 Mon Sep 17 00:00:00 2001 From: Jethro Beekman Date: Mon, 27 Aug 2018 09:57:51 -0700 Subject: [PATCH] Refactor stderr_prints_nothing into a more modular function --- src/libstd/panicking.rs | 26 +++++++++++--------------- src/libstd/sys/cloudabi/stdio.rs | 4 ++-- src/libstd/sys/redox/stdio.rs | 4 ++-- src/libstd/sys/unix/stdio.rs | 4 ++-- src/libstd/sys/wasm/stdio.rs | 8 ++++++-- src/libstd/sys/windows/stdio.rs | 4 ++-- src/libstd/sys_common/util.rs | 7 +++---- 7 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs index 4930d356608..b70d56f9e59 100644 --- a/src/libstd/panicking.rs +++ b/src/libstd/panicking.rs @@ -29,7 +29,7 @@ use mem; use ptr; use raw; -use sys::stdio::{Stderr, stderr_prints_nothing}; +use sys::stdio::panic_output; use sys_common::rwlock::RWLock; use sys_common::thread_info; use sys_common::util; @@ -193,7 +193,6 @@ fn default_hook(info: &PanicInfo) { None => "Box", } }; - let mut err = Stderr::new().ok(); let thread = thread_info::current_thread(); let name = thread.as_ref().and_then(|t| t.name()).unwrap_or(""); @@ -215,17 +214,14 @@ fn default_hook(info: &PanicInfo) { } }; - let prev = LOCAL_STDERR.with(|s| s.borrow_mut().take()); - match (prev, err.as_mut()) { - (Some(mut stderr), _) => { - write(&mut *stderr); - let mut s = Some(stderr); - LOCAL_STDERR.with(|slot| { - *slot.borrow_mut() = s.take(); - }); - } - (None, Some(ref mut err)) => { write(err) } - _ => {} + if let Some(mut local) = LOCAL_STDERR.with(|s| s.borrow_mut().take()) { + write(&mut *local); + let mut s = Some(local); + LOCAL_STDERR.with(|slot| { + *slot.borrow_mut() = s.take(); + }); + } else if let Some(mut out) = panic_output() { + write(&mut out); } } @@ -485,7 +481,7 @@ fn rust_panic_with_hook(payload: &mut dyn BoxMeUp, // Some platforms know that printing to stderr won't ever actually // print anything, and if that's the case we can skip the default // hook. - Hook::Default if stderr_prints_nothing() => {} + Hook::Default if panic_output().is_none() => {} Hook::Default => { info.set_payload(payload.get()); default_hook(&info); @@ -494,7 +490,7 @@ fn rust_panic_with_hook(payload: &mut dyn BoxMeUp, info.set_payload(payload.get()); (*ptr)(&info); } - } + }; HOOK_LOCK.read_unlock(); } diff --git a/src/libstd/sys/cloudabi/stdio.rs b/src/libstd/sys/cloudabi/stdio.rs index 1d7344f921c..c90dbd8beab 100644 --- a/src/libstd/sys/cloudabi/stdio.rs +++ b/src/libstd/sys/cloudabi/stdio.rs @@ -78,6 +78,6 @@ pub fn is_ebadf(err: &io::Error) -> bool { pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE; -pub fn stderr_prints_nothing() -> bool { - false +pub fn panic_output() -> Option { + Stderr::new().ok() } diff --git a/src/libstd/sys/redox/stdio.rs b/src/libstd/sys/redox/stdio.rs index 7a4d11b0ecb..52cd9334ffb 100644 --- a/src/libstd/sys/redox/stdio.rs +++ b/src/libstd/sys/redox/stdio.rs @@ -76,6 +76,6 @@ pub fn is_ebadf(err: &io::Error) -> bool { pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE; -pub fn stderr_prints_nothing() -> bool { - false +pub fn panic_output() -> Option { + Stderr::new().ok() } diff --git a/src/libstd/sys/unix/stdio.rs b/src/libstd/sys/unix/stdio.rs index 87ba2aef4f1..63e341abb2c 100644 --- a/src/libstd/sys/unix/stdio.rs +++ b/src/libstd/sys/unix/stdio.rs @@ -76,6 +76,6 @@ pub fn is_ebadf(err: &io::Error) -> bool { pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE; -pub fn stderr_prints_nothing() -> bool { - false +pub fn panic_output() -> Option { + Stderr::new().ok() } diff --git a/src/libstd/sys/wasm/stdio.rs b/src/libstd/sys/wasm/stdio.rs index 023f29576a2..e51aba75333 100644 --- a/src/libstd/sys/wasm/stdio.rs +++ b/src/libstd/sys/wasm/stdio.rs @@ -70,6 +70,10 @@ pub fn is_ebadf(_err: &io::Error) -> bool { true } -pub fn stderr_prints_nothing() -> bool { - !cfg!(feature = "wasm_syscall") +pub fn panic_output() -> Option { + if cfg!(feature = "wasm_syscall") { + Stderr::new().ok() + } else { + None + } } diff --git a/src/libstd/sys/windows/stdio.rs b/src/libstd/sys/windows/stdio.rs index c3a94698a0f..61e0db87ebe 100644 --- a/src/libstd/sys/windows/stdio.rs +++ b/src/libstd/sys/windows/stdio.rs @@ -228,6 +228,6 @@ pub fn is_ebadf(err: &io::Error) -> bool { // been seen to be acceptable. pub const STDIN_BUF_SIZE: usize = 8 * 1024; -pub fn stderr_prints_nothing() -> bool { - false +pub fn panic_output() -> Option { + Stderr::new().ok() } diff --git a/src/libstd/sys_common/util.rs b/src/libstd/sys_common/util.rs index a373e980b97..fc86a59d17f 100644 --- a/src/libstd/sys_common/util.rs +++ b/src/libstd/sys_common/util.rs @@ -10,14 +10,13 @@ use fmt; use io::prelude::*; -use sys::stdio::{Stderr, stderr_prints_nothing}; +use sys::stdio::panic_output; use thread; pub fn dumb_print(args: fmt::Arguments) { - if stderr_prints_nothing() { - return + if let Some(mut out) = panic_output() { + let _ = out.write_fmt(args); } - let _ = Stderr::new().map(|mut stderr| stderr.write_fmt(args)); } // Other platforms should use the appropriate platform-specific mechanism for -- 2.44.0