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-11-06 02:05:53 -06:00
|
|
|
|
pub use self::Level::*;
|
|
|
|
|
pub use self::RenderSpan::*;
|
|
|
|
|
pub use self::ColorConfig::*;
|
|
|
|
|
use self::Destination::*;
|
|
|
|
|
|
2015-04-15 04:17:58 -05:00
|
|
|
|
use codemap::{self, COMMAND_LINE_SP, COMMAND_LINE_EXPN, Pos, Span};
|
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};
|
2015-04-15 04:17:58 -05:00
|
|
|
|
use std::{cmp, error, fmt};
|
2015-03-11 17:24:14 -05:00
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
use std::io;
|
2015-04-15 04:17:58 -05:00
|
|
|
|
use term::{self, WriterWrapper};
|
2015-03-11 17:24:14 -05:00
|
|
|
|
use libc;
|
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.
|
2015-02-27 08:36:53 -06:00
|
|
|
|
const MAX_LINES: usize = 6;
|
2013-10-21 14:10:57 -05:00
|
|
|
|
|
2015-04-09 13:48:15 -05:00
|
|
|
|
#[derive(Clone)]
|
2014-04-04 20:46:43 -05:00
|
|
|
|
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),
|
|
|
|
|
|
2015-04-09 13:44:56 -05:00
|
|
|
|
/// Similar to a FullSpan, but the cited position is the end of
|
|
|
|
|
/// the span, instead of the start. Used, at least, for telling
|
|
|
|
|
/// compiletest/runtest to look at the last line of the span
|
|
|
|
|
/// (since `end_highlight_lines` displays an arrow to the end
|
|
|
|
|
/// of the span).
|
|
|
|
|
EndSpan(Span),
|
|
|
|
|
|
2015-04-09 13:48:15 -05:00
|
|
|
|
/// A suggestion renders with both with an initial line for the
|
|
|
|
|
/// message, prefixed by file:linenum, followed by a summary
|
|
|
|
|
/// of hypothetical source code, where the `String` is spliced
|
|
|
|
|
/// into the lines in place of the code covered by the span.
|
|
|
|
|
Suggestion(Span, String),
|
|
|
|
|
|
2014-04-04 20:46:43 -05:00
|
|
|
|
/// A FileLine renders with just a line for the message prefixed
|
|
|
|
|
/// by file:linenum.
|
|
|
|
|
FileLine(Span),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RenderSpan {
|
2015-04-09 13:44:56 -05:00
|
|
|
|
fn span(&self) -> Span {
|
|
|
|
|
match *self {
|
|
|
|
|
FullSpan(s) |
|
2015-04-09 13:48:15 -05:00
|
|
|
|
Suggestion(s, _) |
|
2015-04-09 13:44:56 -05:00
|
|
|
|
EndSpan(s) |
|
|
|
|
|
FileLine(s) =>
|
|
|
|
|
s
|
2014-04-04 20:46:43 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 21:54:18 -06:00
|
|
|
|
#[derive(Clone, Copy)]
|
2014-05-08 08:10:03 -05:00
|
|
|
|
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
|
|
|
|
|
2015-04-04 15:47:40 -05:00
|
|
|
|
/// Used as a return value to signify a fatal error occurred. (It is also
|
|
|
|
|
/// used as the argument to panic at the moment, but that will eventually
|
|
|
|
|
/// not be true.)
|
2015-04-15 04:17:58 -05:00
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2015-03-28 16:58:51 -05:00
|
|
|
|
#[must_use]
|
2014-01-17 14:12:46 -06:00
|
|
|
|
pub struct FatalError;
|
|
|
|
|
|
2015-04-15 04:17:58 -05:00
|
|
|
|
impl fmt::Display for FatalError {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
|
write!(f, "parser fatal error")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl error::Error for FatalError {
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
"The parser has encountered a fatal error"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
2015-04-15 04:17:58 -05:00
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2014-03-13 18:00:07 -05:00
|
|
|
|
pub struct ExplicitBug;
|
|
|
|
|
|
2015-04-15 04:17:58 -05:00
|
|
|
|
impl fmt::Display for ExplicitBug {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
|
write!(f, "parser internal bug")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl error::Error for ExplicitBug {
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
"The parser has encountered an internal bug"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2015-05-13 15:00:17 -05:00
|
|
|
|
pub fn new(handler: Handler, cm: codemap::CodeMap) -> SpanHandler {
|
|
|
|
|
SpanHandler {
|
|
|
|
|
handler: handler,
|
|
|
|
|
cm: cm,
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-28 16:58:51 -05:00
|
|
|
|
pub fn span_fatal(&self, sp: Span, msg: &str) -> FatalError {
|
2014-03-16 13:56:24 -05:00
|
|
|
|
self.handler.emit(Some((&self.cm, sp)), msg, Fatal);
|
2015-03-28 16:58:51 -05:00
|
|
|
|
return FatalError;
|
2012-01-13 19:03:51 -06:00
|
|
|
|
}
|
2015-03-28 16:58:51 -05:00
|
|
|
|
pub fn span_fatal_with_code(&self, sp: Span, msg: &str, code: &str) -> FatalError {
|
2015-01-18 15:39:18 -06:00
|
|
|
|
self.handler.emit_with_code(Some((&self.cm, sp)), msg, code, Fatal);
|
2015-03-28 16:58:51 -05:00
|
|
|
|
return FatalError;
|
2015-01-18 15:39:18 -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) {
|
2015-04-09 13:44:56 -05:00
|
|
|
|
self.handler.custom_emit(&self.cm, EndSpan(sp), msg, Note);
|
2014-04-04 20:46:43 -05:00
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
2015-04-09 13:48:15 -05:00
|
|
|
|
/// Prints out a message with a suggested edit of the code.
|
|
|
|
|
///
|
|
|
|
|
/// See `diagnostic::RenderSpan::Suggestion` for more information.
|
|
|
|
|
pub fn span_suggestion(&self, sp: Span, msg: &str, suggestion: String) {
|
|
|
|
|
self.handler.custom_emit(&self.cm, Suggestion(sp, suggestion), 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
|
|
|
|
}
|
2015-01-13 17:49:17 -06:00
|
|
|
|
pub fn fileline_help(&self, sp: Span, msg: &str) {
|
|
|
|
|
self.handler.custom_emit(&self.cm, FileLine(sp), msg, Help);
|
|
|
|
|
}
|
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) -> ! {
|
2015-02-18 13:48:57 -06:00
|
|
|
|
self.span_bug(sp, &format!("unimplemented {}", 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2015-01-17 17:33:05 -06:00
|
|
|
|
err_count: Cell<usize>,
|
2014-06-14 13:03:34 -05:00
|
|
|
|
emit: RefCell<Box<Emitter + Send>>,
|
2015-01-26 17:42:24 -06:00
|
|
|
|
pub can_emit_warnings: bool
|
2013-12-31 08:17:59 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Handler {
|
2015-05-13 15:00:17 -05:00
|
|
|
|
pub fn new(color_config: ColorConfig,
|
|
|
|
|
registry: Option<diagnostics::registry::Registry>,
|
|
|
|
|
can_emit_warnings: bool) -> Handler {
|
|
|
|
|
let emitter = Box::new(EmitterWriter::stderr(color_config, registry));
|
|
|
|
|
Handler::with_emitter(can_emit_warnings, emitter)
|
|
|
|
|
}
|
|
|
|
|
pub fn with_emitter(can_emit_warnings: bool, e: Box<Emitter + Send>) -> Handler {
|
|
|
|
|
Handler {
|
|
|
|
|
err_count: Cell::new(0),
|
|
|
|
|
emit: RefCell::new(e),
|
|
|
|
|
can_emit_warnings: can_emit_warnings
|
|
|
|
|
}
|
|
|
|
|
}
|
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);
|
2015-07-20 11:23:31 -05:00
|
|
|
|
|
|
|
|
|
// Suppress the fatal error message from the panic below as we've
|
|
|
|
|
// already terminated in our own "legitimate" fashion.
|
|
|
|
|
io::set_panic(Box::new(io::sink()));
|
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) {
|
2015-01-27 19:01:48 -06:00
|
|
|
|
self.err_count.set(self.err_count.get() + 1);
|
2012-01-13 19:03:51 -06:00
|
|
|
|
}
|
2015-01-17 17:33:05 -06:00
|
|
|
|
pub fn err_count(&self) -> usize {
|
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 {
|
2015-01-27 19:01:48 -06:00
|
|
|
|
self.err_count.get() > 0
|
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() {
|
2015-01-27 19:01:48 -06:00
|
|
|
|
0 => return,
|
|
|
|
|
1 => s = "aborting due to previous error".to_string(),
|
2015-01-17 18:41:56 -06: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
|
|
|
|
}
|
2015-02-18 13:48:57 -06: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-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) -> ! {
|
2015-02-18 13:48:57 -06:00
|
|
|
|
self.bug(&format!("unimplemented {}", 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) {
|
2015-01-26 17:42:24 -06:00
|
|
|
|
if lvl == Warning && !self.can_emit_warnings { return }
|
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) {
|
2015-01-26 17:42:24 -06:00
|
|
|
|
if lvl == Warning && !self.can_emit_warnings { return }
|
2014-07-01 11:39:41 -05:00
|
|
|
|
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) {
|
2015-01-26 17:42:24 -06:00
|
|
|
|
if lvl == Warning && !self.can_emit_warnings { return }
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
|
#[derive(Copy, PartialEq, Clone, Debug)]
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-01-20 17:45:07 -06:00
|
|
|
|
impl fmt::Display for Level {
|
2014-02-19 20:56:33 -06:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-01-20 17:45:07 -06:00
|
|
|
|
use std::fmt::Display;
|
2014-02-19 20:56:33 -06:00
|
|
|
|
|
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
|
|
|
|
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>),
|
2015-03-11 17:24:14 -05:00
|
|
|
|
Raw(Box<Write + Send>),
|
2014-02-28 13:37:04 -06:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-06 22:19:50 -05:00
|
|
|
|
/// Do not use this for messages that end in `\n` – use `println_maybe_styled` instead. See
|
|
|
|
|
/// `EmitterWriter::print_maybe_styled` for details.
|
|
|
|
|
macro_rules! print_maybe_styled {
|
|
|
|
|
($writer: expr, $style: expr, $($arg: tt)*) => {
|
|
|
|
|
$writer.print_maybe_styled(format_args!($($arg)*), $style, false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! println_maybe_styled {
|
|
|
|
|
($writer: expr, $style: expr, $($arg: tt)*) => {
|
|
|
|
|
$writer.print_maybe_styled(format_args!($($arg)*), $style, true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2015-03-11 17:24:14 -05:00
|
|
|
|
let stderr = io::stderr();
|
2014-05-08 08:10:03 -05:00
|
|
|
|
|
|
|
|
|
let use_color = match color_config {
|
|
|
|
|
Always => true,
|
|
|
|
|
Never => false,
|
2015-03-11 17:24:14 -05:00
|
|
|
|
Auto => stderr_isatty(),
|
2014-05-08 08:10:03 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if use_color {
|
2014-04-08 10:54:54 -05:00
|
|
|
|
let dst = match term::stderr() {
|
|
|
|
|
Some(t) => Terminal(t),
|
2015-02-15 02:52:21 -06:00
|
|
|
|
None => Raw(Box::new(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 {
|
2015-02-15 02:52:21 -06:00
|
|
|
|
EmitterWriter { dst: Raw(Box::new(stderr)), registry: registry }
|
2014-02-28 13:37:04 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 17:24:14 -05:00
|
|
|
|
pub fn new(dst: Box<Write + Send>,
|
2014-07-01 11:39:41 -05:00
|
|
|
|
registry: Option<diagnostics::registry::Registry>) -> EmitterWriter {
|
|
|
|
|
EmitterWriter { dst: Raw(dst), registry: registry }
|
2014-02-28 13:37:04 -06:00
|
|
|
|
}
|
2015-07-06 18:11:20 -05:00
|
|
|
|
|
|
|
|
|
fn print_maybe_styled(&mut self,
|
2015-07-06 22:19:50 -05:00
|
|
|
|
args: fmt::Arguments,
|
|
|
|
|
color: term::attr::Attr,
|
|
|
|
|
print_newline_at_end: bool) -> io::Result<()> {
|
2015-07-06 18:11:20 -05:00
|
|
|
|
match self.dst {
|
|
|
|
|
Terminal(ref mut t) => {
|
|
|
|
|
try!(t.attr(color));
|
|
|
|
|
// 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.
|
2015-07-06 22:19:50 -05:00
|
|
|
|
try!(t.write_fmt(args));
|
|
|
|
|
try!(t.reset());
|
|
|
|
|
if print_newline_at_end {
|
|
|
|
|
t.write_all(b"\n")
|
|
|
|
|
} else {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Raw(ref mut w) => {
|
|
|
|
|
try!(w.write_fmt(args));
|
|
|
|
|
if print_newline_at_end {
|
|
|
|
|
w.write_all(b"\n")
|
2015-07-06 18:11:20 -05:00
|
|
|
|
} else {
|
2015-07-06 22:19:50 -05:00
|
|
|
|
Ok(())
|
2015-07-06 18:11:20 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn print_diagnostic(&mut self, topic: &str, lvl: Level,
|
|
|
|
|
msg: &str, code: Option<&str>) -> io::Result<()> {
|
|
|
|
|
if !topic.is_empty() {
|
|
|
|
|
try!(write!(&mut self.dst, "{} ", topic));
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-06 22:19:50 -05:00
|
|
|
|
try!(print_maybe_styled!(self, term::attr::ForegroundColor(lvl.color()),
|
|
|
|
|
"{}: ", lvl.to_string()));
|
|
|
|
|
try!(print_maybe_styled!(self, term::attr::Bold, "{}", msg));
|
2015-07-06 18:11:20 -05:00
|
|
|
|
|
|
|
|
|
match code {
|
|
|
|
|
Some(code) => {
|
|
|
|
|
let style = term::attr::ForegroundColor(term::color::BRIGHT_MAGENTA);
|
2015-07-06 22:19:50 -05:00
|
|
|
|
try!(print_maybe_styled!(self, style, " [{}]", code.clone()));
|
2015-07-06 18:11:20 -05:00
|
|
|
|
}
|
|
|
|
|
None => ()
|
|
|
|
|
}
|
|
|
|
|
try!(write!(&mut self.dst, "\n"));
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn emit_(&mut self, cm: &codemap::CodeMap, rsp: RenderSpan,
|
|
|
|
|
msg: &str, code: Option<&str>, lvl: Level) -> io::Result<()> {
|
|
|
|
|
let sp = rsp.span();
|
|
|
|
|
|
|
|
|
|
// We cannot check equality directly with COMMAND_LINE_SP
|
|
|
|
|
// since PartialEq is manually implemented to ignore the ExpnId
|
|
|
|
|
let ss = if sp.expn_id == COMMAND_LINE_EXPN {
|
|
|
|
|
"<command line option>".to_string()
|
|
|
|
|
} else if let EndSpan(_) = rsp {
|
|
|
|
|
let span_end = Span { lo: sp.hi, hi: sp.hi, expn_id: sp.expn_id};
|
|
|
|
|
cm.span_to_string(span_end)
|
|
|
|
|
} else {
|
|
|
|
|
cm.span_to_string(sp)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try!(self.print_diagnostic(&ss[..], lvl, msg, code));
|
|
|
|
|
|
|
|
|
|
match rsp {
|
|
|
|
|
FullSpan(_) => {
|
|
|
|
|
try!(self.highlight_lines(cm, sp, lvl, cm.span_to_lines(sp)));
|
|
|
|
|
try!(self.print_macro_backtrace(cm, sp));
|
|
|
|
|
}
|
|
|
|
|
EndSpan(_) => {
|
|
|
|
|
try!(self.end_highlight_lines(cm, sp, lvl, cm.span_to_lines(sp)));
|
|
|
|
|
try!(self.print_macro_backtrace(cm, sp));
|
|
|
|
|
}
|
|
|
|
|
Suggestion(_, ref suggestion) => {
|
|
|
|
|
try!(self.highlight_suggestion(cm, sp, suggestion));
|
|
|
|
|
try!(self.print_macro_backtrace(cm, sp));
|
|
|
|
|
}
|
|
|
|
|
FileLine(..) => {
|
|
|
|
|
// no source text in this case!
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match code {
|
|
|
|
|
Some(code) =>
|
|
|
|
|
match self.registry.as_ref().and_then(|registry| registry.find_description(code)) {
|
|
|
|
|
Some(_) => {
|
|
|
|
|
try!(self.print_diagnostic(&ss[..], Help,
|
|
|
|
|
&format!("run `rustc --explain {}` to see a \
|
|
|
|
|
detailed explanation", code), None));
|
|
|
|
|
}
|
|
|
|
|
None => ()
|
|
|
|
|
},
|
|
|
|
|
None => (),
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn highlight_suggestion(&mut self,
|
|
|
|
|
cm: &codemap::CodeMap,
|
|
|
|
|
sp: Span,
|
|
|
|
|
suggestion: &str)
|
|
|
|
|
-> io::Result<()>
|
|
|
|
|
{
|
|
|
|
|
let lines = cm.span_to_lines(sp).unwrap();
|
|
|
|
|
assert!(!lines.lines.is_empty());
|
|
|
|
|
|
|
|
|
|
// To build up the result, we want to take the snippet from the first
|
|
|
|
|
// line that precedes the span, prepend that with the suggestion, and
|
|
|
|
|
// then append the snippet from the last line that trails the span.
|
|
|
|
|
let fm = &lines.file;
|
|
|
|
|
|
|
|
|
|
let first_line = &lines.lines[0];
|
|
|
|
|
let prefix = fm.get_line(first_line.line_index)
|
|
|
|
|
.map(|l| &l[..first_line.start_col.0])
|
|
|
|
|
.unwrap_or("");
|
|
|
|
|
|
|
|
|
|
let last_line = lines.lines.last().unwrap();
|
|
|
|
|
let suffix = fm.get_line(last_line.line_index)
|
|
|
|
|
.map(|l| &l[last_line.end_col.0..])
|
|
|
|
|
.unwrap_or("");
|
|
|
|
|
|
|
|
|
|
let complete = format!("{}{}{}", prefix, suggestion, suffix);
|
|
|
|
|
|
|
|
|
|
// print the suggestion without any line numbers, but leave
|
|
|
|
|
// space for them. This helps with lining up with previous
|
|
|
|
|
// snippets from the actual error being reported.
|
|
|
|
|
let fm = &*lines.file;
|
|
|
|
|
let mut lines = complete.lines();
|
|
|
|
|
for (line, line_index) in lines.by_ref().take(MAX_LINES).zip(first_line.line_index..) {
|
|
|
|
|
let elided_line_num = format!("{}", line_index+1);
|
|
|
|
|
try!(write!(&mut self.dst, "{0}:{1:2$} {3}\n",
|
|
|
|
|
fm.name, "", elided_line_num.len(), line));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if we elided some lines, add an ellipsis
|
|
|
|
|
if lines.next().is_some() {
|
|
|
|
|
let elided_line_num = format!("{}", first_line.line_index + MAX_LINES + 1);
|
|
|
|
|
try!(write!(&mut self.dst, "{0:1$} {0:2$} ...\n",
|
|
|
|
|
"", fm.name.len(), elided_line_num.len()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn highlight_lines(&mut self,
|
|
|
|
|
cm: &codemap::CodeMap,
|
|
|
|
|
sp: Span,
|
|
|
|
|
lvl: Level,
|
|
|
|
|
lines: codemap::FileLinesResult)
|
|
|
|
|
-> io::Result<()>
|
|
|
|
|
{
|
|
|
|
|
let lines = match lines {
|
|
|
|
|
Ok(lines) => lines,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
try!(write!(&mut self.dst, "(internal compiler error: unprintable span)\n"));
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let fm = &*lines.file;
|
|
|
|
|
|
|
|
|
|
let line_strings: Option<Vec<&str>> =
|
|
|
|
|
lines.lines.iter()
|
|
|
|
|
.map(|info| fm.get_line(info.line_index))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let line_strings = match line_strings {
|
|
|
|
|
None => { return Ok(()); }
|
|
|
|
|
Some(line_strings) => line_strings
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Display only the first MAX_LINES lines.
|
|
|
|
|
let all_lines = lines.lines.len();
|
|
|
|
|
let display_lines = cmp::min(all_lines, MAX_LINES);
|
|
|
|
|
let display_line_infos = &lines.lines[..display_lines];
|
|
|
|
|
let display_line_strings = &line_strings[..display_lines];
|
|
|
|
|
|
|
|
|
|
// Calculate the widest number to format evenly and fix #11715
|
|
|
|
|
assert!(display_line_infos.len() > 0);
|
|
|
|
|
let mut max_line_num = display_line_infos[display_line_infos.len() - 1].line_index + 1;
|
|
|
|
|
let mut digits = 0;
|
|
|
|
|
while max_line_num > 0 {
|
|
|
|
|
max_line_num /= 10;
|
|
|
|
|
digits += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print the offending lines
|
|
|
|
|
for (line_info, line) in display_line_infos.iter().zip(display_line_strings) {
|
|
|
|
|
try!(write!(&mut self.dst, "{}:{:>width$} {}\n",
|
|
|
|
|
fm.name,
|
|
|
|
|
line_info.line_index + 1,
|
|
|
|
|
line,
|
|
|
|
|
width=digits));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we elided something, put an ellipsis.
|
|
|
|
|
if display_lines < all_lines {
|
|
|
|
|
let last_line_index = display_line_infos.last().unwrap().line_index;
|
|
|
|
|
let s = format!("{}:{} ", fm.name, last_line_index + 1);
|
|
|
|
|
try!(write!(&mut self.dst, "{0:1$}...\n", "", s.len()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME (#3260)
|
|
|
|
|
// If there's one line at fault we can easily point to the problem
|
|
|
|
|
if lines.lines.len() == 1 {
|
|
|
|
|
let lo = cm.lookup_char_pos(sp.lo);
|
|
|
|
|
let mut digits = 0;
|
|
|
|
|
let mut num = (lines.lines[0].line_index + 1) / 10;
|
|
|
|
|
|
|
|
|
|
// how many digits must be indent past?
|
|
|
|
|
while num > 0 { num /= 10; digits += 1; }
|
|
|
|
|
|
|
|
|
|
let mut s = String::new();
|
|
|
|
|
// Skip is the number of characters we need to skip because they are
|
|
|
|
|
// part of the 'filename:line ' part of the previous line.
|
|
|
|
|
let skip = fm.name.chars().count() + digits + 3;
|
|
|
|
|
for _ in 0..skip {
|
|
|
|
|
s.push(' ');
|
|
|
|
|
}
|
|
|
|
|
if let Some(orig) = fm.get_line(lines.lines[0].line_index) {
|
|
|
|
|
let mut col = skip;
|
|
|
|
|
let mut lastc = ' ';
|
|
|
|
|
let mut iter = orig.chars().enumerate();
|
|
|
|
|
for (pos, ch) in iter.by_ref() {
|
|
|
|
|
lastc = ch;
|
|
|
|
|
if pos >= lo.col.to_usize() { break; }
|
|
|
|
|
// 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.
|
|
|
|
|
match ch {
|
|
|
|
|
'\t' => {
|
|
|
|
|
col += 8 - col%8;
|
|
|
|
|
s.push('\t');
|
|
|
|
|
},
|
|
|
|
|
_ => {
|
|
|
|
|
col += 1;
|
|
|
|
|
s.push(' ');
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try!(write!(&mut self.dst, "{}", s));
|
|
|
|
|
let mut s = String::from("^");
|
|
|
|
|
let count = match lastc {
|
|
|
|
|
// Most terminals have a tab stop every eight columns by default
|
|
|
|
|
'\t' => 8 - col%8,
|
|
|
|
|
_ => 1,
|
|
|
|
|
};
|
|
|
|
|
col += count;
|
|
|
|
|
s.extend(::std::iter::repeat('~').take(count));
|
|
|
|
|
|
|
|
|
|
let hi = cm.lookup_char_pos(sp.hi);
|
|
|
|
|
if hi.col != lo.col {
|
|
|
|
|
for (pos, ch) in iter {
|
|
|
|
|
if pos >= hi.col.to_usize() { break; }
|
|
|
|
|
let count = match ch {
|
|
|
|
|
'\t' => 8 - col%8,
|
|
|
|
|
_ => 1,
|
|
|
|
|
};
|
|
|
|
|
col += count;
|
|
|
|
|
s.extend(::std::iter::repeat('~').take(count));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.len() > 1 {
|
|
|
|
|
// One extra squiggly is replaced by a "^"
|
|
|
|
|
s.pop();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-06 22:19:50 -05:00
|
|
|
|
try!(println_maybe_styled!(self, term::attr::ForegroundColor(lvl.color()),
|
|
|
|
|
"{}", s));
|
2015-07-06 18:11:20 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Here are the differences between this and the normal `highlight_lines`:
|
|
|
|
|
/// `end_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), `end_highlight_lines` will print the first line, then
|
|
|
|
|
/// dot dot dot, then last line, whereas `highlight_lines` prints the first
|
|
|
|
|
/// six lines.
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
fn end_highlight_lines(&mut self,
|
|
|
|
|
cm: &codemap::CodeMap,
|
|
|
|
|
sp: Span,
|
|
|
|
|
lvl: Level,
|
|
|
|
|
lines: codemap::FileLinesResult)
|
|
|
|
|
-> io::Result<()> {
|
|
|
|
|
let lines = match lines {
|
|
|
|
|
Ok(lines) => lines,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
try!(write!(&mut self.dst, "(internal compiler error: unprintable span)\n"));
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let fm = &*lines.file;
|
|
|
|
|
|
|
|
|
|
let lines = &lines.lines[..];
|
|
|
|
|
if lines.len() > MAX_LINES {
|
|
|
|
|
if let Some(line) = fm.get_line(lines[0].line_index) {
|
|
|
|
|
try!(write!(&mut self.dst, "{}:{} {}\n", fm.name,
|
|
|
|
|
lines[0].line_index + 1, line));
|
|
|
|
|
}
|
|
|
|
|
try!(write!(&mut self.dst, "...\n"));
|
|
|
|
|
let last_line_index = lines[lines.len() - 1].line_index;
|
|
|
|
|
if let Some(last_line) = fm.get_line(last_line_index) {
|
|
|
|
|
try!(write!(&mut self.dst, "{}:{} {}\n", fm.name,
|
|
|
|
|
last_line_index + 1, last_line));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for line_info in lines {
|
|
|
|
|
if let Some(line) = fm.get_line(line_info.line_index) {
|
|
|
|
|
try!(write!(&mut self.dst, "{}:{} {}\n", fm.name,
|
|
|
|
|
line_info.line_index + 1, line));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let last_line_start = format!("{}:{} ", fm.name, lines[lines.len()-1].line_index + 1);
|
|
|
|
|
let hi = cm.lookup_char_pos(sp.hi);
|
|
|
|
|
let skip = last_line_start.chars().count();
|
|
|
|
|
let mut s = String::new();
|
|
|
|
|
for _ in 0..skip {
|
|
|
|
|
s.push(' ');
|
|
|
|
|
}
|
|
|
|
|
if let Some(orig) = fm.get_line(lines[0].line_index) {
|
|
|
|
|
let iter = orig.chars().enumerate();
|
|
|
|
|
for (pos, ch) in iter {
|
|
|
|
|
// Span seems to use half-opened interval, so subtract 1
|
|
|
|
|
if pos >= hi.col.to_usize() - 1 { break; }
|
|
|
|
|
// 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.
|
|
|
|
|
match ch {
|
|
|
|
|
'\t' => s.push('\t'),
|
|
|
|
|
_ => s.push(' '),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s.push('^');
|
2015-07-06 22:19:50 -05:00
|
|
|
|
println_maybe_styled!(self, term::attr::ForegroundColor(lvl.color()),
|
|
|
|
|
"{}", s)
|
2015-07-06 18:11:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn print_macro_backtrace(&mut self,
|
|
|
|
|
cm: &codemap::CodeMap,
|
|
|
|
|
sp: Span)
|
|
|
|
|
-> io::Result<()> {
|
|
|
|
|
let cs = try!(cm.with_expn_info(sp.expn_id, |expn_info| -> io::Result<_> {
|
|
|
|
|
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 => ("", "!"),
|
|
|
|
|
codemap::CompilerExpansion => ("", ""),
|
|
|
|
|
};
|
|
|
|
|
try!(self.print_diagnostic(&ss, Note,
|
|
|
|
|
&format!("in expansion of {}{}{}",
|
|
|
|
|
pre,
|
|
|
|
|
ei.callee.name,
|
|
|
|
|
post),
|
|
|
|
|
None));
|
|
|
|
|
let ss = cm.span_to_string(ei.call_site);
|
|
|
|
|
try!(self.print_diagnostic(&ss, Note, "expansion site", None));
|
|
|
|
|
Ok(Some(ei.call_site))
|
|
|
|
|
}
|
|
|
|
|
None => Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
cs.map_or(Ok(()), |call_site| self.print_macro_backtrace(cm, call_site))
|
|
|
|
|
}
|
2014-02-28 13:37:04 -06:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 17:24:14 -05:00
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
fn stderr_isatty() -> bool {
|
|
|
|
|
unsafe { libc::isatty(libc::STDERR_FILENO) != 0 }
|
|
|
|
|
}
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn stderr_isatty() -> bool {
|
2015-04-01 20:44:53 -05:00
|
|
|
|
const STD_ERROR_HANDLE: libc::DWORD = -12i32 as libc::DWORD;
|
2015-03-11 17:24:14 -05:00
|
|
|
|
extern "system" {
|
|
|
|
|
fn GetStdHandle(which: libc::DWORD) -> libc::HANDLE;
|
|
|
|
|
fn GetConsoleMode(hConsoleHandle: libc::HANDLE,
|
|
|
|
|
lpMode: libc::LPDWORD) -> libc::BOOL;
|
|
|
|
|
}
|
|
|
|
|
unsafe {
|
|
|
|
|
let handle = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
|
let mut out = 0;
|
|
|
|
|
GetConsoleMode(handle, &mut out) != 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Write for Destination {
|
|
|
|
|
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
|
|
|
|
|
match *self {
|
|
|
|
|
Terminal(ref mut t) => t.write(bytes),
|
|
|
|
|
Raw(ref mut w) => w.write(bytes),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
2014-02-28 13:37:04 -06:00
|
|
|
|
match *self {
|
2015-03-11 17:24:14 -05:00
|
|
|
|
Terminal(ref mut t) => t.flush(),
|
|
|
|
|
Raw(ref mut w) => w.flush(),
|
2014-02-28 13:37:04 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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 {
|
2015-07-06 18:11:20 -05:00
|
|
|
|
Some((cm, COMMAND_LINE_SP)) => self.emit_(cm,
|
2014-12-10 14:18:23 -06:00
|
|
|
|
FileLine(COMMAND_LINE_SP),
|
2015-04-09 13:44:56 -05:00
|
|
|
|
msg, code, lvl),
|
2015-07-06 18:11:20 -05:00
|
|
|
|
Some((cm, sp)) => self.emit_(cm, FullSpan(sp), msg, code, lvl),
|
|
|
|
|
None => self.print_diagnostic("", lvl, msg, code),
|
2014-01-29 19:39:21 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match error {
|
|
|
|
|
Ok(()) => {}
|
2014-12-20 02:09:35 -06: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) {
|
2015-07-06 18:11:20 -05:00
|
|
|
|
match self.emit_(cm, sp, msg, None, lvl) {
|
2014-01-29 19:39:21 -06:00
|
|
|
|
Ok(()) => {}
|
2014-12-20 02:09:35 -06: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-12-08 12:28:32 -06:00
|
|
|
|
pub fn expect<T, M>(diag: &SpanHandler, opt: Option<T>, msg: M) -> T where
|
|
|
|
|
M: FnOnce() -> String,
|
|
|
|
|
{
|
2012-08-06 14:34:08 -05:00
|
|
|
|
match opt {
|
2014-09-13 11:06:01 -05:00
|
|
|
|
Some(t) => t,
|
2015-02-18 13:48:57 -06:00
|
|
|
|
None => diag.handler().bug(&msg()),
|
2012-05-22 16:55:39 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-23 04:43:27 -05:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
2015-07-06 18:11:20 -05:00
|
|
|
|
use super::{EmitterWriter, Level};
|
2015-06-23 04:43:27 -05:00
|
|
|
|
use codemap::{mk_sp, CodeMap, BytePos};
|
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
use std::io::{self, Write};
|
|
|
|
|
use std::str::from_utf8;
|
|
|
|
|
|
|
|
|
|
// Diagnostic doesn't align properly in span where line number increases by one digit
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_hilight_suggestion_issue_11715() {
|
|
|
|
|
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 mut ew = EmitterWriter::new(Box::new(Sink(data.clone())), None);
|
|
|
|
|
let cm = CodeMap::new();
|
|
|
|
|
let content = "abcdefg
|
|
|
|
|
koksi
|
|
|
|
|
line3
|
|
|
|
|
line4
|
|
|
|
|
cinq
|
|
|
|
|
line6
|
|
|
|
|
line7
|
|
|
|
|
line8
|
|
|
|
|
line9
|
|
|
|
|
line10
|
|
|
|
|
e-lä-vän
|
|
|
|
|
tolv
|
|
|
|
|
dreizehn
|
|
|
|
|
";
|
2015-07-02 00:14:14 -05:00
|
|
|
|
let file = cm.new_filemap_and_lines("dummy.txt", content);
|
2015-06-23 04:43:27 -05:00
|
|
|
|
let start = file.lines.borrow()[7];
|
|
|
|
|
let end = file.lines.borrow()[11];
|
|
|
|
|
let sp = mk_sp(start, end);
|
|
|
|
|
let lvl = Level::Error;
|
|
|
|
|
println!("span_to_lines");
|
|
|
|
|
let lines = cm.span_to_lines(sp);
|
|
|
|
|
println!("highlight_lines");
|
2015-07-06 18:11:20 -05:00
|
|
|
|
ew.highlight_lines(&cm, sp, lvl, lines).unwrap();
|
2015-06-23 04:43:27 -05:00
|
|
|
|
println!("done");
|
|
|
|
|
let vec = data.lock().unwrap().clone();
|
|
|
|
|
let vec: &[u8] = &vec;
|
2015-07-01 22:37:52 -05:00
|
|
|
|
let str = from_utf8(vec).unwrap();
|
|
|
|
|
println!("{}", str);
|
|
|
|
|
assert_eq!(str, "dummy.txt: 8 line8\n\
|
|
|
|
|
dummy.txt: 9 line9\n\
|
|
|
|
|
dummy.txt:10 line10\n\
|
|
|
|
|
dummy.txt:11 e-lä-vän\n\
|
|
|
|
|
dummy.txt:12 tolv\n");
|
2015-06-23 04:43:27 -05:00
|
|
|
|
}
|
|
|
|
|
}
|