2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-08-31 11:13:04 -05:00
|
|
|
use codemap::{Pos, Span};
|
2012-12-23 16:41:37 -06:00
|
|
|
use codemap;
|
2014-07-01 11:39:41 -05:00
|
|
|
use diagnostics;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2014-02-28 13:37:04 -06:00
|
|
|
use std::cell::{RefCell, Cell};
|
2014-02-19 20:56:33 -06:00
|
|
|
use std::fmt;
|
|
|
|
use std::io;
|
2014-01-29 18:20:34 -06:00
|
|
|
use std::iter::range;
|
2014-05-22 18:57:53 -05:00
|
|
|
use std::string::String;
|
2014-06-25 20:18:13 -05:00
|
|
|
use term::WriterWrapper;
|
2014-01-30 18:15:10 -06:00
|
|
|
use term;
|
2012-01-13 18:05:58 -06:00
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// maximum number of lines we will print for each error; arbitrary.
|
2014-01-21 23:33:37 -06:00
|
|
|
static MAX_LINES: uint = 6u;
|
2013-10-21 14:10:57 -05:00
|
|
|
|
2014-04-04 20:46:43 -05:00
|
|
|
#[deriving(Clone)]
|
|
|
|
pub enum RenderSpan {
|
|
|
|
/// A FullSpan renders with both with an initial line for the
|
|
|
|
/// message, prefixed by file:linenum, followed by a summary of
|
|
|
|
/// the source code covered by the span.
|
|
|
|
FullSpan(Span),
|
|
|
|
|
|
|
|
/// A FileLine renders with just a line for the message prefixed
|
|
|
|
/// by file:linenum.
|
|
|
|
FileLine(Span),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RenderSpan {
|
|
|
|
fn span(self) -> Span {
|
|
|
|
match self {
|
|
|
|
FullSpan(s) | FileLine(s) => s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn is_full_span(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
&FullSpan(..) => true,
|
|
|
|
&FileLine(..) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 08:10:03 -05:00
|
|
|
#[deriving(Clone)]
|
|
|
|
pub enum ColorConfig {
|
|
|
|
Auto,
|
|
|
|
Always,
|
|
|
|
Never
|
|
|
|
}
|
|
|
|
|
2013-08-29 20:34:09 -05:00
|
|
|
pub trait Emitter {
|
2014-02-28 13:37:04 -06:00
|
|
|
fn emit(&mut self, cmsp: Option<(&codemap::CodeMap, Span)>,
|
2014-07-01 11:39:41 -05:00
|
|
|
msg: &str, code: Option<&str>, lvl: Level);
|
2014-02-28 13:37:04 -06:00
|
|
|
fn custom_emit(&mut self, cm: &codemap::CodeMap,
|
2014-04-04 20:46:43 -05:00
|
|
|
sp: RenderSpan, msg: &str, lvl: Level);
|
2013-08-29 20:34:09 -05:00
|
|
|
}
|
2012-01-13 21:00:09 -06:00
|
|
|
|
2014-10-09 14:17:22 -05:00
|
|
|
/// This structure is used to signify that a task has panicked with a fatal error
|
2014-01-17 14:12:46 -06:00
|
|
|
/// from the diagnostics. You can use this with the `Any` trait to figure out
|
|
|
|
/// how a rustc task died (if so desired).
|
|
|
|
pub struct FatalError;
|
|
|
|
|
2014-03-13 18:00:07 -05:00
|
|
|
/// Signifies that the compiler died with an explicit call to `.bug`
|
|
|
|
/// or `.span_bug` rather than a failed assertion, etc.
|
|
|
|
pub struct ExplicitBug;
|
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// A span-handler is like a handler but also
|
|
|
|
/// accepts span information for source-location
|
|
|
|
/// reporting.
|
2013-12-31 08:17:59 -06:00
|
|
|
pub struct SpanHandler {
|
2014-03-27 17:39:48 -05:00
|
|
|
pub handler: Handler,
|
|
|
|
pub cm: codemap::CodeMap,
|
2013-01-17 10:55:28 -06:00
|
|
|
}
|
2012-01-13 19:03:51 -06:00
|
|
|
|
2013-12-31 08:17:59 -06:00
|
|
|
impl SpanHandler {
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn span_fatal(&self, sp: Span, msg: &str) -> ! {
|
2014-03-16 13:56:24 -05:00
|
|
|
self.handler.emit(Some((&self.cm, sp)), msg, Fatal);
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!(FatalError);
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn span_err(&self, sp: Span, msg: &str) {
|
2014-03-16 13:56:24 -05:00
|
|
|
self.handler.emit(Some((&self.cm, sp)), msg, Error);
|
2012-01-24 23:42:54 -06:00
|
|
|
self.handler.bump_err_count();
|
|
|
|
}
|
2014-07-01 11:39:41 -05:00
|
|
|
pub fn span_err_with_code(&self, sp: Span, msg: &str, code: &str) {
|
|
|
|
self.handler.emit_with_code(Some((&self.cm, sp)), msg, code, Error);
|
|
|
|
self.handler.bump_err_count();
|
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn span_warn(&self, sp: Span, msg: &str) {
|
2014-03-16 13:56:24 -05:00
|
|
|
self.handler.emit(Some((&self.cm, sp)), msg, Warning);
|
2012-01-24 23:42:54 -06:00
|
|
|
}
|
2014-07-11 11:54:01 -05:00
|
|
|
pub fn span_warn_with_code(&self, sp: Span, msg: &str, code: &str) {
|
|
|
|
self.handler.emit_with_code(Some((&self.cm, sp)), msg, code, Warning);
|
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn span_note(&self, sp: Span, msg: &str) {
|
2014-03-16 13:56:24 -05:00
|
|
|
self.handler.emit(Some((&self.cm, sp)), msg, Note);
|
2012-01-24 23:42:54 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn span_end_note(&self, sp: Span, msg: &str) {
|
2014-04-04 20:46:43 -05:00
|
|
|
self.handler.custom_emit(&self.cm, FullSpan(sp), msg, Note);
|
|
|
|
}
|
2014-08-29 01:55:35 -05:00
|
|
|
pub fn span_help(&self, sp: Span, msg: &str) {
|
|
|
|
self.handler.emit(Some((&self.cm, sp)), msg, Help);
|
|
|
|
}
|
2014-04-04 20:46:43 -05:00
|
|
|
pub fn fileline_note(&self, sp: Span, msg: &str) {
|
|
|
|
self.handler.custom_emit(&self.cm, FileLine(sp), msg, Note);
|
2014-01-21 23:33:37 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
|
2014-03-16 13:56:24 -05:00
|
|
|
self.handler.emit(Some((&self.cm, sp)), msg, Bug);
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!(ExplicitBug);
|
2012-01-24 23:42:54 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn span_unimpl(&self, sp: Span, msg: &str) -> ! {
|
2014-05-19 19:23:26 -05:00
|
|
|
self.span_bug(sp, format!("unimplemented {}", msg).as_slice());
|
2012-01-24 23:42:54 -06:00
|
|
|
}
|
2014-03-16 13:56:24 -05:00
|
|
|
pub fn handler<'a>(&'a self) -> &'a Handler {
|
|
|
|
&self.handler
|
2012-01-24 23:42:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// A handler deals with errors; certain errors
|
|
|
|
/// (fatal, bug, unimpl) may cause immediate exit,
|
|
|
|
/// others log errors for later reporting.
|
2013-12-31 08:17:59 -06:00
|
|
|
pub struct Handler {
|
2013-12-27 17:14:10 -06:00
|
|
|
err_count: Cell<uint>,
|
2014-06-14 13:03:34 -05:00
|
|
|
emit: RefCell<Box<Emitter + Send>>,
|
2013-12-31 08:17:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler {
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn fatal(&self, msg: &str) -> ! {
|
2014-07-01 11:39:41 -05:00
|
|
|
self.emit.borrow_mut().emit(None, msg, None, Fatal);
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!(FatalError);
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn err(&self, msg: &str) {
|
2014-07-01 11:39:41 -05:00
|
|
|
self.emit.borrow_mut().emit(None, msg, None, Error);
|
2012-01-25 01:07:16 -06:00
|
|
|
self.bump_err_count();
|
2012-01-24 23:42:54 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn bump_err_count(&self) {
|
2013-12-27 17:14:10 -06:00
|
|
|
self.err_count.set(self.err_count.get() + 1u);
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn err_count(&self) -> uint {
|
2013-12-27 17:14:10 -06:00
|
|
|
self.err_count.get()
|
2013-05-06 08:00:37 -05:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn has_errors(&self) -> bool {
|
2013-12-27 17:14:10 -06:00
|
|
|
self.err_count.get()> 0u
|
2013-05-06 08:00:37 -05:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn abort_if_errors(&self) {
|
2012-06-06 16:28:59 -05:00
|
|
|
let s;
|
2013-12-27 17:14:10 -06:00
|
|
|
match self.err_count.get() {
|
2012-08-03 21:59:04 -05:00
|
|
|
0u => return,
|
2014-05-25 05:17:19 -05:00
|
|
|
1u => s = "aborting due to previous error".to_string(),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2013-09-27 23:01:58 -05:00
|
|
|
s = format!("aborting due to {} previous errors",
|
2014-05-16 12:45:16 -05:00
|
|
|
self.err_count.get());
|
2012-08-03 21:59:04 -05:00
|
|
|
}
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-05-16 12:45:16 -05:00
|
|
|
self.fatal(s.as_slice());
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn warn(&self, msg: &str) {
|
2014-07-01 11:39:41 -05:00
|
|
|
self.emit.borrow_mut().emit(None, msg, None, Warning);
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn note(&self, msg: &str) {
|
2014-07-01 11:39:41 -05:00
|
|
|
self.emit.borrow_mut().emit(None, msg, None, Note);
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-08-29 01:55:35 -05:00
|
|
|
pub fn help(&self, msg: &str) {
|
|
|
|
self.emit.borrow_mut().emit(None, msg, None, Help);
|
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn bug(&self, msg: &str) -> ! {
|
2014-07-01 11:39:41 -05:00
|
|
|
self.emit.borrow_mut().emit(None, msg, None, Bug);
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!(ExplicitBug);
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn unimpl(&self, msg: &str) -> ! {
|
2014-05-19 19:23:26 -05:00
|
|
|
self.bug(format!("unimplemented {}", msg).as_slice());
|
2013-02-04 16:02:01 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn emit(&self,
|
|
|
|
cmsp: Option<(&codemap::CodeMap, Span)>,
|
|
|
|
msg: &str,
|
|
|
|
lvl: Level) {
|
2014-07-01 11:39:41 -05:00
|
|
|
self.emit.borrow_mut().emit(cmsp, msg, None, lvl);
|
|
|
|
}
|
|
|
|
pub fn emit_with_code(&self,
|
|
|
|
cmsp: Option<(&codemap::CodeMap, Span)>,
|
|
|
|
msg: &str,
|
|
|
|
code: &str,
|
|
|
|
lvl: Level) {
|
|
|
|
self.emit.borrow_mut().emit(cmsp, msg, Some(code), lvl);
|
2012-01-24 23:42:54 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn custom_emit(&self, cm: &codemap::CodeMap,
|
2014-04-04 20:46:43 -05:00
|
|
|
sp: RenderSpan, msg: &str, lvl: Level) {
|
2014-03-20 17:05:37 -05:00
|
|
|
self.emit.borrow_mut().custom_emit(cm, sp, msg, lvl);
|
2014-01-21 23:33:37 -06:00
|
|
|
}
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
|
|
|
|
2014-03-16 13:56:24 -05:00
|
|
|
pub fn mk_span_handler(handler: Handler, cm: codemap::CodeMap) -> SpanHandler {
|
|
|
|
SpanHandler {
|
2013-08-29 20:34:09 -05:00
|
|
|
handler: handler,
|
|
|
|
cm: cm,
|
2013-12-31 08:17:59 -06:00
|
|
|
}
|
2012-01-24 23:42:54 -06:00
|
|
|
}
|
|
|
|
|
2014-07-01 11:39:41 -05:00
|
|
|
pub fn default_handler(color_config: ColorConfig,
|
|
|
|
registry: Option<diagnostics::registry::Registry>) -> Handler {
|
|
|
|
mk_handler(box EmitterWriter::stderr(color_config, registry))
|
2014-02-28 13:37:04 -06:00
|
|
|
}
|
|
|
|
|
2014-06-14 13:03:34 -05:00
|
|
|
pub fn mk_handler(e: Box<Emitter + Send>) -> Handler {
|
2014-03-16 13:56:24 -05:00
|
|
|
Handler {
|
2013-12-27 17:14:10 -06:00
|
|
|
err_count: Cell::new(0),
|
2014-02-28 13:37:04 -06:00
|
|
|
emit: RefCell::new(e),
|
2013-12-31 08:17:59 -06:00
|
|
|
}
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2012-01-13 18:05:58 -06:00
|
|
|
|
2014-09-04 13:29:26 -05:00
|
|
|
#[deriving(PartialEq, Clone)]
|
2014-01-09 07:05:33 -06:00
|
|
|
pub enum Level {
|
2014-03-13 18:00:07 -05:00
|
|
|
Bug,
|
2014-01-09 07:05:33 -06:00
|
|
|
Fatal,
|
|
|
|
Error,
|
|
|
|
Warning,
|
|
|
|
Note,
|
2014-08-29 01:55:35 -05:00
|
|
|
Help,
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
|
|
|
|
2014-02-19 20:56:33 -06:00
|
|
|
impl fmt::Show for Level {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use std::fmt::Show;
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
match *self {
|
2014-03-13 18:00:07 -05:00
|
|
|
Bug => "error: internal compiler error".fmt(f),
|
2014-02-19 20:56:33 -06:00
|
|
|
Fatal | Error => "error".fmt(f),
|
|
|
|
Warning => "warning".fmt(f),
|
|
|
|
Note => "note".fmt(f),
|
2014-08-29 01:55:35 -05:00
|
|
|
Help => "help".fmt(f),
|
2014-01-09 07:05:33 -06:00
|
|
|
}
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
impl Level {
|
|
|
|
fn color(self) -> term::color::Color {
|
|
|
|
match self {
|
2014-03-13 18:00:07 -05:00
|
|
|
Bug | Fatal | Error => term::color::BRIGHT_RED,
|
2014-01-09 07:05:33 -06:00
|
|
|
Warning => term::color::BRIGHT_YELLOW,
|
2014-08-29 01:55:35 -05:00
|
|
|
Note => term::color::BRIGHT_GREEN,
|
|
|
|
Help => term::color::BRIGHT_CYAN,
|
2014-01-09 07:05:33 -06:00
|
|
|
}
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 13:37:04 -06:00
|
|
|
fn print_maybe_styled(w: &mut EmitterWriter,
|
|
|
|
msg: &str,
|
|
|
|
color: term::attr::Attr) -> io::IoResult<()> {
|
|
|
|
match w.dst {
|
|
|
|
Terminal(ref mut t) => {
|
|
|
|
try!(t.attr(color));
|
2014-05-19 16:37:52 -05:00
|
|
|
// If `msg` ends in a newline, we need to reset the color before
|
|
|
|
// the newline. We're making the assumption that we end up writing
|
|
|
|
// to a `LineBufferedWriter`, which means that emitting the reset
|
|
|
|
// after the newline ends up buffering the reset until we print
|
|
|
|
// another line or exit. Buffering the reset is a problem if we're
|
|
|
|
// sharing the terminal with any other programs (e.g. other rustc
|
|
|
|
// instances via `make -jN`).
|
|
|
|
//
|
|
|
|
// Note that if `msg` contains any internal newlines, this will
|
|
|
|
// result in the `LineBufferedWriter` flushing twice instead of
|
|
|
|
// once, which still leaves the opportunity for interleaved output
|
|
|
|
// to be miscolored. We assume this is rare enough that we don't
|
|
|
|
// have to worry about it.
|
|
|
|
if msg.ends_with("\n") {
|
|
|
|
try!(t.write_str(msg.slice_to(msg.len()-1)));
|
|
|
|
try!(t.reset());
|
|
|
|
try!(t.write_str("\n"));
|
|
|
|
} else {
|
|
|
|
try!(t.write_str(msg));
|
|
|
|
try!(t.reset());
|
|
|
|
}
|
2014-02-28 13:37:04 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Raw(ref mut w) => {
|
|
|
|
w.write_str(msg)
|
|
|
|
}
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 11:39:41 -05:00
|
|
|
fn print_diagnostic(dst: &mut EmitterWriter, topic: &str, lvl: Level,
|
|
|
|
msg: &str, code: Option<&str>) -> io::IoResult<()> {
|
2013-06-19 06:42:47 -05:00
|
|
|
if !topic.is_empty() {
|
2014-02-28 13:37:04 -06:00
|
|
|
try!(write!(&mut dst.dst, "{} ", topic));
|
2013-06-19 06:42:47 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 12:45:16 -05:00
|
|
|
try!(print_maybe_styled(dst,
|
2014-06-21 05:39:03 -05:00
|
|
|
format!("{}: ", lvl.to_string()).as_slice(),
|
2014-02-28 13:37:04 -06:00
|
|
|
term::attr::ForegroundColor(lvl.color())));
|
2014-05-16 12:45:16 -05:00
|
|
|
try!(print_maybe_styled(dst,
|
2014-07-01 11:39:41 -05:00
|
|
|
format!("{}", msg).as_slice(),
|
2014-05-16 12:45:16 -05:00
|
|
|
term::attr::Bold));
|
2014-07-01 11:39:41 -05:00
|
|
|
|
|
|
|
match code {
|
|
|
|
Some(code) => {
|
|
|
|
let style = term::attr::ForegroundColor(term::color::BRIGHT_MAGENTA);
|
|
|
|
try!(print_maybe_styled(dst, format!(" [{}]", code.clone()).as_slice(), style));
|
|
|
|
}
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
try!(dst.dst.write_char('\n'));
|
2014-01-29 19:39:21 -06:00
|
|
|
Ok(())
|
2013-06-19 06:42:47 -05:00
|
|
|
}
|
|
|
|
|
2014-02-28 13:37:04 -06:00
|
|
|
pub struct EmitterWriter {
|
2014-03-27 17:39:48 -05:00
|
|
|
dst: Destination,
|
2014-07-01 11:39:41 -05:00
|
|
|
registry: Option<diagnostics::registry::Registry>
|
2014-02-28 13:37:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Destination {
|
2014-06-25 20:18:13 -05:00
|
|
|
Terminal(Box<term::Terminal<WriterWrapper> + Send>),
|
2014-06-14 13:03:34 -05:00
|
|
|
Raw(Box<Writer + Send>),
|
2014-02-28 13:37:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EmitterWriter {
|
2014-07-01 11:39:41 -05:00
|
|
|
pub fn stderr(color_config: ColorConfig,
|
|
|
|
registry: Option<diagnostics::registry::Registry>) -> EmitterWriter {
|
2014-02-28 13:37:04 -06:00
|
|
|
let stderr = io::stderr();
|
2014-05-08 08:10:03 -05:00
|
|
|
|
|
|
|
let use_color = match color_config {
|
|
|
|
Always => true,
|
|
|
|
Never => false,
|
|
|
|
Auto => stderr.get_ref().isatty()
|
|
|
|
};
|
|
|
|
|
|
|
|
if use_color {
|
2014-04-08 10:54:54 -05:00
|
|
|
let dst = match term::stderr() {
|
|
|
|
Some(t) => Terminal(t),
|
|
|
|
None => Raw(box stderr),
|
2014-02-28 13:37:04 -06:00
|
|
|
};
|
2014-07-01 11:39:41 -05:00
|
|
|
EmitterWriter { dst: dst, registry: registry }
|
2014-02-28 13:37:04 -06:00
|
|
|
} else {
|
2014-07-01 11:39:41 -05:00
|
|
|
EmitterWriter { dst: Raw(box stderr), registry: registry }
|
2014-02-28 13:37:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 11:39:41 -05:00
|
|
|
pub fn new(dst: Box<Writer + Send>,
|
|
|
|
registry: Option<diagnostics::registry::Registry>) -> EmitterWriter {
|
|
|
|
EmitterWriter { dst: Raw(dst), registry: registry }
|
2014-02-28 13:37:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for Destination {
|
|
|
|
fn write(&mut self, bytes: &[u8]) -> io::IoResult<()> {
|
|
|
|
match *self {
|
|
|
|
Terminal(ref mut t) => t.write(bytes),
|
|
|
|
Raw(ref mut w) => w.write(bytes),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-29 20:34:09 -05:00
|
|
|
|
2014-02-28 13:37:04 -06:00
|
|
|
impl Emitter for EmitterWriter {
|
|
|
|
fn emit(&mut self,
|
2014-01-01 00:23:41 -06:00
|
|
|
cmsp: Option<(&codemap::CodeMap, Span)>,
|
2014-07-01 11:39:41 -05:00
|
|
|
msg: &str, code: Option<&str>, lvl: Level) {
|
2014-01-29 19:39:21 -06:00
|
|
|
let error = match cmsp {
|
2014-07-01 11:39:41 -05:00
|
|
|
Some((cm, sp)) => emit(self, cm, FullSpan(sp), msg, code, lvl, false),
|
|
|
|
None => print_diagnostic(self, "", lvl, msg, code),
|
2014-01-29 19:39:21 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
match error {
|
|
|
|
Ok(()) => {}
|
2014-10-09 14:17:22 -05:00
|
|
|
Err(e) => panic!("failed to print diagnostics: {}", e),
|
2013-08-29 20:34:09 -05:00
|
|
|
}
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
2014-01-21 23:33:37 -06:00
|
|
|
|
2014-02-28 13:37:04 -06:00
|
|
|
fn custom_emit(&mut self, cm: &codemap::CodeMap,
|
2014-04-04 20:46:43 -05:00
|
|
|
sp: RenderSpan, msg: &str, lvl: Level) {
|
2014-07-01 11:39:41 -05:00
|
|
|
match emit(self, cm, sp, msg, None, lvl, true) {
|
2014-01-29 19:39:21 -06:00
|
|
|
Ok(()) => {}
|
2014-10-09 14:17:22 -05:00
|
|
|
Err(e) => panic!("failed to print diagnostics: {}", e),
|
2014-01-29 19:39:21 -06:00
|
|
|
}
|
2014-01-21 23:33:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-04 20:46:43 -05:00
|
|
|
fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, rsp: RenderSpan,
|
2014-07-01 11:39:41 -05:00
|
|
|
msg: &str, code: Option<&str>, lvl: Level, custom: bool) -> io::IoResult<()> {
|
2014-04-04 20:46:43 -05:00
|
|
|
let sp = rsp.span();
|
2014-06-21 05:39:03 -05:00
|
|
|
let ss = cm.span_to_string(sp);
|
2014-01-21 23:33:37 -06:00
|
|
|
let lines = cm.span_to_lines(sp);
|
|
|
|
if custom {
|
|
|
|
// we want to tell compiletest/runtest to look at the last line of the
|
|
|
|
// span (since `custom_highlight_lines` displays an arrow to the end of
|
|
|
|
// the span)
|
2014-09-17 11:01:33 -05:00
|
|
|
let span_end = Span { lo: sp.hi, hi: sp.hi, expn_id: sp.expn_id};
|
2014-06-21 05:39:03 -05:00
|
|
|
let ses = cm.span_to_string(span_end);
|
2014-07-01 11:39:41 -05:00
|
|
|
try!(print_diagnostic(dst, ses.as_slice(), lvl, msg, code));
|
2014-04-04 20:46:43 -05:00
|
|
|
if rsp.is_full_span() {
|
|
|
|
try!(custom_highlight_lines(dst, cm, sp, lvl, lines));
|
|
|
|
}
|
2014-01-21 23:33:37 -06:00
|
|
|
} else {
|
2014-07-01 11:39:41 -05:00
|
|
|
try!(print_diagnostic(dst, ss.as_slice(), lvl, msg, code));
|
2014-04-04 20:46:43 -05:00
|
|
|
if rsp.is_full_span() {
|
|
|
|
try!(highlight_lines(dst, cm, sp, lvl, lines));
|
|
|
|
}
|
2014-01-21 23:33:37 -06:00
|
|
|
}
|
2014-08-29 01:55:35 -05:00
|
|
|
try!(print_macro_backtrace(dst, cm, sp));
|
|
|
|
match code {
|
|
|
|
Some(code) =>
|
|
|
|
match dst.registry.as_ref().and_then(|registry| registry.find_description(code)) {
|
|
|
|
Some(_) => {
|
|
|
|
try!(print_diagnostic(dst, ss.as_slice(), Help,
|
|
|
|
format!("pass `--explain {}` to see a detailed \
|
|
|
|
explanation", code).as_slice(), None));
|
|
|
|
}
|
|
|
|
None => ()
|
|
|
|
},
|
|
|
|
None => (),
|
|
|
|
}
|
|
|
|
Ok(())
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
|
|
|
|
2014-02-28 13:37:04 -06:00
|
|
|
fn highlight_lines(err: &mut EmitterWriter,
|
|
|
|
cm: &codemap::CodeMap,
|
2013-08-29 20:34:09 -05:00
|
|
|
sp: Span,
|
2014-01-09 07:05:33 -06:00
|
|
|
lvl: Level,
|
2014-03-16 13:56:24 -05:00
|
|
|
lines: codemap::FileLines) -> io::IoResult<()> {
|
2014-03-20 17:05:37 -05:00
|
|
|
let fm = &*lines.file;
|
2012-01-13 18:05:58 -06:00
|
|
|
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut elided = false;
|
2014-01-01 00:23:41 -06:00
|
|
|
let mut display_lines = lines.lines.as_slice();
|
2014-01-21 23:33:37 -06:00
|
|
|
if display_lines.len() > MAX_LINES {
|
2014-09-24 06:41:09 -05:00
|
|
|
display_lines = display_lines[0u..MAX_LINES];
|
2012-01-13 18:05:58 -06:00
|
|
|
elided = true;
|
|
|
|
}
|
|
|
|
// Print the offending lines
|
2013-08-03 11:45:23 -05:00
|
|
|
for line in display_lines.iter() {
|
2014-02-28 13:37:04 -06:00
|
|
|
try!(write!(&mut err.dst, "{}:{} {}\n", fm.name, *line + 1,
|
|
|
|
fm.get_line(*line as int)));
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
|
|
|
if elided {
|
2013-05-14 04:52:12 -05:00
|
|
|
let last_line = display_lines[display_lines.len() - 1u];
|
2013-09-27 23:01:58 -05:00
|
|
|
let s = format!("{}:{} ", fm.name, last_line + 1u);
|
2014-02-28 13:37:04 -06:00
|
|
|
try!(write!(&mut err.dst, "{0:1$}...\n", "", s.len()));
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
|
|
|
|
2013-02-08 18:01:39 -06:00
|
|
|
// FIXME (#3260)
|
2012-01-13 18:05:58 -06:00
|
|
|
// If there's one line at fault we can easily point to the problem
|
2013-05-14 04:52:12 -05:00
|
|
|
if lines.lines.len() == 1u {
|
2012-11-12 20:24:56 -06:00
|
|
|
let lo = cm.lookup_char_pos(sp.lo);
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut digits = 0u;
|
2014-10-15 01:05:01 -05:00
|
|
|
let mut num = (lines.lines[0] + 1u) / 10u;
|
2012-01-13 18:05:58 -06:00
|
|
|
|
|
|
|
// how many digits must be indent past?
|
|
|
|
while num > 0u { num /= 10u; digits += 1u; }
|
|
|
|
|
|
|
|
// indent past |name:## | and the 0-offset column location
|
2013-06-09 09:44:58 -05:00
|
|
|
let left = fm.name.len() + digits + lo.col.to_uint() + 3u;
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut s = String::new();
|
2013-02-08 17:45:00 -06:00
|
|
|
// Skip is the number of characters we need to skip because they are
|
|
|
|
// part of the 'filename:line ' part of the previous line.
|
2013-06-09 09:44:58 -05:00
|
|
|
let skip = fm.name.len() + digits + 3u;
|
2014-04-02 18:54:22 -05:00
|
|
|
for _ in range(0, skip) {
|
2014-10-15 01:05:01 -05:00
|
|
|
s.push(' ');
|
2014-04-02 18:54:22 -05:00
|
|
|
}
|
2014-10-15 01:05:01 -05:00
|
|
|
let orig = fm.get_line(lines.lines[0] as int);
|
2013-08-03 11:45:23 -05:00
|
|
|
for pos in range(0u, left-skip) {
|
2014-06-19 20:22:33 -05:00
|
|
|
let cur_char = orig.as_bytes()[pos] as char;
|
2013-06-11 21:13:42 -05:00
|
|
|
// Whenever a tab occurs on the previous line, we insert one on
|
|
|
|
// the error-point-squiggly-line as well (instead of a space).
|
|
|
|
// That way the squiggly line will usually appear in the correct
|
|
|
|
// position.
|
2014-02-15 15:15:03 -06:00
|
|
|
match cur_char {
|
2014-10-15 01:05:01 -05:00
|
|
|
'\t' => s.push('\t'),
|
|
|
|
_ => s.push(' '),
|
2013-06-11 21:13:42 -05:00
|
|
|
};
|
|
|
|
}
|
2014-02-28 13:37:04 -06:00
|
|
|
try!(write!(&mut err.dst, "{}", s));
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut s = String::from_str("^");
|
2012-11-12 20:24:56 -06:00
|
|
|
let hi = cm.lookup_char_pos(sp.hi);
|
2012-01-13 18:05:58 -06:00
|
|
|
if hi.col != lo.col {
|
|
|
|
// the ^ already takes up one space
|
2013-06-25 17:36:48 -05:00
|
|
|
let num_squigglies = hi.col.to_uint()-lo.col.to_uint()-1u;
|
2014-04-02 18:54:22 -05:00
|
|
|
for _ in range(0, num_squigglies) {
|
2014-10-15 01:05:01 -05:00
|
|
|
s.push('~');
|
2014-04-02 18:54:22 -05:00
|
|
|
}
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
2014-05-19 19:23:26 -05:00
|
|
|
try!(print_maybe_styled(err,
|
|
|
|
format!("{}\n", s).as_slice(),
|
2014-02-28 13:37:04 -06:00
|
|
|
term::attr::ForegroundColor(lvl.color())));
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
2014-01-29 19:39:21 -06:00
|
|
|
Ok(())
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
2012-02-04 19:37:24 -06:00
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// Here are the differences between this and the normal `highlight_lines`:
|
|
|
|
/// `custom_highlight_lines` will always put arrow on the last byte of the
|
|
|
|
/// span (instead of the first byte). Also, when the span is too long (more
|
|
|
|
/// than 6 lines), `custom_highlight_lines` will print the first line, then
|
|
|
|
/// dot dot dot, then last line, whereas `highlight_lines` prints the first
|
|
|
|
/// six lines.
|
2014-02-28 13:37:04 -06:00
|
|
|
fn custom_highlight_lines(w: &mut EmitterWriter,
|
|
|
|
cm: &codemap::CodeMap,
|
2014-01-21 23:33:37 -06:00
|
|
|
sp: Span,
|
|
|
|
lvl: Level,
|
2014-04-02 18:54:22 -05:00
|
|
|
lines: codemap::FileLines)
|
|
|
|
-> io::IoResult<()> {
|
2014-03-20 17:05:37 -05:00
|
|
|
let fm = &*lines.file;
|
2014-01-21 23:33:37 -06:00
|
|
|
|
|
|
|
let lines = lines.lines.as_slice();
|
|
|
|
if lines.len() > MAX_LINES {
|
2014-02-28 13:37:04 -06:00
|
|
|
try!(write!(&mut w.dst, "{}:{} {}\n", fm.name,
|
|
|
|
lines[0] + 1, fm.get_line(lines[0] as int)));
|
|
|
|
try!(write!(&mut w.dst, "...\n"));
|
2014-01-21 23:33:37 -06:00
|
|
|
let last_line = lines[lines.len()-1];
|
2014-02-28 13:37:04 -06:00
|
|
|
try!(write!(&mut w.dst, "{}:{} {}\n", fm.name,
|
|
|
|
last_line + 1, fm.get_line(last_line as int)));
|
2014-01-21 23:33:37 -06:00
|
|
|
} else {
|
|
|
|
for line in lines.iter() {
|
2014-02-28 13:37:04 -06:00
|
|
|
try!(write!(&mut w.dst, "{}:{} {}\n", fm.name,
|
|
|
|
*line + 1, fm.get_line(*line as int)));
|
2014-01-21 23:33:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let last_line_start = format!("{}:{} ", fm.name, lines[lines.len()-1]+1);
|
|
|
|
let hi = cm.lookup_char_pos(sp.hi);
|
|
|
|
// Span seems to use half-opened interval, so subtract 1
|
|
|
|
let skip = last_line_start.len() + hi.col.to_uint() - 1;
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut s = String::new();
|
2014-04-02 18:54:22 -05:00
|
|
|
for _ in range(0, skip) {
|
2014-10-15 01:05:01 -05:00
|
|
|
s.push(' ');
|
2014-04-02 18:54:22 -05:00
|
|
|
}
|
2014-10-15 01:05:01 -05:00
|
|
|
s.push('^');
|
|
|
|
s.push('\n');
|
2014-04-02 18:54:22 -05:00
|
|
|
print_maybe_styled(w,
|
2014-05-19 19:23:26 -05:00
|
|
|
s.as_slice(),
|
2014-04-02 18:54:22 -05:00
|
|
|
term::attr::ForegroundColor(lvl.color()))
|
2014-01-21 23:33:37 -06:00
|
|
|
}
|
|
|
|
|
2014-02-28 13:37:04 -06:00
|
|
|
fn print_macro_backtrace(w: &mut EmitterWriter,
|
|
|
|
cm: &codemap::CodeMap,
|
2014-04-02 18:54:22 -05:00
|
|
|
sp: Span)
|
|
|
|
-> io::IoResult<()> {
|
2014-09-17 11:01:33 -05:00
|
|
|
let cs = try!(cm.with_expn_info(sp.expn_id, |expn_info| match expn_info {
|
|
|
|
Some(ei) => {
|
|
|
|
let ss = ei.callee.span.map_or(String::new(), |span| cm.span_to_string(span));
|
|
|
|
let (pre, post) = match ei.callee.format {
|
|
|
|
codemap::MacroAttribute => ("#[", "]"),
|
|
|
|
codemap::MacroBang => ("", "!")
|
|
|
|
};
|
|
|
|
try!(print_diagnostic(w, ss.as_slice(), Note,
|
|
|
|
format!("in expansion of {}{}{}", pre,
|
|
|
|
ei.callee.name,
|
|
|
|
post).as_slice(), None));
|
|
|
|
let ss = cm.span_to_string(ei.call_site);
|
|
|
|
try!(print_diagnostic(w, ss.as_slice(), Note, "expansion site", None));
|
|
|
|
Ok(Some(ei.call_site))
|
|
|
|
}
|
|
|
|
None => Ok(None)
|
|
|
|
}));
|
|
|
|
cs.map_or(Ok(()), |call_site| print_macro_backtrace(w, cm, call_site))
|
2012-02-04 19:37:24 -06:00
|
|
|
}
|
2012-05-22 16:55:39 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn expect<T>(diag: &SpanHandler, opt: Option<T>, msg: || -> String) -> T {
|
2012-08-06 14:34:08 -05:00
|
|
|
match opt {
|
2014-09-13 11:06:01 -05:00
|
|
|
Some(t) => t,
|
|
|
|
None => diag.handler().bug(msg().as_slice()),
|
2012-05-22 16:55:39 -05:00
|
|
|
}
|
|
|
|
}
|