Skip to content

Commit e0a02bd

Browse files
author
Jethro Beekman
committed
Refactor stderr_prints_nothing into a more meaningful type
1 parent 13c9439 commit e0a02bd

File tree

7 files changed

+53
-42
lines changed

7 files changed

+53
-42
lines changed

src/libstd/panicking.rs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,26 @@ use intrinsics;
2929
use mem;
3030
use ptr;
3131
use raw;
32-
use sys::stdio::{Stderr, stderr_prints_nothing};
32+
use sys::stdio::{Stderr, panic_output};
3333
use sys_common::rwlock::RWLock;
3434
use sys_common::thread_info;
3535
use sys_common::util;
3636
use thread;
3737

38+
#[derive(Clone, Copy)]
39+
#[allow(dead_code)]
40+
pub enum PanicOutput {
41+
StdErr,
42+
}
43+
44+
impl PanicOutput {
45+
pub(crate) fn write<T, F: FnOnce(&mut Write) -> T>(self, write: F) {
46+
match self {
47+
PanicOutput::StdErr => Stderr::new().ok().map(|mut w| write(&mut w)),
48+
};
49+
}
50+
}
51+
3852
thread_local! {
3953
pub static LOCAL_STDERR: RefCell<Option<Box<dyn Write + Send>>> = {
4054
RefCell::new(None)
@@ -168,6 +182,11 @@ pub fn take_hook() -> Box<dyn Fn(&PanicInfo) + 'static + Sync + Send> {
168182
}
169183

170184
fn default_hook(info: &PanicInfo) {
185+
let out = match panic_output() {
186+
Some(out) => out,
187+
None => return
188+
};
189+
171190
#[cfg(feature = "backtrace")]
172191
use sys_common::backtrace;
173192

@@ -193,7 +212,6 @@ fn default_hook(info: &PanicInfo) {
193212
None => "Box<Any>",
194213
}
195214
};
196-
let mut err = Stderr::new().ok();
197215
let thread = thread_info::current_thread();
198216
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
199217

@@ -215,17 +233,14 @@ fn default_hook(info: &PanicInfo) {
215233
}
216234
};
217235

218-
let prev = LOCAL_STDERR.with(|s| s.borrow_mut().take());
219-
match (prev, err.as_mut()) {
220-
(Some(mut stderr), _) => {
221-
write(&mut *stderr);
222-
let mut s = Some(stderr);
223-
LOCAL_STDERR.with(|slot| {
224-
*slot.borrow_mut() = s.take();
225-
});
226-
}
227-
(None, Some(ref mut err)) => { write(err) }
228-
_ => {}
236+
if let Some(mut local) = LOCAL_STDERR.with(|s| s.borrow_mut().take()) {
237+
write(&mut *local);
238+
let mut s = Some(local);
239+
LOCAL_STDERR.with(|slot| {
240+
*slot.borrow_mut() = s.take();
241+
});
242+
} else {
243+
out.write(write);
229244
}
230245
}
231246

@@ -466,20 +481,11 @@ fn rust_panic_with_hook(payload: &mut dyn BoxMeUp,
466481
Location::internal_constructor(file, line, col),
467482
);
468483
HOOK_LOCK.read();
484+
info.set_payload(payload.get());
469485
match HOOK {
470-
// Some platforms know that printing to stderr won't ever actually
471-
// print anything, and if that's the case we can skip the default
472-
// hook.
473-
Hook::Default if stderr_prints_nothing() => {}
474-
Hook::Default => {
475-
info.set_payload(payload.get());
476-
default_hook(&info);
477-
}
478-
Hook::Custom(ptr) => {
479-
info.set_payload(payload.get());
480-
(*ptr)(&info);
481-
}
482-
}
486+
Hook::Default => default_hook(&info),
487+
Hook::Custom(ptr) => (*ptr)(&info),
488+
};
483489
HOOK_LOCK.read_unlock();
484490
}
485491

