2016-04-15 20:23:50 -05:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2015-12-30 19:43:42 -06:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! A JSON emitter for errors.
|
2015-12-30 23:47:14 -06:00
|
|
|
//!
|
|
|
|
//! This works by converting errors to a simplified structural format (see the
|
|
|
|
//! structs at the start of the file) and then serialising them. These should
|
|
|
|
//! contain as much information about the error as possible.
|
|
|
|
//!
|
|
|
|
//! The format of the JSON output should be considered *unstable*. For now the
|
|
|
|
//! structs at the end of this file (Diagnostic*) specify the error format.
|
|
|
|
|
|
|
|
// FIXME spec the JSON output properly.
|
|
|
|
|
2016-06-21 17:08:13 -05:00
|
|
|
use codemap::CodeMap;
|
|
|
|
use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan};
|
|
|
|
use errors::registry::Registry;
|
2016-07-06 11:08:16 -05:00
|
|
|
use errors::{DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion, CodeMapper};
|
2015-12-30 19:43:42 -06:00
|
|
|
use errors::emitter::Emitter;
|
|
|
|
|
|
|
|
use std::rc::Rc;
|
2015-12-30 23:47:14 -06:00
|
|
|
use std::io::{self, Write};
|
2016-04-15 20:23:50 -05:00
|
|
|
use std::vec;
|
2015-12-30 23:47:14 -06:00
|
|
|
|
|
|
|
use rustc_serialize::json::as_json;
|
2015-12-30 19:43:42 -06:00
|
|
|
|
|
|
|
pub struct JsonEmitter {
|
2015-12-30 23:47:14 -06:00
|
|
|
dst: Box<Write + Send>,
|
|
|
|
registry: Option<Registry>,
|
2016-06-21 17:08:13 -05:00
|
|
|
cm: Rc<CodeMapper + 'static>,
|
2015-12-30 19:43:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl JsonEmitter {
|
2016-09-26 17:45:50 -05:00
|
|
|
pub fn stderr(registry: Option<Registry>,
|
|
|
|
code_map: Rc<CodeMap>) -> JsonEmitter {
|
|
|
|
JsonEmitter {
|
|
|
|
dst: Box::new(io::stderr()),
|
|
|
|
registry: registry,
|
|
|
|
cm: code_map,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 19:43:42 -06:00
|
|
|
pub fn basic() -> JsonEmitter {
|
2015-12-30 23:47:14 -06:00
|
|
|
JsonEmitter::stderr(None, Rc::new(CodeMap::new()))
|
2015-12-30 19:43:42 -06:00
|
|
|
}
|
|
|
|
|
2016-09-26 17:45:50 -05:00
|
|
|
pub fn new(dst: Box<Write + Send>,
|
|
|
|
registry: Option<Registry>,
|
|
|
|
code_map: Rc<CodeMap>) -> JsonEmitter {
|
2015-12-30 19:43:42 -06:00
|
|
|
JsonEmitter {
|
2016-09-26 17:45:50 -05:00
|
|
|
dst: dst,
|
2015-12-30 23:47:14 -06:00
|
|
|
registry: registry,
|
|
|
|
cm: code_map,
|
2015-12-30 19:43:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Emitter for JsonEmitter {
|
2016-07-07 06:50:49 -05:00
|
|
|
fn emit(&mut self, db: &DiagnosticBuilder) {
|
2015-12-30 23:47:14 -06:00
|
|
|
let data = Diagnostic::from_diagnostic_builder(db, self);
|
|
|
|
if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
|
|
|
|
panic!("failed to print diagnostics: {:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following data types are provided just for serialisation.
|
|
|
|
|
|
|
|
#[derive(RustcEncodable)]
|
|
|
|
struct Diagnostic<'a> {
|
|
|
|
/// The primary error message.
|
|
|
|
message: &'a str,
|
|
|
|
code: Option<DiagnosticCode>,
|
|
|
|
/// "error: internal compiler error", "error", "warning", "note", "help".
|
|
|
|
level: &'static str,
|
2015-12-13 06:12:47 -06:00
|
|
|
spans: Vec<DiagnosticSpan>,
|
2016-04-15 20:23:50 -05:00
|
|
|
/// Associated diagnostic messages.
|
2015-12-30 23:47:14 -06:00
|
|
|
children: Vec<Diagnostic<'a>>,
|
2016-04-15 20:23:50 -05:00
|
|
|
/// The message as rustc would render it. Currently this is only
|
|
|
|
/// `Some` for "suggestions", but eventually it will include all
|
|
|
|
/// snippets.
|
|
|
|
rendered: Option<String>,
|
2015-12-30 23:47:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(RustcEncodable)]
|
|
|
|
struct DiagnosticSpan {
|
|
|
|
file_name: String,
|
|
|
|
byte_start: u32,
|
|
|
|
byte_end: u32,
|
|
|
|
/// 1-based.
|
|
|
|
line_start: usize,
|
|
|
|
line_end: usize,
|
|
|
|
/// 1-based, character offset.
|
|
|
|
column_start: usize,
|
|
|
|
column_end: usize,
|
2016-04-20 13:57:20 -05:00
|
|
|
/// Is this a "primary" span -- meaning the point, or one of the points,
|
|
|
|
/// where the error occurred?
|
|
|
|
is_primary: bool,
|
2016-03-23 21:32:42 -05:00
|
|
|
/// Source text from the start of line_start to the end of line_end.
|
|
|
|
text: Vec<DiagnosticSpanLine>,
|
2016-04-20 13:57:20 -05:00
|
|
|
/// Label that should be placed at this location (if any)
|
|
|
|
label: Option<String>,
|
2016-04-15 20:23:50 -05:00
|
|
|
/// If we are suggesting a replacement, this will contain text
|
|
|
|
/// that should be sliced in atop this span. You may prefer to
|
|
|
|
/// load the fully rendered version from the parent `Diagnostic`,
|
|
|
|
/// however.
|
|
|
|
suggested_replacement: Option<String>,
|
|
|
|
/// Macro invocations that created the code at this span, if any.
|
|
|
|
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
|
2016-03-23 21:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(RustcEncodable)]
|
|
|
|
struct DiagnosticSpanLine {
|
|
|
|
text: String,
|
2016-04-15 20:23:50 -05:00
|
|
|
|
2016-03-23 21:32:42 -05:00
|
|
|
/// 1-based, character offset in self.text.
|
|
|
|
highlight_start: usize,
|
2016-04-15 20:23:50 -05:00
|
|
|
|
2016-03-23 21:32:42 -05:00
|
|
|
highlight_end: usize,
|
2015-12-30 23:47:14 -06:00
|
|
|
}
|
2015-12-30 19:43:42 -06:00
|
|
|
|
2016-04-15 20:23:50 -05:00
|
|
|
#[derive(RustcEncodable)]
|
|
|
|
struct DiagnosticSpanMacroExpansion {
|
|
|
|
/// span where macro was applied to generate this code; note that
|
|
|
|
/// this may itself derive from a macro (if
|
|
|
|
/// `span.expansion.is_some()`)
|
|
|
|
span: DiagnosticSpan,
|
|
|
|
|
|
|
|
/// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
|
|
|
|
macro_decl_name: String,
|
|
|
|
|
|
|
|
/// span where macro was defined (if known)
|
|
|
|
def_site_span: Option<DiagnosticSpan>,
|
|
|
|
}
|
|
|
|
|
2015-12-30 23:47:14 -06:00
|
|
|
#[derive(RustcEncodable)]
|
|
|
|
struct DiagnosticCode {
|
|
|
|
/// The code itself.
|
|
|
|
code: String,
|
|
|
|
/// An explanation for the code.
|
|
|
|
explanation: Option<&'static str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Diagnostic<'a> {
|
|
|
|
fn from_diagnostic_builder<'c>(db: &'c DiagnosticBuilder,
|
|
|
|
je: &JsonEmitter)
|
|
|
|
-> Diagnostic<'c> {
|
|
|
|
Diagnostic {
|
|
|
|
message: &db.message,
|
|
|
|
code: DiagnosticCode::map_opt_string(db.code.clone(), je),
|
|
|
|
level: db.level.to_str(),
|
2016-04-20 13:57:20 -05:00
|
|
|
spans: DiagnosticSpan::from_multispan(&db.span, je),
|
2015-12-30 23:47:14 -06:00
|
|
|
children: db.children.iter().map(|c| {
|
|
|
|
Diagnostic::from_sub_diagnostic(c, je)
|
|
|
|
}).collect(),
|
2016-04-15 20:23:50 -05:00
|
|
|
rendered: None,
|
2015-12-30 23:47:14 -06:00
|
|
|
}
|
2015-12-30 19:43:42 -06:00
|
|
|
}
|
|
|
|
|
2015-12-30 23:47:14 -06:00
|
|
|
fn from_sub_diagnostic<'c>(db: &'c SubDiagnostic, je: &JsonEmitter) -> Diagnostic<'c> {
|
|
|
|
Diagnostic {
|
|
|
|
message: &db.message,
|
|
|
|
code: None,
|
|
|
|
level: db.level.to_str(),
|
2015-12-13 06:12:47 -06:00
|
|
|
spans: db.render_span.as_ref()
|
|
|
|
.map(|sp| DiagnosticSpan::from_render_span(sp, je))
|
2016-04-20 13:57:20 -05:00
|
|
|
.unwrap_or_else(|| DiagnosticSpan::from_multispan(&db.span, je)),
|
2015-12-30 23:47:14 -06:00
|
|
|
children: vec![],
|
2016-04-15 20:23:50 -05:00
|
|
|
rendered: db.render_span.as_ref()
|
|
|
|
.and_then(|rsp| je.render(rsp)),
|
2015-12-30 23:47:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticSpan {
|
2016-04-20 13:57:20 -05:00
|
|
|
fn from_span_label(span: SpanLabel,
|
|
|
|
suggestion: Option<&String>,
|
|
|
|
je: &JsonEmitter)
|
|
|
|
-> DiagnosticSpan {
|
|
|
|
Self::from_span_etc(span.span,
|
|
|
|
span.is_primary,
|
|
|
|
span.label,
|
|
|
|
suggestion,
|
|
|
|
je)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_span_etc(span: Span,
|
|
|
|
is_primary: bool,
|
|
|
|
label: Option<String>,
|
|
|
|
suggestion: Option<&String>,
|
|
|
|
je: &JsonEmitter)
|
|
|
|
-> DiagnosticSpan {
|
2016-04-15 20:23:50 -05:00
|
|
|
// obtain the full backtrace from the `macro_backtrace`
|
|
|
|
// helper; in some ways, it'd be better to expand the
|
|
|
|
// backtrace ourselves, but the `macro_backtrace` helper makes
|
|
|
|
// some decision, such as dropping some frames, and I don't
|
|
|
|
// want to duplicate that logic here.
|
|
|
|
let backtrace = je.cm.macro_backtrace(span).into_iter();
|
2016-04-20 13:57:20 -05:00
|
|
|
DiagnosticSpan::from_span_full(span,
|
|
|
|
is_primary,
|
|
|
|
label,
|
|
|
|
suggestion,
|
|
|
|
backtrace,
|
|
|
|
je)
|
2016-04-15 20:23:50 -05:00
|
|
|
}
|
|
|
|
|
2016-04-20 13:57:20 -05:00
|
|
|
fn from_span_full(span: Span,
|
|
|
|
is_primary: bool,
|
|
|
|
label: Option<String>,
|
|
|
|
suggestion: Option<&String>,
|
|
|
|
mut backtrace: vec::IntoIter<MacroBacktrace>,
|
|
|
|
je: &JsonEmitter)
|
|
|
|
-> DiagnosticSpan {
|
2016-04-15 20:23:50 -05:00
|
|
|
let start = je.cm.lookup_char_pos(span.lo);
|
|
|
|
let end = je.cm.lookup_char_pos(span.hi);
|
2016-04-20 13:57:20 -05:00
|
|
|
let backtrace_step = backtrace.next().map(|bt| {
|
|
|
|
let call_site =
|
|
|
|
Self::from_span_full(bt.call_site,
|
|
|
|
false,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
backtrace,
|
|
|
|
je);
|
|
|
|
let def_site_span = bt.def_site_span.map(|sp| {
|
|
|
|
Self::from_span_full(sp,
|
|
|
|
false,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
vec![].into_iter(),
|
|
|
|
je)
|
|
|
|
});
|
|
|
|
Box::new(DiagnosticSpanMacroExpansion {
|
|
|
|
span: call_site,
|
|
|
|
macro_decl_name: bt.macro_decl_name,
|
|
|
|
def_site_span: def_site_span,
|
|
|
|
})
|
|
|
|
});
|
2016-04-15 20:23:50 -05:00
|
|
|
DiagnosticSpan {
|
|
|
|
file_name: start.file.name.clone(),
|
|
|
|
byte_start: span.lo.0,
|
|
|
|
byte_end: span.hi.0,
|
|
|
|
line_start: start.line,
|
|
|
|
line_end: end.line,
|
|
|
|
column_start: start.col.0 + 1,
|
|
|
|
column_end: end.col.0 + 1,
|
2016-04-20 13:57:20 -05:00
|
|
|
is_primary: is_primary,
|
2016-04-15 20:23:50 -05:00
|
|
|
text: DiagnosticSpanLine::from_span(span, je),
|
|
|
|
suggested_replacement: suggestion.cloned(),
|
|
|
|
expansion: backtrace_step,
|
2016-04-20 13:57:20 -05:00
|
|
|
label: label,
|
2016-04-15 20:23:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 06:12:47 -06:00
|
|
|
fn from_multispan(msp: &MultiSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
|
2016-04-20 13:57:20 -05:00
|
|
|
msp.span_labels()
|
|
|
|
.into_iter()
|
|
|
|
.map(|span_str| Self::from_span_label(span_str, None, je))
|
|
|
|
.collect()
|
2016-04-15 20:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_suggestion(suggestion: &CodeSuggestion, je: &JsonEmitter)
|
|
|
|
-> Vec<DiagnosticSpan> {
|
2016-04-20 13:57:20 -05:00
|
|
|
assert_eq!(suggestion.msp.span_labels().len(), suggestion.substitutes.len());
|
|
|
|
suggestion.msp.span_labels()
|
|
|
|
.into_iter()
|
|
|
|
.zip(&suggestion.substitutes)
|
|
|
|
.map(|(span_label, suggestion)| {
|
|
|
|
DiagnosticSpan::from_span_label(span_label,
|
|
|
|
Some(suggestion),
|
|
|
|
je)
|
|
|
|
})
|
|
|
|
.collect()
|
2015-12-30 23:47:14 -06:00
|
|
|
}
|
|
|
|
|
2015-12-13 06:12:47 -06:00
|
|
|
fn from_render_span(rsp: &RenderSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
|
|
|
|
match *rsp {
|
2016-04-20 13:57:20 -05:00
|
|
|
RenderSpan::FullSpan(ref msp) =>
|
|
|
|
DiagnosticSpan::from_multispan(msp, je),
|
|
|
|
RenderSpan::Suggestion(ref suggestion) =>
|
|
|
|
DiagnosticSpan::from_suggestion(suggestion, je),
|
2016-04-03 17:32:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticSpanLine {
|
2016-06-21 17:08:13 -05:00
|
|
|
fn line_from_filemap(fm: &syntax_pos::FileMap,
|
2016-04-03 17:32:37 -05:00
|
|
|
index: usize,
|
|
|
|
h_start: usize,
|
|
|
|
h_end: usize)
|
|
|
|
-> DiagnosticSpanLine {
|
|
|
|
DiagnosticSpanLine {
|
|
|
|
text: fm.get_line(index).unwrap().to_owned(),
|
|
|
|
highlight_start: h_start,
|
|
|
|
highlight_end: h_end,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a list of DiagnosticSpanLines from span - each line with any part
|
|
|
|
/// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
|
|
|
|
/// `span` within the line.
|
2016-04-15 20:23:50 -05:00
|
|
|
fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
|
|
|
|
je.cm.span_to_lines(span)
|
|
|
|
.map(|lines| {
|
|
|
|
let fm = &*lines.file;
|
|
|
|
lines.lines
|
|
|
|
.iter()
|
|
|
|
.map(|line| {
|
|
|
|
DiagnosticSpanLine::line_from_filemap(fm,
|
|
|
|
line.line_index,
|
|
|
|
line.start_col.0 + 1,
|
|
|
|
line.end_col.0 + 1)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.unwrap_or(vec![])
|
2016-04-03 17:32:37 -05:00
|
|
|
}
|
2016-03-23 21:32:42 -05:00
|
|
|
}
|
|
|
|
|
2015-12-30 23:47:14 -06:00
|
|
|
impl DiagnosticCode {
|
|
|
|
fn map_opt_string(s: Option<String>, je: &JsonEmitter) -> Option<DiagnosticCode> {
|
|
|
|
s.map(|s| {
|
|
|
|
|
|
|
|
let explanation = je.registry
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|registry| registry.find_description(&s));
|
|
|
|
|
|
|
|
DiagnosticCode {
|
|
|
|
code: s,
|
|
|
|
explanation: explanation,
|
|
|
|
}
|
|
|
|
})
|
2015-12-30 19:43:42 -06:00
|
|
|
}
|
|
|
|
}
|
2016-04-15 20:23:50 -05:00
|
|
|
|
|
|
|
impl JsonEmitter {
|
|
|
|
fn render(&self, render_span: &RenderSpan) -> Option<String> {
|
2016-06-21 17:08:13 -05:00
|
|
|
use std::borrow::Borrow;
|
|
|
|
|
2016-04-15 20:23:50 -05:00
|
|
|
match *render_span {
|
|
|
|
RenderSpan::FullSpan(_) => {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
RenderSpan::Suggestion(ref suggestion) => {
|
2016-06-21 17:08:13 -05:00
|
|
|
Some(suggestion.splice_lines(self.cm.borrow()))
|
2016-04-15 20:23:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|