提交 95942155 编写于 作者: A Alex Crichton

Merge branch 'no-stderr-sink' of https://github.com/Zoxc/rust into rollup

......@@ -2837,8 +2837,8 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
(&None, &Some(..), Closed) => "RangeToInclusive",
(&Some(..), &Some(..), Closed) => "RangeInclusive",
(_, &None, Closed) =>
panic!(self.diagnostic().span_fatal(
e.span, "inclusive range with no end")),
self.diagnostic().span_fatal(
e.span, "inclusive range with no end").raise(),
};
let fields =
......
......@@ -1365,7 +1365,7 @@ pub fn build_target_config(opts: &Options, sp: &Handler) -> Config {
sp.struct_fatal(&format!("Error loading target specification: {}", e))
.help("Use `--print target-list` for a list of built-in targets")
.emit();
panic!(FatalError);
FatalError.raise();
}
};
......@@ -1373,8 +1373,8 @@ pub fn build_target_config(opts: &Options, sp: &Handler) -> Config {
"16" => (ast::IntTy::I16, ast::UintTy::U16),
"32" => (ast::IntTy::I32, ast::UintTy::U32),
"64" => (ast::IntTy::I64, ast::UintTy::U64),
w => panic!(sp.fatal(&format!("target specification was invalid: \
unrecognized target-pointer-width {}", w))),
w => sp.fatal(&format!("target specification was invalid: \
unrecognized target-pointer-width {}", w)).raise(),
};
Config {
......
......@@ -250,7 +250,7 @@ pub fn struct_fatal<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
}
pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
panic!(self.diagnostic().span_fatal(sp, msg))
self.diagnostic().span_fatal(sp, msg).raise()
}
pub fn span_fatal_with_code<S: Into<MultiSpan>>(
&self,
......@@ -258,10 +258,10 @@ pub fn span_fatal_with_code<S: Into<MultiSpan>>(
msg: &str,
code: DiagnosticId,
) -> ! {
panic!(self.diagnostic().span_fatal_with_code(sp, msg, code))
self.diagnostic().span_fatal_with_code(sp, msg, code).raise()
}
pub fn fatal(&self, msg: &str) -> ! {
panic!(self.diagnostic().fatal(msg))
self.diagnostic().fatal(msg).raise()
}
pub fn span_err_or_warn<S: Into<MultiSpan>>(&self, is_warning: bool, sp: S, msg: &str) {
if is_warning {
......@@ -943,7 +943,7 @@ pub fn build_session_(sopts: config::Options,
let host = match Target::search(config::host_triple()) {
Ok(t) => t,
Err(e) => {
panic!(span_diagnostic.fatal(&format!("Error loading host specification: {}", e)));
span_diagnostic.fatal(&format!("Error loading host specification: {}", e)).raise();
}
};
let target_cfg = config::build_target_config(&sopts, &span_diagnostic);
......@@ -969,7 +969,7 @@ pub fn build_session_(sopts: config::Options,
let working_dir = match env::current_dir() {
Ok(dir) => dir,
Err(e) => {
panic!(p_s.span_diagnostic.fatal(&format!("Current directory is invalid: {}", e)))
p_s.span_diagnostic.fatal(&format!("Current directory is invalid: {}", e)).raise()
}
};
let working_dir = file_path_mapping.map_prefix(working_dir);
......@@ -1100,7 +1100,7 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
};
let handler = errors::Handler::with_emitter(true, false, emitter);
handler.emit(&MultiSpan::new(), msg, errors::Level::Fatal);
panic!(errors::FatalError);
errors::FatalError.raise();
}
pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
......
......@@ -87,11 +87,11 @@
use std::ffi::OsString;
use std::io::{self, Read, Write};
use std::iter::repeat;
use std::panic;
use std::path::PathBuf;
use std::process::{self, Command, Stdio};
use std::rc::Rc;
use std::str;
use std::sync::{Arc, Mutex};
use std::thread;
use syntax::ast;
......@@ -168,7 +168,7 @@ pub fn run<F>(run_compiler: F) -> isize
handler.emit(&MultiSpan::new(),
"aborting due to previous error(s)",
errors::Level::Fatal);
exit_on_err();
panic::resume_unwind(Box::new(errors::FatalErrorMarker));
}
}
}
......@@ -1228,27 +1228,16 @@ pub fn in_rustc_thread<F, R>(f: F) -> Result<R, Box<Any + Send>>
/// The diagnostic emitter yielded to the procedure should be used for reporting
/// errors of the compiler.
pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
struct Sink(Arc<Mutex<Vec<u8>>>);
impl Write for Sink {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
Write::write(&mut *self.0.lock().unwrap(), data)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let data = Arc::new(Mutex::new(Vec::new()));
let err = Sink(data.clone());
let result = in_rustc_thread(move || {
io::set_panic(Some(box err));
f()
});
if let Err(value) = result {
// Thread panicked without emitting a fatal diagnostic
if !value.is::<errors::FatalError>() {
if !value.is::<errors::FatalErrorMarker>() {
// Emit a newline
eprintln!("");
let emitter =
Box::new(errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
None,
......@@ -1273,22 +1262,12 @@ fn flush(&mut self) -> io::Result<()> {
&note,
errors::Level::Note);
}
eprintln!("{}", str::from_utf8(&data.lock().unwrap()).unwrap());
}
exit_on_err();
panic::resume_unwind(Box::new(errors::FatalErrorMarker));
}
}
fn exit_on_err() -> ! {
// Panic so the process returns a failure code, but don't pollute the
// output with some unnecessary panic messages, we've already
// printed everything that we needed to.
io::set_panic(Some(box io::sink()));
panic!();
}
#[cfg(stage0)]
pub fn diagnostics_registry() -> errors::registry::Registry {
use errors::registry::Registry;
......
......@@ -19,6 +19,7 @@
#![cfg_attr(unix, feature(libc))]
#![feature(conservative_impl_trait)]
#![feature(i128_type)]
#![feature(optin_builtin_traits)]
extern crate term;
#[cfg(unix)]
......@@ -44,6 +45,7 @@
use std::{error, fmt};
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
use std::panic;
mod diagnostic;
mod diagnostic_builder;
......@@ -201,6 +203,18 @@ fn push_trailing(buf: &mut String,
#[must_use]
pub struct FatalError;
pub struct FatalErrorMarker;
// Don't implement Send on FatalError. This makes it impossible to panic!(FatalError).
// We don't want to invoke the panic handler and print a backtrace for fatal errors.
impl !Send for FatalError {}
impl FatalError {
pub fn raise(self) -> ! {
panic::resume_unwind(Box::new(FatalErrorMarker))
}
}
impl fmt::Display for FatalError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "parser fatal error")
......@@ -539,7 +553,7 @@ pub fn abort_if_errors(&self) {
}
}
panic!(self.fatal(&s));
self.fatal(&s).raise();
}
pub fn emit(&self, msp: &MultiSpan, msg: &str, lvl: Level) {
if lvl == Warning && !self.flags.can_emit_warnings {
......
......@@ -155,7 +155,7 @@ fn get_llvm_opt_size(optimize: config::OptLevel) -> llvm::CodeGenOptSize {
pub fn create_target_machine(sess: &Session) -> TargetMachineRef {
target_machine_factory(sess)().unwrap_or_else(|err| {
panic!(llvm_err(sess.diagnostic(), err))
llvm_err(sess.diagnostic(), err).raise()
})
}
......@@ -589,7 +589,7 @@ fn generate_lto_work(cgcx: &CodegenContext,
"generate lto")
}).unwrap_or(Timeline::noop());
let lto_modules = lto::run(cgcx, modules, &mut timeline)
.unwrap_or_else(|e| panic!(e));
.unwrap_or_else(|e| e.raise());
lto_modules.into_iter().map(|module| {
let cost = module.cost();
......
......@@ -786,7 +786,7 @@ pub fn struct_span_fatal<S: Into<MultiSpan>>(&self,
/// substitute; we never hit resolve/type-checking so the dummy
/// value doesn't have to match anything)
pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
panic!(self.parse_sess.span_diagnostic.span_fatal(sp, msg));
self.parse_sess.span_diagnostic.span_fatal(sp, msg).raise();
}
/// Emit `msg` attached to `sp`, without immediately stopping
......
......@@ -455,7 +455,7 @@ fn expand_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Optio
suggested_limit));
err.emit();
self.cx.trace_macros_diag();
panic!(FatalError);
FatalError.raise();
}
Some(result)
......
......@@ -116,9 +116,10 @@ fn make_items(mut self: Box<ExpandResult<'a>>)
while self.p.token != token::Eof {
match panictry!(self.p.parse_item()) {
Some(item) => ret.push(item),
None => panic!(self.p.diagnostic().span_fatal(self.p.span,
None => self.p.diagnostic().span_fatal(self.p.span,
&format!("expected item, found `{}`",
self.p.this_token_to_string())))
self.p.this_token_to_string()))
.raise()
}
}
Some(ret)
......
......@@ -573,7 +573,7 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
Some(i) => token::NtItem(i),
None => {
p.fatal("expected an item keyword").emit();
panic!(FatalError);
FatalError.raise();
}
},
"block" => token::NtBlock(panictry!(p.parse_block())),
......@@ -581,7 +581,7 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
Some(s) => token::NtStmt(s),
None => {
p.fatal("expected a statement").emit();
panic!(FatalError);
FatalError.raise();
}
},
"pat" => token::NtPat(panictry!(p.parse_pat())),
......@@ -597,7 +597,7 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
let token_str = pprust::token_to_string(&p.token);
p.fatal(&format!("expected ident, found {}",
&token_str[..])).emit();
panic!(FatalError)
FatalError.raise()
}
},
"path" => token::NtPath(panictry!(p.parse_path_common(PathStyle::Type, false))),
......
......@@ -222,10 +222,10 @@ pub fn compile(sess: &ParseSess, features: &RefCell<Features>, def: &ast::Item)
Success(m) => m,
Failure(sp, tok) => {
let s = parse_failure_msg(tok);
panic!(sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s));
sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s).raise();
}
Error(sp, s) => {
panic!(sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s));
sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s).raise();
}
};
......
......@@ -1954,7 +1954,7 @@ fn check(self, handler: &Handler) {
.span_note(ca_span, "`#![feature(custom_attribute)]` declared here")
.emit();
panic!(FatalError);
FatalError.raise();
}
if let (Some(span), None) = (self.copy_closures, self.clone_closures) {
......@@ -1963,7 +1963,7 @@ fn check(self, handler: &Handler) {
.span_note(span, "`#![feature(copy_closures)]` declared here")
.emit();
panic!(FatalError);
FatalError.raise();
}
}
}
......
......@@ -54,7 +54,7 @@
Ok(e) => e,
Err(mut e) => {
e.emit();
panic!(FatalError);
FatalError.raise()
}
}
})
......
......@@ -265,7 +265,7 @@ fn read_block_comment(rdr: &mut StringReader,
while level > 0 {
debug!("=== block comment level {}", level);
if rdr.is_eof() {
panic!(rdr.fatal("unterminated block comment"));
rdr.fatal("unterminated block comment").raise();
}
if rdr.ch_is('\n') {
trim_whitespace_prefix_and_push_line(&mut lines, curr_line, col);
......
......@@ -90,7 +90,7 @@ fn unwrap_or_abort(&mut self, res: Result<TokenAndSpan, ()>) -> TokenAndSpan {
Ok(tok) => tok,
Err(_) => {
self.emit_fatal_errors();
panic!(FatalError);
FatalError.raise();
}
}
}
......@@ -191,7 +191,7 @@ pub fn new(sess: &'a ParseSess, filemap: Rc<syntax_pos::FileMap>) -> Self {
let mut sr = StringReader::new_raw(sess, filemap);
if sr.advance_token().is_err() {
sr.emit_fatal_errors();
panic!(FatalError);
FatalError.raise();
}
sr
}
......@@ -216,7 +216,7 @@ pub fn retokenize(sess: &'a ParseSess, mut span: Span) -> Self {
if sr.advance_token().is_err() {
sr.emit_fatal_errors();
panic!(FatalError);
FatalError.raise();
}
sr
}
......@@ -647,7 +647,7 @@ fn scan_block_comment(&mut self) -> Option<TokenAndSpan> {
"unterminated block comment"
};
let last_bpos = self.pos;
panic!(self.fatal_span_(start_bpos, last_bpos, msg));
self.fatal_span_(start_bpos, last_bpos, msg).raise();
}
let n = self.ch.unwrap();
match n {
......@@ -808,9 +808,9 @@ fn scan_hex_digits(&mut self, n_digits: usize, delim: char, below_0x7f_only: boo
for _ in 0..n_digits {
if self.is_eof() {
let last_bpos = self.pos;
panic!(self.fatal_span_(start_bpos,
last_bpos,
"unterminated numeric character escape"));
self.fatal_span_(start_bpos,
last_bpos,
"unterminated numeric character escape").raise();
}
if self.ch_is(delim) {
let last_bpos = self.pos;
......@@ -1025,9 +1025,9 @@ fn scan_unicode_escape(&mut self, delim: char) -> bool {
}
},
None => {
panic!(self.fatal_span_(start_bpos,
self.pos,
"unterminated unicode escape (found EOF)"));
self.fatal_span_(start_bpos,
self.pos,
"unterminated unicode escape (found EOF)").raise();
}
}
self.bump();
......@@ -1283,9 +1283,9 @@ fn next_token_inner(&mut self) -> Result<token::Token, ()> {
// lifetimes shouldn't end with a single quote
// if we find one, then this is an invalid character literal
if self.ch_is('\'') {
panic!(self.fatal_span_verbose(
start_with_quote, self.next_pos,
String::from("character literal may only contain one codepoint")));
self.fatal_span_verbose(start_with_quote, self.next_pos,
String::from("character literal may only contain one codepoint"))
.raise();
}
......@@ -1332,9 +1332,8 @@ fn next_token_inner(&mut self) -> Result<token::Token, ()> {
break;
}
}
panic!(self.fatal_span_verbose(
start_with_quote, pos,
String::from("character literal may only contain one codepoint")));
self.fatal_span_verbose(start_with_quote, pos,
String::from("character literal may only contain one codepoint")).raise();
}
let id = if valid {
......@@ -1364,9 +1363,9 @@ fn next_token_inner(&mut self) -> Result<token::Token, ()> {
while !self.ch_is('"') {
if self.is_eof() {
let last_bpos = self.pos;
panic!(self.fatal_span_(start_bpos,
last_bpos,
"unterminated double quote string"));
self.fatal_span_(start_bpos,
last_bpos,
"unterminated double quote string").raise();
}
let ch_start = self.pos;
......@@ -1399,15 +1398,15 @@ fn next_token_inner(&mut self) -> Result<token::Token, ()> {
if self.is_eof() {
let last_bpos = self.pos;
panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
self.fatal_span_(start_bpos, last_bpos, "unterminated raw string").raise();
} else if !self.ch_is('"') {
let last_bpos = self.pos;
let curr_char = self.ch.unwrap();
panic!(self.fatal_span_char(start_bpos,
last_bpos,
"found invalid character; only `#` is allowed \
in raw string delimitation",
curr_char));
self.fatal_span_char(start_bpos,
last_bpos,
"found invalid character; only `#` is allowed \
in raw string delimitation",
curr_char).raise();
}
self.bump();
let content_start_bpos = self.pos;
......@@ -1416,7 +1415,7 @@ fn next_token_inner(&mut self) -> Result<token::Token, ()> {
'outer: loop {
if self.is_eof() {
let last_bpos = self.pos;
panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
self.fatal_span_(start_bpos, last_bpos, "unterminated raw string").raise();
}
// if self.ch_is('"') {
// content_end_bpos = self.pos;
......@@ -1573,9 +1572,9 @@ fn scan_byte(&mut self) -> token::Lit {
// character before position `start` are an
// ascii single quote and ascii 'b'.
let pos = self.pos;
panic!(self.fatal_span_verbose(start - BytePos(2),
pos,
"unterminated byte constant".to_string()));
self.fatal_span_verbose(start - BytePos(2),
pos,
"unterminated byte constant".to_string()).raise();
}
let id = if valid {
......@@ -1599,7 +1598,7 @@ fn scan_byte_string(&mut self) -> token::Lit {
while !self.ch_is('"') {
if self.is_eof() {
let pos = self.pos;
panic!(self.fatal_span_(start, pos, "unterminated double quote byte string"));
self.fatal_span_(start, pos, "unterminated double quote byte string").raise();
}
let ch_start = self.pos;
......@@ -1631,15 +1630,15 @@ fn scan_raw_byte_string(&mut self) -> token::Lit {
if self.is_eof() {
let pos = self.pos;
panic!(self.fatal_span_(start_bpos, pos, "unterminated raw string"));
self.fatal_span_(start_bpos, pos, "unterminated raw string").raise();
} else if !self.ch_is('"') {
let pos = self.pos;
let ch = self.ch.unwrap();
panic!(self.fatal_span_char(start_bpos,
self.fatal_span_char(start_bpos,
pos,
"found invalid character; only `#` is allowed in raw \
string delimitation",
ch));
ch).raise();
}
self.bump();
let content_start_bpos = self.pos;
......@@ -1648,7 +1647,7 @@ fn scan_raw_byte_string(&mut self) -> token::Lit {
match self.ch {
None => {
let pos = self.pos;
panic!(self.fatal_span_(start_bpos, pos, "unterminated raw string"))
self.fatal_span_(start_bpos, pos, "unterminated raw string").raise()
}
Some('"') => {
content_end_bpos = self.pos;
......
......@@ -212,8 +212,8 @@ fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option<Span>)
Err(e) => {
let msg = format!("couldn't read {:?}: {}", path.display(), e);
match spanopt {
Some(sp) => panic!(sess.span_diagnostic.span_fatal(sp, &msg)),
None => panic!(sess.span_diagnostic.fatal(&msg))
Some(sp) => sess.span_diagnostic.span_fatal(sp, &msg).raise(),
None => sess.span_diagnostic.fatal(&msg).raise()
}
}
}
......
......@@ -123,7 +123,7 @@ fn fold_item(&mut self, i: P<ast::Item>) -> SmallVector<P<ast::Item>> {
match i.node {
ast::ItemKind::Fn(_, ast::Unsafety::Unsafe, _, _, _, _) => {
let diag = self.cx.span_diagnostic;
panic!(diag.span_fatal(i.span, "unsafe functions cannot be used for tests"));
diag.span_fatal(i.span, "unsafe functions cannot be used for tests").raise();
}
_ => {
debug!("this is a test function");
......
......@@ -92,7 +92,7 @@ fn expand(&self,
}
err.emit();
panic!(FatalError);
FatalError.raise();
}
};
......@@ -103,13 +103,13 @@ fn expand(&self,
// fail if there have been errors emitted
Ok(_) if ecx.parse_sess.span_diagnostic.err_count() > error_count_before => {
ecx.struct_span_fatal(span, msg).emit();
panic!(FatalError);
FatalError.raise();
}
Ok(new_items) => new_items.into_iter().map(Annotatable::Item).collect(),
Err(_) => {
// FIXME: handle this better
ecx.struct_span_fatal(span, msg).emit();
panic!(FatalError);
FatalError.raise();
}
}
})
......
......@@ -51,7 +51,7 @@ fn expand<'cx>(&self,
}
err.emit();
panic!(FatalError);
FatalError.raise();
}
}
}
......@@ -86,7 +86,7 @@ fn expand<'cx>(&self,
}
err.emit();
panic!(FatalError);
FatalError.raise();
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册