src/libstd/sys/cloudabi/stdio.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use io;
1212
use sys::cloudabi::abi;
13+
use panicking::PanicOutput;
1314

1415
pub struct Stdin(());
1516
pub struct Stdout(());
@@ -78,6 +79,6 @@ pub fn is_ebadf(err: &io::Error) -> bool {
7879

7980
pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
8081

81-
pub fn stderr_prints_nothing() -> bool {
82-
false
82+
pub fn panic_output() -> Option<PanicOutput> {
83+
Some(PanicOutput::StdErr)
8384
}

src/libstd/sys/redox/stdio.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use io;
1212
use sys::{cvt, syscall};
1313
use sys::fd::FileDesc;
14+
use panicking::PanicOutput;
1415

1516
pub struct Stdin(());
1617
pub struct Stdout(());
@@ -76,6 +77,6 @@ pub fn is_ebadf(err: &io::Error) -> bool {
7677

7778
pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
7879

79-
pub fn stderr_prints_nothing() -> bool {
80-
false
80+
pub fn panic_output() -> Option<PanicOutput> {
81+
Some(PanicOutput::StdErr)
8182
}

src/libstd/sys/unix/stdio.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use io;
1212
use libc;
1313
use sys::fd::FileDesc;
14+
use panicking::PanicOutput;
1415

1516
pub struct Stdin(());
1617
pub struct Stdout(());
@@ -76,6 +77,6 @@ pub fn is_ebadf(err: &io::Error) -> bool {
7677

7778
pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
7879

79-
pub fn stderr_prints_nothing() -> bool {
80-
false
80+
pub fn panic_output() -> Option<PanicOutput> {
81+
Some(PanicOutput::StdErr)
8182
}

src/libstd/sys/wasm/stdio.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use io;
1212
use sys::{ReadSysCall, WriteSysCall};
13+
use panicking::PanicOutput;
1314

1415
pub struct Stdin;
1516
pub struct Stdout;
@@ -70,6 +71,10 @@ pub fn is_ebadf(_err: &io::Error) -> bool {
7071
true
7172
}
7273

73-
pub fn stderr_prints_nothing() -> bool {
74-
!cfg!(feature = "wasm_syscall")
74+
pub fn panic_output() -> Option<PanicOutput> {
75+
if cfg!(feature = "wasm_syscall") {
76+
Some(PanicOutput::StdErr)
77+
} else {
78+
None
79+
}
7580
}

src/libstd/sys/windows/stdio.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use sync::Mutex;
2020
use sys::c;
2121
use sys::cvt;
2222
use sys::handle::Handle;
23+
use panicking::PanicOutput;
2324

2425
pub enum Output {
2526
Console(c::HANDLE),
@@ -228,6 +229,6 @@ pub fn is_ebadf(err: &io::Error) -> bool {
228229
// been seen to be acceptable.
229230
pub const STDIN_BUF_SIZE: usize = 8 * 1024;
230231

231-
pub fn stderr_prints_nothing() -> bool {
232-
false
232+
pub fn panic_output() -> Option<PanicOutput> {
233+
Some(PanicOutput::StdErr)
233234
}

src/libstd/sys_common/util.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@
99
// except according to those terms.
1010

1111
use fmt;
12-
use io::prelude::*;
13-
use sys::stdio::{Stderr, stderr_prints_nothing};
12+
use sys::stdio::panic_output;
1413
use thread;
1514

1615
pub fn dumb_print(args: fmt::Arguments) {
17-
if stderr_prints_nothing() {
18-
return
19-
}
20-
let _ = Stderr::new().map(|mut stderr| stderr.write_fmt(args));
16+
panic_output().map(|w| w.write(|out| out.write_fmt(args)));
2117
}
2218

2319
// Other platforms should use the appropriate platform-specific mechanism for

0 commit comments

Comments
 (0)