Skip to content

Commit aff7bd6

Browse files
committed
Merge set_panic and set_print into set_output_capture.
There were no use cases for setting them separately. Merging them simplifies some things.
1 parent 08b7cb7 commit aff7bd6

File tree

13 files changed

+57
-131
lines changed

13 files changed

+57
-131
lines changed

compiler/rustc_interface/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![feature(bool_to_option)]
22
#![feature(box_syntax)]
3-
#![feature(set_stdio)]
3+
#![feature(internal_output_capture)]
44
#![feature(nll)]
55
#![feature(generator_trait)]
66
#![feature(generators)]

compiler/rustc_interface/src/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
148148

149149
let main_handler = move || {
150150
rustc_span::with_session_globals(edition, || {
151-
io::set_panic(stderr.clone());
151+
io::set_output_capture(stderr.clone());
152152
f()
153153
})
154154
};
@@ -186,7 +186,7 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
186186
// on the new threads.
187187
let main_handler = move |thread: rayon::ThreadBuilder| {
188188
rustc_span::SESSION_GLOBALS.set(session_globals, || {
189-
io::set_panic(stderr.clone());
189+
io::set_output_capture(stderr.clone());
190190
thread.run()
191191
})
192192
};

library/std/src/io/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -269,20 +269,18 @@ pub use self::buffered::{BufReader, BufWriter, LineWriter};
269269
pub use self::cursor::Cursor;
270270
#[stable(feature = "rust1", since = "1.0.0")]
271271
pub use self::error::{Error, ErrorKind, Result};
272+
#[unstable(feature = "internal_output_capture", issue = "none")]
273+
#[doc(no_inline, hidden)]
274+
pub use self::stdio::set_output_capture;
272275
#[stable(feature = "rust1", since = "1.0.0")]
273276
pub use self::stdio::{stderr, stdin, stdout, Stderr, Stdin, Stdout};
274277
#[stable(feature = "rust1", since = "1.0.0")]
275278
pub use self::stdio::{StderrLock, StdinLock, StdoutLock};
276279
#[unstable(feature = "print_internals", issue = "none")]
277280
pub use self::stdio::{_eprint, _print};
278-
#[unstable(feature = "libstd_io_internals", issue = "42788")]
279-
#[doc(no_inline, hidden)]
280-
pub use self::stdio::{set_panic, set_print};
281281
#[stable(feature = "rust1", since = "1.0.0")]
282282
pub use self::util::{copy, empty, repeat, sink, Empty, Repeat, Sink};
283283

284-
pub(crate) use self::stdio::clone_io;
285-
286284
mod buffered;
287285
mod cursor;
288286
mod error;

library/std/src/io/stdio.rs

+27-85
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,29 @@ use crate::sync::{Arc, Mutex, MutexGuard};
1414
use crate::sys::stdio;
1515
use crate::sys_common;
1616
use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
17-
use crate::thread::LocalKey;
1817

1918
type LocalStream = Arc<Mutex<Vec<u8>>>;
2019

2120
thread_local! {
22-
/// Used by the test crate to capture the output of the print! and println! macros.
23-
static LOCAL_STDOUT: Cell<Option<LocalStream>> = {
21+
/// Used by the test crate to capture the output of the print macros and panics.
22+
static OUTPUT_CAPTURE: Cell<Option<LocalStream>> = {
2423
Cell::new(None)
2524
}
2625
}
2726

28-
thread_local! {
29-
/// Used by the test crate to capture the output of the eprint! and eprintln! macros, and panics.
30-
static LOCAL_STDERR: Cell<Option<LocalStream>> = {
31-
Cell::new(None)
32-
}
33-
}
34-
35-
/// Flag to indicate LOCAL_STDOUT and/or LOCAL_STDERR is used.
27+
/// Flag to indicate OUTPUT_CAPTURE is used.
3628
///
37-
/// If both are None and were never set on any thread, this flag is set to
38-
/// false, and both LOCAL_STDOUT and LOCAL_STDOUT can be safely ignored on all
39-
/// threads, saving some time and memory registering an unused thread local.
29+
/// If it is None and was never set on any thread, this flag is set to false,
30+
/// and OUTPUT_CAPTURE can be safely ignored on all threads, saving some time
31+
/// and memory registering an unused thread local.
4032
///
41-
/// Note about memory ordering: This contains information about whether two
42-
/// thread local variables might be in use. Although this is a global flag, the
33+
/// Note about memory ordering: This contains information about whether a
34+
/// thread local variable might be in use. Although this is a global flag, the
4335
/// memory ordering between threads does not matter: we only want this flag to
44-
/// have a consistent order between set_print/set_panic and print_to *within
36+
/// have a consistent order between set_output_capture and print_to *within
4537
/// the same thread*. Within the same thread, things always have a perfectly
4638
/// consistent order. So Ordering::Relaxed is fine.
47-
static LOCAL_STREAMS: AtomicBool = AtomicBool::new(false);
39+
static OUTPUT_CAPTURE_USED: AtomicBool = AtomicBool::new(false);
4840

4941
/// A handle to a raw instance of the standard input stream of this process.
5042
///
@@ -890,70 +882,24 @@ impl fmt::Debug for StderrLock<'_> {
890882
}
891883
}
892884

