Skip to content

Commit 51136bd

Browse files
committed
Buffer panic errors in other threads
1 parent 57a28d8 commit 51136bd

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#![feature(quote)]
6161
#![feature(refcell_replace_swap)]
6262
#![feature(rustc_diagnostic_macros)]
63+
#![feature(set_stdio)]
6364
#![feature(slice_patterns)]
6465
#![feature(specialization)]
6566
#![feature(unboxed_closures)]

src/librustc/ty/context.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ use std::hash::{Hash, Hasher};
6363
use std::mem;
6464
use std::ops::Deref;
6565
use std::iter;
66+
use std::io;
67+
use std::io::Write;
6668
use std::sync::mpsc;
67-
use std::sync::Arc;
69+
use std::sync::{Mutex, Arc};
6870
use syntax::abi;
6971
use syntax::ast::{self, Name, NodeId};
7072
use syntax::attr;
@@ -882,6 +884,18 @@ pub struct GlobalCtxt<'tcx> {
882884
output_filenames: Arc<OutputFilenames>,
883885
}
884886

887+
scoped_thread_local!(pub static PANIC_SINK: Arc<Mutex<Vec<u8>>>);
888+
889+
pub struct Sink(pub Arc<Mutex<Vec<u8>>>);
890+
impl Write for Sink {
891+
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
892+
Write::write(&mut *self.0.lock().unwrap(), data)
893+
}
894+
fn flush(&mut self) -> io::Result<()> {
895+
Ok(())
896+
}
897+
}
898+
885899
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
886900
pub fn query(self) -> &'a maps::QueryJob<'gcx> {
887901
self.query
@@ -1191,19 +1205,26 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11911205
let (r, pool_end) = try_with(&PROFQ_CHAN, |prof_chan| {
11921206
try_with(&syntax::GLOBALS, |syntax_globals| {
11931207
try_with(&syntax_pos::GLOBALS, |syntax_pos_globals| {
1194-
let main_handler = move |worker: &mut FnMut()| {
1195-
maybe_set(&PROFQ_CHAN, prof_chan, || {
1196-
maybe_set(&syntax::GLOBALS, syntax_globals, || {
1197-
maybe_set(&syntax_pos::GLOBALS, syntax_pos_globals, || {
1198-
tls::enter_global(gcx, |_| {
1199-
worker();
1208+
try_with(&PANIC_SINK, |panic_sink| {
1209+
let main_handler = move |worker: &mut FnMut()| {
1210+
maybe_set(&PROFQ_CHAN, prof_chan, || {
1211+
maybe_set(&syntax::GLOBALS, syntax_globals, || {
1212+
maybe_set(&syntax_pos::GLOBALS, syntax_pos_globals, || {
1213+
if let Some(sink_data) = panic_sink {
1214+
let err = Sink(sink_data.clone());
1215+
io::set_panic(Some(box err));
1216+
}
1217+
1218+
tls::enter_global(gcx, |_| {
1219+
worker();
1220+
})
12001221
})
12011222
})
12021223
})
1203-
})
1204-
};
1224+
};
12051225

1206-
rayon::ThreadPool::scoped_pool(config, main_handler, with_pool).unwrap()
1226+
rayon::ThreadPool::scoped_pool(config, main_handler, with_pool).unwrap()
1227+
})
12071228
})
12081229
})
12091230
});

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub use self::binding::BindingMode;
7878
pub use self::binding::BindingMode::*;
7979

8080
pub use self::context::{TyCtxt, GlobalArenas, AllArenas, tls, keep_local};
81-
pub use self::context::{Lift, TypeckTables};
81+
pub use self::context::{Lift, TypeckTables, PANIC_SINK, Sink};
8282

8383
pub use self::instance::{Instance, InstanceDef};
8484

src/librustc_driver/lib.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ use rustc::session::config::nightly_options;
7373
use rustc::session::{early_error, early_warn};
7474
use rustc::lint::Lint;
7575
use rustc::lint;
76+
use rustc::ty::{PANIC_SINK, Sink};
7677
use rustc::middle::cstore::CrateStore;
7778
use rustc_metadata::locator;
7879
use rustc_metadata::cstore::CStore;
@@ -1212,22 +1213,14 @@ pub fn in_rustc_thread<F, R>(f: F) -> Result<R, Box<Any + Send>>
12121213
/// The diagnostic emitter yielded to the procedure should be used for reporting
12131214
/// errors of the compiler.
12141215
pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
1215-
struct Sink(Arc<Mutex<Vec<u8>>>);
1216-
impl Write for Sink {
1217-
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
1218-
Write::write(&mut *self.0.lock().unwrap(), data)
1219-
}
1220-
fn flush(&mut self) -> io::Result<()> {
1221-
Ok(())
1222-
}
1223-
}
1224-
12251216
let data = Arc::new(Mutex::new(Vec::new()));
12261217
let err = Sink(data.clone());
12271218

12281219
let result = in_rustc_thread(move || {
12291220
io::set_panic(Some(box err));
1230-
f()
1221+
PANIC_SINK.set(data.clone(), || {
1222+
f()
1223+
})
12311224
});
12321225

12331226
if let Err(value) = result {

0 commit comments

Comments
 (0)