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.
|
|
|
|
|
2014-02-26 11:58:41 -06:00
|
|
|
extern crate libc;
|
|
|
|
|
2013-08-31 11:13:04 -05:00
|
|
|
use codemap::{Pos, Span};
|
2012-12-23 16:41:37 -06:00
|
|
|
use codemap;
|
|
|
|
|
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-04-02 18:54:22 -05:00
|
|
|
use std::strbuf::StrBuf;
|
2014-01-30 18:15:10 -06:00
|
|
|
use term;
|
2012-01-13 18:05:58 -06:00
|
|
|
|
2014-01-21 23:33:37 -06:00
|
|
|
// maximum number of lines we will print for each error; arbitrary.
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-06 16:38:33 -06:00
|
|
|
msg: &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-01-17 14:12:46 -06:00
|
|
|
/// This structure is used to signify that a task has failed with a fatal error
|
|
|
|
/// 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;
|
|
|
|
|
2013-02-11 15:36:24 -06: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-01-17 14:12:46 -06:00
|
|
|
fail!(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-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-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);
|
|
|
|
}
|
|
|
|
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-03-13 18:00:07 -05:00
|
|
|
fail!(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-04-15 20:17:48 -05:00
|
|
|
self.span_bug(sp, "unimplemented ".to_owned() + msg);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-31 08:17:59 -06:00
|
|
|
// a handler deals with errors; certain errors
|
|
|
|
// (fatal, bug, unimpl) may cause immediate exit,
|
|
|
|
// others log errors for later reporting.
|
|
|
|
pub struct Handler {
|
2013-12-27 17:14:10 -06:00
|
|
|
err_count: Cell<uint>,
|
2014-05-05 20:56:44 -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-03-20 17:05:37 -05:00
|
|
|
self.emit.borrow_mut().emit(None, msg, Fatal);
|
2014-01-17 14:12:46 -06:00
|
|
|
fail!(FatalError);
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn err(&self, msg: &str) {
|
2014-03-20 17:05:37 -05:00
|
|
|
self.emit.borrow_mut().emit(None, msg, 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-04-15 20:17:48 -05:00
|
|
|
1u => s = "aborting due to previous error".to_owned(),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2013-09-27 23:01:58 -05:00
|
|
|
s = format!("aborting due to {} previous errors",
|
2013-12-27 17:14:10 -06:00
|
|
|
self.err_count.get());
|
2012-08-03 21:59:04 -05:00
|
|
|
}
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2012-06-06 16:28:59 -05:00
|
|
|
self.fatal(s);
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn warn(&self, msg: &str) {
|
2014-03-20 17:05:37 -05:00
|
|
|
self.emit.borrow_mut().emit(None, msg, Warning);
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn note(&self, msg: &str) {
|
2014-03-20 17:05:37 -05:00
|
|
|
self.emit.borrow_mut().emit(None, msg, Note);
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn bug(&self, msg: &str) -> ! {
|
2014-03-20 17:05:37 -05:00
|
|
|
self.emit.borrow_mut().emit(None, msg, Bug);
|
2014-03-13 18:00:07 -05:00
|
|
|
fail!(ExplicitBug);
|
2012-01-13 19:03:51 -06:00
|
|
|
}
|
2014-02-06 16:38:33 -06:00
|
|
|
pub fn unimpl(&self, msg: &str) -> ! {
|
2014-04-15 20:17:48 -05:00
|
|
|
self.bug("unimplemented ".to_owned() + msg);
|
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-03-20 17:05:37 -05:00
|
|
|
self.emit.borrow_mut().emit(cmsp, msg, 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-03-16 13:56:24 -05:00
|
|
|
pub fn default_handler() -> Handler {
|
2014-04-25 03:08:02 -05:00
|
|
|
mk_handler(box EmitterWriter::stderr())
|
2014-02-28 13:37:04 -06:00
|
|
|
}
|
|
|
|
|
2014-05-05 20:56:44 -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
|
|
|
|
2013-03-20 10:52:45 -05:00
|
|
|
#[deriving(Eq)]
|
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,
|
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-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,
|
|
|
|
Note => term::color::BRIGHT_GREEN
|
|
|
|
}
|
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));
|
|
|
|
try!(t.write_str(msg));
|
|
|
|
try!(t.reset());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Raw(ref mut w) => {
|
|
|
|
w.write_str(msg)
|
|
|
|
}
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 13:37:04 -06:00
|
|
|
fn print_diagnostic(dst: &mut EmitterWriter,
|
|
|
|
topic: &str, lvl: Level, msg: &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-02-28 13:37:04 -06:00
|
|
|
try!(print_maybe_styled(dst, format!("{}: ", lvl.to_str()),
|
|
|
|
term::attr::ForegroundColor(lvl.color())));
|
|
|
|
try!(print_maybe_styled(dst, format!("{}\n", msg), term::attr::Bold));
|
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-02-28 13:37:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Destination {
|
|
|
|
Terminal(term::Terminal<io::stdio::StdWriter>),
|
2014-05-05 20:56:44 -05:00
|
|
|
Raw(Box<Writer:Send>),
|
2014-02-28 13:37:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EmitterWriter {
|
|
|
|
pub fn stderr() -> EmitterWriter {
|
|
|
|
let stderr = io::stderr();
|
2014-02-28 14:55:30 -06:00
|
|
|
if stderr.get_ref().isatty() {
|
|
|
|
let dst = match term::Terminal::new(stderr.unwrap()) {
|
2014-02-28 13:37:04 -06:00
|
|
|
Ok(t) => Terminal(t),
|
2014-04-25 03:08:02 -05:00
|
|
|
Err(..) => Raw(box io::stderr()),
|
2014-02-28 13:37:04 -06:00
|
|
|
};
|
|
|
|
EmitterWriter { dst: dst }
|
|
|
|
} else {
|
2014-04-25 03:08:02 -05:00
|
|
|
EmitterWriter { dst: Raw(box stderr) }
|
2014-02-28 13:37:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
pub fn new(dst: Box<Writer:Send>) -> EmitterWriter {
|
2014-02-28 13:37:04 -06:00
|
|
|
EmitterWriter { dst: Raw(dst) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)>,
|
2013-08-29 20:34:09 -05:00
|
|
|
msg: &str,
|
2014-01-09 07:05:33 -06:00
|
|
|
lvl: Level) {
|
2014-01-29 19:39:21 -06:00
|
|
|
let error = match cmsp {
|
2014-04-04 20:46:43 -05:00
|
|
|
Some((cm, sp)) => emit(self, cm, FullSpan(sp), msg, lvl, false),
|
2014-02-28 13:37:04 -06:00
|
|
|
None => print_diagnostic(self, "", lvl, msg),
|
2014-01-29 19:39:21 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
match error {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(e) => fail!("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-02-28 13:37:04 -06:00
|
|
|
match emit(self, cm, sp, msg, lvl, true) {
|
2014-01-29 19:39:21 -06:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(e) => fail!("failed to print diagnostics: {}", e),
|
|
|
|
}
|
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-01-29 19:39:21 -06:00
|
|
|
msg: &str, lvl: Level, custom: bool) -> io::IoResult<()> {
|
2014-04-04 20:46:43 -05:00
|
|
|
let sp = rsp.span();
|
2014-01-21 23:33:37 -06:00
|
|
|
let ss = cm.span_to_str(sp);
|
|
|
|
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)
|
|
|
|
let span_end = Span { lo: sp.hi, hi: sp.hi, expn_info: sp.expn_info};
|
|
|
|
let ses = cm.span_to_str(span_end);
|
2014-02-28 13:37:04 -06:00
|
|
|
try!(print_diagnostic(dst, ses, lvl, msg));
|
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-02-28 13:37:04 -06:00
|
|
|
try!(print_diagnostic(dst, ss, lvl, msg));
|
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-02-28 13:37:04 -06:00
|
|
|
print_macro_backtrace(dst, cm, sp)
|
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 {
|
|
|
|
display_lines = display_lines.slice(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-02-28 14:54:01 -06:00
|
|
|
let mut num = (*lines.lines.get(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-04-02 18:54:22 -05:00
|
|
|
let mut s = StrBuf::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) {
|
|
|
|
s.push_char(' ');
|
|
|
|
}
|
2014-02-28 14:54:01 -06:00
|
|
|
let orig = fm.get_line(*lines.lines.get(0) as int);
|
2013-08-03 11:45:23 -05:00
|
|
|
for pos in range(0u, left-skip) {
|
2014-02-15 15:15:03 -06:00
|
|
|
let cur_char = orig[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 {
|
2013-06-11 21:13:42 -05:00
|
|
|
'\t' => s.push_char('\t'),
|
|
|
|
_ => s.push_char(' '),
|
|
|
|
};
|
|
|
|
}
|
2014-02-28 13:37:04 -06:00
|
|
|
try!(write!(&mut err.dst, "{}", s));
|
2014-04-02 18:54:22 -05:00
|
|
|
let mut s = StrBuf::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) {
|
|
|
|
s.push_char('~');
|
|
|
|
}
|
2012-01-13 18:05:58 -06:00
|
|
|
}
|
2014-04-02 18:54:22 -05:00
|
|
|
try!(print_maybe_styled(err, s.into_owned() + "\n",
|
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-01-21 23:33:37 -06: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-04-02 18:54:22 -05:00
|
|
|
let mut s = StrBuf::new();
|
|
|
|
for _ in range(0, skip) {
|
|
|
|
s.push_char(' ');
|
|
|
|
}
|
2014-01-21 23:33:37 -06:00
|
|
|
s.push_char('^');
|
2014-04-02 18:54:22 -05:00
|
|
|
s.push_char('\n');
|
|
|
|
print_maybe_styled(w,
|
|
|
|
s.into_owned(),
|
|
|
|
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<()> {
|
2013-08-03 11:45:23 -05:00
|
|
|
for ei in sp.expn_info.iter() {
|
2014-04-15 20:17:48 -05:00
|
|
|
let ss = ei.callee.span.as_ref().map_or("".to_owned(), |span| cm.span_to_str(*span));
|
2013-12-06 20:41:11 -06:00
|
|
|
let (pre, post) = match ei.callee.format {
|
|
|
|
codemap::MacroAttribute => ("#[", "]"),
|
|
|
|
codemap::MacroBang => ("", "!")
|
|
|
|
};
|
2014-02-28 13:37:04 -06:00
|
|
|
try!(print_diagnostic(w, ss, Note,
|
|
|
|
format!("in expansion of {}{}{}", pre,
|
|
|
|
ei.callee.name, post)));
|
2012-11-12 20:24:56 -06:00
|
|
|
let ss = cm.span_to_str(ei.call_site);
|
2014-02-28 13:37:04 -06:00
|
|
|
try!(print_diagnostic(w, ss, Note, "expansion site"));
|
|
|
|
try!(print_macro_backtrace(w, cm, ei.call_site));
|
2012-02-04 19:37:24 -06:00
|
|
|
}
|
2014-01-29 19:39:21 -06:00
|
|
|
Ok(())
|
2012-02-04 19:37:24 -06:00
|
|
|
}
|
2012-05-22 16:55:39 -05:00
|
|
|
|
2014-03-16 13:56:24 -05:00
|
|
|
pub fn expect<T:Clone>(diag: &SpanHandler, opt: Option<T>, msg: || -> ~str) -> T {
|
2012-08-06 14:34:08 -05:00
|
|
|
match opt {
|
2013-07-02 14:47:32 -05:00
|
|
|
Some(ref t) => (*t).clone(),
|
|
|
|
None => diag.handler().bug(msg()),
|
2012-05-22 16:55:39 -05:00
|
|
|
}
|
|
|
|
}
|