893-
/// Resets the thread-local stderr handle to the specified writer
894-
///
895-
/// This will replace the current thread's stderr handle, returning the old
896-
/// handle. All future calls to `panic!` and friends will emit their output to
897-
/// this specified handle.
898-
///
899-
/// Note that this does not need to be called for all new threads; the default
900-
/// output handle is to the process's stderr stream.
901-
#[unstable(
902-
feature = "set_stdio",
903-
reason = "this function may disappear completely or be replaced \
904-
with a more general mechanism",
905-
issue = "none"
906-
)]
907-
#[doc(hidden)]
908-
pub fn set_panic(sink: Option<LocalStream>) -> Option<LocalStream> {
909-
if sink.is_none() && !LOCAL_STREAMS.load(Ordering::Relaxed) {
910-
// LOCAL_STDERR is definitely None since LOCAL_STREAMS is false.
911-
return None;
912-
}
913-
LOCAL_STREAMS.store(true, Ordering::Relaxed);
914-
LOCAL_STDERR.with(move |slot| slot.replace(sink))
915-
}
916-
917-
/// Resets the thread-local stdout handle to the specified writer
918-
///
919-
/// This will replace the current thread's stdout handle, returning the old
920-
/// handle. All future calls to `print!` and friends will emit their output to
921-
/// this specified handle.
922-
///
923-
/// Note that this does not need to be called for all new threads; the default
924-
/// output handle is to the process's stdout stream.
885+
/// Sets the thread-local output capture buffer and returns the old one.
925886
#[unstable(
926-
feature = "set_stdio",
927-
reason = "this function may disappear completely or be replaced \
928-
with a more general mechanism",
887+
feature = "internal_output_capture",
888+
reason = "this function is meant for use in the test crate \
889+
and may disappear in the future",
929890
issue = "none"
930891
)]
931892
#[doc(hidden)]
932-
pub fn set_print(sink: Option<LocalStream>) -> Option<LocalStream> {
933-
if sink.is_none() && !LOCAL_STREAMS.load(Ordering::Relaxed) {
934-
// LOCAL_STDOUT is definitely None since LOCAL_STREAMS is false.
893+
pub fn set_output_capture(sink: Option<LocalStream>) -> Option<LocalStream> {
894+
if sink.is_none() && !OUTPUT_CAPTURE_USED.load(Ordering::Relaxed) {
895+
// OUTPUT_CAPTURE is definitely None since OUTPUT_CAPTURE_USED is false.
935896
return None;
936897
}
937-
LOCAL_STREAMS.store(true, Ordering::Relaxed);
938-
LOCAL_STDOUT.with(move |slot| slot.replace(sink))
939-
}
940-
941-
pub(crate) fn clone_io() -> (Option<LocalStream>, Option<LocalStream>) {
942-
// Don't waste time when LOCAL_{STDOUT,STDERR} are definitely None.
943-
if !LOCAL_STREAMS.load(Ordering::Relaxed) {
944-
return (None, None);
945-
}
946-
947-
let clone = |cell: &Cell<Option<LocalStream>>| {
948-
let s = cell.take();
949-
cell.set(s.clone());
950-
s
951-
};
952-
953-
(LOCAL_STDOUT.with(clone), LOCAL_STDERR.with(clone))
898+
OUTPUT_CAPTURE_USED.store(true, Ordering::Relaxed);
899+
OUTPUT_CAPTURE.with(move |slot| slot.replace(sink))
954900
}
955901

956-
/// Write `args` to output stream `local_s` if possible, `global_s`
902+
/// Write `args` to the capture buffer if enabled and possible, or `global_s`
957903
/// otherwise. `label` identifies the stream in a panic message.
958904
///
959905
/// This function is used to print error messages, so it takes extra
@@ -963,16 +909,12 @@ pub(crate) fn clone_io() -> (Option<LocalStream>, Option<LocalStream>) {
963909
/// thread, it will just fall back to the global stream.
964910
///
965911
/// However, if the actual I/O causes an error, this function does panic.
966-
fn print_to<T>(
967-
args: fmt::Arguments<'_>,
968-
local_s: &'static LocalKey<Cell<Option<LocalStream>>>,
969-
global_s: fn() -> T,
970-
label: &str,
971-
) where
912+
fn print_to<T>(args: fmt::Arguments<'_>, global_s: fn() -> T, label: &str)
913+
where
972914
T: Write,
973915
{
974-
if LOCAL_STREAMS.load(Ordering::Relaxed)
975-
&& local_s.try_with(|s| {
916+
if OUTPUT_CAPTURE_USED.load(Ordering::Relaxed)
917+
&& OUTPUT_CAPTURE.try_with(|s| {
976918
// Note that we completely remove a local sink to write to in case
977919
// our printing recursively panics/prints, so the recursive
978920
// panic/print goes to the global sink instead of our local sink.
@@ -982,7 +924,7 @@ fn print_to<T>(
982924
})
983925
}) == Ok(Some(()))
984926
{
985-
// Succesfully wrote to local stream.
927+
// Succesfully wrote to capture buffer.
986928
return;
987929
}
988930

@@ -999,7 +941,7 @@ fn print_to<T>(
999941
#[doc(hidden)]
1000942
#[cfg(not(test))]
1001943
pub fn _print(args: fmt::Arguments<'_>) {
1002-
print_to(args, &LOCAL_STDOUT, stdout, "stdout");
944+
print_to(args, stdout, "stdout");
1003945
}
1004946

1005947
#[unstable(
@@ -1010,7 +952,7 @@ pub fn _print(args: fmt::Arguments<'_>) {
1010952
#[doc(hidden)]
1011953
#[cfg(not(test))]
1012954
pub fn _eprint(args: fmt::Arguments<'_>) {
1013-
print_to(args, &LOCAL_STDERR, stderr, "stderr");
955+
print_to(args, stderr, "stderr");
1014956
}
1015957

1016958
#[cfg(test)]

library/std/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@
207207
// std may use features in a platform-specific way
208208
#![allow(unused_features)]
209209
#![cfg_attr(not(bootstrap), feature(rustc_allow_const_fn_unstable))]
210-
#![cfg_attr(test, feature(print_internals, set_stdio, update_panic_count))]
210+
#![cfg_attr(test, feature(internal_output_capture, print_internals, update_panic_count))]
211211
#![cfg_attr(
212212
all(target_vendor = "fortanix", target_env = "sgx"),
213213
feature(slice_index_methods, coerce_unsized, sgx_platform)

library/std/src/panicking.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ use crate::sys_common::{thread_info, util};
2424
use crate::thread;
2525

2626
#[cfg(not(test))]
27-
use crate::io::set_panic;
27+
use crate::io::set_output_capture;
2828
// make sure to use the stderr output configured
2929
// by libtest in the real copy of std
3030
#[cfg(test)]
31-
use realstd::io::set_panic;
31+
use realstd::io::set_output_capture;
3232

3333
// Binary interface to the panic runtime that the standard library depends on.
3434
//
@@ -218,9 +218,9 @@ fn default_hook(info: &PanicInfo<'_>) {
218218
}
219219
};
220220

221-
if let Some(local) = set_panic(None) {
221+
if let Some(local) = set_output_capture(None) {
222222
write(&mut *local.lock().unwrap_or_else(|e| e.into_inner()));
223-
set_panic(Some(local));
223+
set_output_capture(Some(local));
224224
} else if let Some(mut out) = panic_output() {
225225
write(&mut out);
226226
}

library/std/src/thread/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -456,15 +456,15 @@ impl Builder {
456456
let my_packet: Arc<UnsafeCell<Option<Result<T>>>> = Arc::new(UnsafeCell::new(None));
457457
let their_packet = my_packet.clone();
458458

459-
let (stdout, stderr) = crate::io::clone_io();
459+
let output_capture = crate::io::set_output_capture(None);
460+
crate::io::set_output_capture(output_capture.clone());
460461

461462
let main = move || {
462463
if let Some(name) = their_thread.cname() {
463464
imp::Thread::set_name(name);
464465
}
465466

466-
crate::io::set_print(stdout);
467-
crate::io::set_panic(stderr);
467+
crate::io::set_output_capture(output_capture);
468468

469469
// SAFETY: the stack guard passed is the one for the current thread.
470470
// This means the current thread's stack and the new thread's stack

library/test/src/bench.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,14 @@ where
184184
let mut bs = Bencher { mode: BenchMode::Auto, summary: None, bytes: 0 };
185185

186186
let data = Arc::new(Mutex::new(Vec::new()));
187-
let oldio = if !nocapture {
188-
Some((io::set_print(Some(data.clone())), io::set_panic(Some(data.clone()))))
189-
} else {
190-
None
191-
};
187+
188+
if !nocapture {
189+
io::set_output_capture(Some(data.clone()));
190+
}
192191

193192
let result = catch_unwind(AssertUnwindSafe(|| bs.bench(f)));
194193

195-
if let Some((printio, panicio)) = oldio {
196-
io::set_print(printio);
197-
io::set_panic(panicio);
198-
}
194+
io::set_output_capture(None);
199195

200196
let test_result = match result {
201197
//bs.bench(f) {

library/test/src/lib.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#![feature(nll)]
2626
#![feature(bool_to_option)]
2727
#![feature(available_concurrency)]
28-
#![feature(set_stdio)]
28+
#![feature(internal_output_capture)]
2929
#![feature(panic_unwind)]
3030
#![feature(staged_api)]
3131
#![feature(termination_trait_lib)]
@@ -530,11 +530,9 @@ fn run_test_in_process(
530530
// Buffer for capturing standard I/O
531531
let data = Arc::new(Mutex::new(Vec::new()));
532532

533-
let oldio = if !nocapture {
534-
Some((io::set_print(Some(data.clone())), io::set_panic(Some(data.clone()))))
535-
} else {
536-
None
537-
};
533+
if !nocapture {
534+
io::set_output_capture(Some(data.clone()));
535+
}
538536

539537
let start = report_time.then(Instant::now);
540538
let result = catch_unwind(AssertUnwindSafe(testfn));
@@ -543,10 +541,7 @@ fn run_test_in_process(
543541
TestExecTime(duration)
544542
});
545543

546-
if let Some((printio, panicio)) = oldio {
547-
io::set_print(printio);
548-
io::set_panic(panicio);
549-
}
544+
io::set_output_capture(None);
550545

551546
let test_result = match result {
552547
Ok(()) => calc_result(&desc, Ok(()), &time_opts, &exec_time),

src/doc/unstable-book/src/library-features/libstd-io-internals.md src/doc/unstable-book/src/library-features/internal-output-capture.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# `libstd_io_internals`
1+
# `internal_output_capture`
22

33
This feature is internal to the Rust compiler and is not intended for general use.
44

src/doc/unstable-book/src/library-features/set-stdio.md

-5
This file was deleted.

src/test/ui/panic-while-printing.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// run-pass
22
// ignore-emscripten no subprocess support
33

4-
#![feature(set_stdio)]
4+
#![feature(internal_output_capture)]
55

66
use std::fmt;
77
use std::fmt::{Display, Formatter};
8-
use std::io::set_panic;
8+
use std::io::set_output_capture;
99
use std::sync::{Arc, Mutex};
1010

1111
pub struct A;
@@ -17,7 +17,7 @@ impl Display for A {
1717
}
1818

1919
fn main() {
20-
set_panic(Some(Arc::new(Mutex::new(Vec::new()))));
20+
set_output_capture(Some(Arc::new(Mutex::new(Vec::new()))));
2121
assert!(std::panic::catch_unwind(|| {
2222
eprintln!("{}", A);
2323
})

src/test/ui/threads-sendsync/task-stderr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-pass
22
// ignore-emscripten no threads support
33

4-
#![feature(set_stdio)]
4+
#![feature(internal_output_capture)]
55

66
use std::io;
77
use std::str;
@@ -13,7 +13,7 @@ fn main() {
1313
let res = thread::Builder::new().spawn({
1414
let data = data.clone();
1515
move || {
16-
io::set_panic(Some(data));
16+
io::set_output_capture(Some(data));
1717
panic!("Hello, world!")
1818
}
1919
}).unwrap().join();

0 commit comments

Comments
 (0)