Skip to content

Commit 030b1ed

Browse files
author
Jethro Beekman
committed
Refactor stderr_prints_nothing into a more modular function
1 parent 367e783 commit 030b1ed

File tree

7 files changed

+28
-29
lines changed

7 files changed

+28
-29
lines changed

src/libstd/panicking.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use intrinsics;
2929
use mem;
3030
use ptr;
3131
use raw;
32-
use sys::stdio::{Stderr, stderr_prints_nothing};
32+
use sys::stdio::panic_output;
3333
use sys_common::rwlock::RWLock;
3434
use sys_common::thread_info;
3535
use sys_common::util;
@@ -193,7 +193,6 @@ fn default_hook(info: &PanicInfo) {
193193
None => "Box<Any>",
194194
}
195195
};
196-
let mut err = Stderr::new().ok();
197196
let thread = thread_info::current_thread();
198197
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
199198

@@ -215,17 +214,14 @@ fn default_hook(info: &PanicInfo) {
215214
}
216215
};
217216

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-
_ => {}
217+
if let Some(mut local) = LOCAL_STDERR.with(|s| s.borrow_mut().take()) {
218+
write(&mut *local);
219+
let mut s = Some(local);
220+
LOCAL_STDERR.with(|slot| {
221+
*slot.borrow_mut() = s.take();
222+
});
223+
} else if let Some(mut out) = panic_output() {
224+
write(&mut out);
229225
}
230226
}
231227

@@ -485,7 +481,7 @@ fn rust_panic_with_hook(payload: &mut dyn BoxMeUp,
485481
// Some platforms know that printing to stderr won't ever actually
486482
// print anything, and if that's the case we can skip the default
487483
// hook.
488-
Hook::Default if stderr_prints_nothing() => {}
484+
Hook::Default if panic_output().is_none() => {}
489485
Hook::Default => {
490486
info.set_payload(payload.get());
491487
default_hook(&info);
@@ -494,7 +490,7 @@ fn rust_panic_with_hook(payload: &mut dyn BoxMeUp,
494490
info.set_payload(payload.get());
495491
(*ptr)(&info);
496492
}
497-
}
493+
};
498494
HOOK_LOCK.read_unlock();
499495
}
500496

src/libstd/sys/cloudabi/stdio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ pub fn is_ebadf(err: &io::Error) -> bool {
7878

7979
pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
8080

81-
pub fn stderr_prints_nothing() -> bool {
82-
false
81+
pub fn panic_output() -> Option<impl io::Write> {
82+
Stderr::new().ok()
8383
}

src/libstd/sys/redox/stdio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,6 @@ pub fn is_ebadf(err: &io::Error) -> bool {
7676

7777
pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
7878

79-
pub fn stderr_prints_nothing() -> bool {
80-
false
79+
pub fn panic_output() -> Option<impl io::Write> {
80+
Stderr::new().ok()
8181
}

src/libstd/sys/unix/stdio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,6 @@ pub fn is_ebadf(err: &io::Error) -> bool {
7676

7777
pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
7878

79-
pub fn stderr_prints_nothing() -> bool {
80-
false
79+
pub fn panic_output() -> Option<impl io::Write> {
80+
Stderr::new().ok()
8181
}

src/libstd/sys/wasm/stdio.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ pub fn is_ebadf(_err: &io::Error) -> bool {
7070
true
7171
}
7272

73-
pub fn stderr_prints_nothing() -> bool {
74-
!cfg!(feature = "wasm_syscall")
73+
pub fn panic_output() -> Option<impl io::Write> {
74+
if cfg!(feature = "wasm_syscall") {
75+
Stderr::new().ok()
76+
} else {
77+
None
78+
}
7579
}

src/libstd/sys/windows/stdio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,6 @@ pub fn is_ebadf(err: &io::Error) -> bool {
228228
// been seen to be acceptable.
229229
pub const STDIN_BUF_SIZE: usize = 8 * 1024;
230230

231-
pub fn stderr_prints_nothing() -> bool {
232-
false
231+
pub fn panic_output() -> Option<impl io::Write> {
232+
Stderr::new().ok()
233233
}

src/libstd/sys_common/util.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010

1111
use fmt;
1212
use io::prelude::*;
13-
use sys::stdio::{Stderr, stderr_prints_nothing};
13+
use sys::stdio::panic_output;
1414
use thread;
1515

1616
pub fn dumb_print(args: fmt::Arguments) {
17-
if stderr_prints_nothing() {
18-
return
17+
if let Some(mut out) = panic_output() {
18+
let _ = out.write_fmt(args);
1919
}
20-
let _ = Stderr::new().map(|mut stderr| stderr.write_fmt(args));
2120
}
2221

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

0 commit comments

Comments
 (0)