2015-12-31 01:43:42 +00:00
|
|
|
//! A JSON emitter for errors.
|
2015-12-31 05:47:14 +00:00
|
|
|
//!
|
|
|
|
//! This works by converting errors to a simplified structural format (see the
|
2017-08-15 19:45:21 +00:00
|
|
|
//! structs at the start of the file) and then serializing them. These should
|
2015-12-31 05:47:14 +00:00
|
|
|
//! 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.
|
|
|
|
|
2019-01-11 16:32:31 +00:00
|
|
|
// FIXME: spec the JSON output properly.
|
2015-12-31 05:47:14 +00:00
|
|
|
|
2019-02-06 17:33:01 +00:00
|
|
|
use crate::source_map::{SourceMap, FilePathMapping};
|
2015-12-31 01:43:42 +00:00
|
|
|
|
2019-02-07 15:56:05 +00:00
|
|
|
use errors::registry::Registry;
|
2019-11-15 13:32:31 +00:00
|
|
|
use errors::{SubDiagnostic, CodeSuggestion};
|
2019-02-07 15:56:05 +00:00
|
|
|
use errors::{DiagnosticId, Applicability};
|
2019-03-25 10:16:58 +00:00
|
|
|
use errors::emitter::{Emitter, HumanReadableErrorType};
|
2015-12-31 01:43:42 +00:00
|
|
|
|
2019-02-09 02:24:02 +00:00
|
|
|
use syntax_pos::{MacroBacktrace, Span, SpanLabel, MultiSpan};
|
2018-03-03 05:26:02 +00:00
|
|
|
use rustc_data_structures::sync::{self, Lrc};
|
2015-12-31 05:47:14 +00:00
|
|
|
use std::io::{self, Write};
|
2019-05-02 02:06:33 +00:00
|
|
|
use std::path::Path;
|
2016-04-16 01:23:50 +00:00
|
|
|
use std::vec;
|
2017-11-17 07:55:22 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2015-12-31 05:47:14 +00:00
|
|
|
|
2017-11-03 12:38:26 +00:00
|
|
|
use rustc_serialize::json::{as_json, as_pretty_json};
|
2015-12-31 01:43:42 +00:00
|
|
|
|
2019-10-03 00:55:31 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2015-12-31 01:43:42 +00:00
|
|
|
pub struct JsonEmitter {
|
2018-07-10 19:06:26 +00:00
|
|
|
dst: Box<dyn Write + Send>,
|
2015-12-31 05:47:14 +00:00
|
|
|
registry: Option<Registry>,
|
2019-11-15 13:32:31 +00:00
|
|
|
sm: Lrc<SourceMap>,
|
2017-11-03 12:38:26 +00:00
|
|
|
pretty: bool,
|
2018-02-23 00:18:53 +00:00
|
|
|
ui_testing: bool,
|
2019-03-25 10:16:58 +00:00
|
|
|
json_rendered: HumanReadableErrorType,
|
2019-09-07 13:57:11 +00:00
|
|
|
external_macro_backtrace: bool,
|
2015-12-31 01:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl JsonEmitter {
|
2019-03-12 12:06:43 +00:00
|
|
|
pub fn stderr(
|
|
|
|
registry: Option<Registry>,
|
|
|
|
source_map: Lrc<SourceMap>,
|
|
|
|
pretty: bool,
|
2019-03-25 10:16:58 +00:00
|
|
|
json_rendered: HumanReadableErrorType,
|
2019-09-07 13:57:11 +00:00
|
|
|
external_macro_backtrace: bool,
|
2019-03-12 12:06:43 +00:00
|
|
|
) -> JsonEmitter {
|
2016-09-26 22:45:50 +00:00
|
|
|
JsonEmitter {
|
|
|
|
dst: Box::new(io::stderr()),
|
2017-08-07 05:54:09 +00:00
|
|
|
registry,
|
2018-10-29 20:26:13 +00:00
|
|
|
sm: source_map,
|
2017-11-03 12:38:26 +00:00
|
|
|
pretty,
|
2018-02-23 00:18:53 +00:00
|
|
|
ui_testing: false,
|
2019-03-25 10:16:58 +00:00
|
|
|
json_rendered,
|
2019-09-07 13:57:11 +00:00
|
|
|
external_macro_backtrace,
|
2016-09-26 22:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 13:57:11 +00:00
|
|
|
pub fn basic(
|
|
|
|
pretty: bool,
|
|
|
|
json_rendered: HumanReadableErrorType,
|
|
|
|
external_macro_backtrace: bool,
|
|
|
|
) -> JsonEmitter {
|
2017-04-24 17:01:19 +00:00
|
|
|
let file_path_mapping = FilePathMapping::empty();
|
2018-08-18 10:13:35 +00:00
|
|
|
JsonEmitter::stderr(None, Lrc::new(SourceMap::new(file_path_mapping)),
|
2019-09-07 13:57:11 +00:00
|
|
|
pretty, json_rendered, external_macro_backtrace)
|
2015-12-31 01:43:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 12:06:43 +00:00
|
|
|
pub fn new(
|
|
|
|
dst: Box<dyn Write + Send>,
|
|
|
|
registry: Option<Registry>,
|
|
|
|
source_map: Lrc<SourceMap>,
|
|
|
|
pretty: bool,
|
2019-03-25 10:16:58 +00:00
|
|
|
json_rendered: HumanReadableErrorType,
|
2019-09-07 13:57:11 +00:00
|
|
|
external_macro_backtrace: bool,
|
2019-03-12 12:06:43 +00:00
|
|
|
) -> JsonEmitter {
|
2015-12-31 01:43:42 +00:00
|
|
|
JsonEmitter {
|
2017-08-07 05:54:09 +00:00
|
|
|
dst,
|
|
|
|
registry,
|
2018-10-29 20:26:13 +00:00
|
|
|
sm: source_map,
|
2017-11-03 12:38:26 +00:00
|
|
|
pretty,
|
2018-02-23 00:18:53 +00:00
|
|
|
ui_testing: false,
|
2019-03-25 10:16:58 +00:00
|
|
|
json_rendered,
|
2019-09-07 13:57:11 +00:00
|
|
|
external_macro_backtrace,
|
2015-12-31 01:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-23 00:18:53 +00:00
|
|
|
|
|
|
|
pub fn ui_testing(self, ui_testing: bool) -> Self {
|
|
|
|
Self { ui_testing, ..self }
|
|
|
|
}
|
2015-12-31 01:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Emitter for JsonEmitter {
|
2019-10-15 06:12:55 +00:00
|
|
|
fn emit_diagnostic(&mut self, diag: &errors::Diagnostic) {
|
|
|
|
let data = Diagnostic::from_errors_diagnostic(diag, self);
|
2017-11-03 12:38:26 +00:00
|
|
|
let result = if self.pretty {
|
|
|
|
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
|
|
|
|
} else {
|
|
|
|
writeln!(&mut self.dst, "{}", as_json(&data))
|
|
|
|
};
|
|
|
|
if let Err(e) = result {
|
2015-12-31 05:47:14 +00:00
|
|
|
panic!("failed to print diagnostics: {:?}", e);
|
|
|
|
}
|
|
|
|
}
|
2019-04-14 22:26:08 +00:00
|
|
|
|
2019-05-21 19:38:46 +00:00
|
|
|
fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {
|
|
|
|
let data = ArtifactNotification { artifact: path, emit: artifact_type };
|
2019-04-14 22:26:08 +00:00
|
|
|
let result = if self.pretty {
|
|
|
|
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
|
|
|
|
} else {
|
|
|
|
writeln!(&mut self.dst, "{}", as_json(&data))
|
|
|
|
};
|
|
|
|
if let Err(e) = result {
|
2019-05-02 02:06:33 +00:00
|
|
|
panic!("failed to print notification: {:?}", e);
|
2019-04-14 22:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 23:53:33 +00:00
|
|
|
|
2019-11-15 13:32:31 +00:00
|
|
|
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
|
2019-10-14 04:48:39 +00:00
|
|
|
Some(&self.sm)
|
|
|
|
}
|
|
|
|
|
2019-09-24 23:53:33 +00:00
|
|
|
fn should_show_explain(&self) -> bool {
|
|
|
|
match self.json_rendered {
|
|
|
|
HumanReadableErrorType::Short(_) => false,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The following data types are provided just for serialisation.
|
|
|
|
|
|
|
|
#[derive(RustcEncodable)]
|
2017-01-11 21:55:41 +00:00
|
|
|
struct Diagnostic {
|
2015-12-31 05:47:14 +00:00
|
|
|
/// The primary error message.
|
2017-01-11 21:55:41 +00:00
|
|
|
message: String,
|
2015-12-31 05:47:14 +00:00
|
|
|
code: Option<DiagnosticCode>,
|
|
|
|
/// "error: internal compiler error", "error", "warning", "note", "help".
|
|
|
|
level: &'static str,
|
2015-12-13 12:12:47 +00:00
|
|
|
spans: Vec<DiagnosticSpan>,
|
2016-04-16 01:23:50 +00:00
|
|
|
/// Associated diagnostic messages.
|
2017-01-11 21:55:41 +00:00
|
|
|
children: Vec<Diagnostic>,
|
2017-11-17 07:55:22 +00:00
|
|
|
/// The message as rustc would render it.
|
2016-04-16 01:23:50 +00:00
|
|
|
rendered: Option<String>,
|
2015-12-31 05:47:14 +00: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 18:57:20 +00:00
|
|
|
/// Is this a "primary" span -- meaning the point, or one of the points,
|
|
|
|
/// where the error occurred?
|
|
|
|
is_primary: bool,
|
2016-03-24 02:32:42 +00:00
|
|
|
/// Source text from the start of line_start to the end of line_end.
|
|
|
|
text: Vec<DiagnosticSpanLine>,
|
2016-04-20 18:57:20 +00:00
|
|
|
/// Label that should be placed at this location (if any)
|
|
|
|
label: Option<String>,
|
2016-04-16 01:23:50 +00:00
|
|
|
/// If we are suggesting a replacement, this will contain text
|
2017-11-03 11:37:11 +00:00
|
|
|
/// that should be sliced in atop this span.
|
2016-04-16 01:23:50 +00:00
|
|
|
suggested_replacement: Option<String>,
|
2018-01-18 11:47:46 +00:00
|
|
|
/// If the suggestion is approximate
|
2018-04-25 21:51:06 +00:00
|
|
|
suggestion_applicability: Option<Applicability>,
|
2016-04-16 01:23:50 +00:00
|
|
|
/// Macro invocations that created the code at this span, if any.
|
|
|
|
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
|
2016-03-24 02:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(RustcEncodable)]
|
|
|
|
struct DiagnosticSpanLine {
|
|
|
|
text: String,
|
2016-04-16 01:23:50 +00:00
|
|
|
|
2016-03-24 02:32:42 +00:00
|
|
|
/// 1-based, character offset in self.text.
|
|
|
|
highlight_start: usize,
|
2016-04-16 01:23:50 +00:00
|
|
|
|
2016-03-24 02:32:42 +00:00
|
|
|
highlight_end: usize,
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
2015-12-31 01:43:42 +00:00
|
|
|
|
2016-04-16 01:23:50 +00: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)
|
2019-06-30 00:05:52 +00:00
|
|
|
def_site_span: DiagnosticSpan,
|
2016-04-16 01:23:50 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 05:47:14 +00:00
|
|
|
#[derive(RustcEncodable)]
|
|
|
|
struct DiagnosticCode {
|
|
|
|
/// The code itself.
|
|
|
|
code: String,
|
|
|
|
/// An explanation for the code.
|
|
|
|
explanation: Option<&'static str>,
|
|
|
|
}
|
|
|
|
|
2019-04-14 22:26:08 +00:00
|
|
|
#[derive(RustcEncodable)]
|
2019-05-02 02:06:33 +00:00
|
|
|
struct ArtifactNotification<'a> {
|
|
|
|
/// The path of the artifact.
|
|
|
|
artifact: &'a Path,
|
2019-05-21 19:38:46 +00:00
|
|
|
/// What kind of artifact we're emitting.
|
|
|
|
emit: &'a str,
|
2019-04-14 22:26:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 21:55:41 +00:00
|
|
|
impl Diagnostic {
|
2019-10-15 06:12:55 +00:00
|
|
|
fn from_errors_diagnostic(diag: &errors::Diagnostic,
|
2017-01-11 21:55:41 +00:00
|
|
|
je: &JsonEmitter)
|
|
|
|
-> Diagnostic {
|
2019-10-15 06:12:55 +00:00
|
|
|
let sugg = diag.suggestions.iter().map(|sugg| {
|
2017-10-24 14:41:16 +00:00
|
|
|
Diagnostic {
|
|
|
|
message: sugg.msg.clone(),
|
|
|
|
code: None,
|
|
|
|
level: "help",
|
|
|
|
spans: DiagnosticSpan::from_suggestion(sugg, je),
|
|
|
|
children: vec![],
|
|
|
|
rendered: None,
|
|
|
|
}
|
2017-03-24 16:31:41 +00:00
|
|
|
});
|
2017-11-17 07:55:22 +00:00
|
|
|
|
|
|
|
// generate regular command line output and store it in the json
|
|
|
|
|
|
|
|
// A threadsafe buffer for writing.
|
|
|
|
#[derive(Default, Clone)]
|
|
|
|
struct BufWriter(Arc<Mutex<Vec<u8>>>);
|
|
|
|
|
|
|
|
impl Write for BufWriter {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.0.lock().unwrap().write(buf)
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.0.lock().unwrap().flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let buf = BufWriter::default();
|
|
|
|
let output = buf.clone();
|
2019-09-07 13:57:11 +00:00
|
|
|
je.json_rendered.new_emitter(
|
|
|
|
Box::new(buf), Some(je.sm.clone()), false, None, je.external_macro_backtrace
|
2019-10-15 06:12:55 +00:00
|
|
|
).ui_testing(je.ui_testing).emit_diagnostic(diag);
|
2017-11-17 07:55:22 +00:00
|
|
|
let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
|
|
|
|
let output = String::from_utf8(output).unwrap();
|
|
|
|
|
2015-12-31 05:47:14 +00:00
|
|
|
Diagnostic {
|
2019-10-15 06:12:55 +00:00
|
|
|
message: diag.message(),
|
|
|
|
code: DiagnosticCode::map_opt_string(diag.code.clone(), je),
|
|
|
|
level: diag.level.to_str(),
|
|
|
|
spans: DiagnosticSpan::from_multispan(&diag.span, je),
|
|
|
|
children: diag.children.iter().map(|c| {
|
2015-12-31 05:47:14 +00:00
|
|
|
Diagnostic::from_sub_diagnostic(c, je)
|
2017-05-09 08:04:24 +00:00
|
|
|
}).chain(sugg).collect(),
|
2017-11-20 08:40:55 +00:00
|
|
|
rendered: Some(output),
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
2015-12-31 01:43:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 06:12:55 +00:00
|
|
|
fn from_sub_diagnostic(diag: &SubDiagnostic, je: &JsonEmitter) -> Diagnostic {
|
2015-12-31 05:47:14 +00:00
|
|
|
Diagnostic {
|
2019-10-15 06:12:55 +00:00
|
|
|
message: diag.message(),
|
2015-12-31 05:47:14 +00:00
|
|
|
code: None,
|
2019-10-15 06:12:55 +00:00
|
|
|
level: diag.level.to_str(),
|
|
|
|
spans: diag.render_span.as_ref()
|
2017-11-16 15:36:49 +00:00
|
|
|
.map(|sp| DiagnosticSpan::from_multispan(sp, je))
|
2019-10-15 06:12:55 +00:00
|
|
|
.unwrap_or_else(|| DiagnosticSpan::from_multispan(&diag.span, je)),
|
2015-12-31 05:47:14 +00:00
|
|
|
children: vec![],
|
2017-05-09 08:04:24 +00:00
|
|
|
rendered: None,
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticSpan {
|
2016-04-20 18:57:20 +00:00
|
|
|
fn from_span_label(span: SpanLabel,
|
2018-04-25 21:51:06 +00:00
|
|
|
suggestion: Option<(&String, Applicability)>,
|
2016-04-20 18:57:20 +00:00
|
|
|
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>,
|
2018-04-25 21:51:06 +00:00
|
|
|
suggestion: Option<(&String, Applicability)>,
|
2016-04-20 18:57:20 +00:00
|
|
|
je: &JsonEmitter)
|
|
|
|
-> DiagnosticSpan {
|
2016-04-16 01:23:50 +00: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.
|
2017-03-17 04:04:41 +00:00
|
|
|
let backtrace = span.macro_backtrace().into_iter();
|
2016-04-20 18:57:20 +00:00
|
|
|
DiagnosticSpan::from_span_full(span,
|
|
|
|
is_primary,
|
|
|
|
label,
|
|
|
|
suggestion,
|
|
|
|
backtrace,
|
|
|
|
je)
|
2016-04-16 01:23:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 18:57:20 +00:00
|
|
|
fn from_span_full(span: Span,
|
|
|
|
is_primary: bool,
|
|
|
|
label: Option<String>,
|
2018-04-25 21:51:06 +00:00
|
|
|
suggestion: Option<(&String, Applicability)>,
|
2016-04-20 18:57:20 +00:00
|
|
|
mut backtrace: vec::IntoIter<MacroBacktrace>,
|
|
|
|
je: &JsonEmitter)
|
|
|
|
-> DiagnosticSpan {
|
2018-10-29 20:26:13 +00:00
|
|
|
let start = je.sm.lookup_char_pos(span.lo());
|
|
|
|
let end = je.sm.lookup_char_pos(span.hi());
|
2016-04-20 18:57:20 +00:00
|
|
|
let backtrace_step = backtrace.next().map(|bt| {
|
|
|
|
let call_site =
|
|
|
|
Self::from_span_full(bt.call_site,
|
|
|
|
false,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
backtrace,
|
|
|
|
je);
|
2019-06-30 00:05:52 +00:00
|
|
|
let def_site_span =
|
|
|
|
Self::from_span_full(bt.def_site_span,
|
2016-04-20 18:57:20 +00:00
|
|
|
false,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
vec![].into_iter(),
|
2019-06-30 00:05:52 +00:00
|
|
|
je);
|
2016-04-20 18:57:20 +00:00
|
|
|
Box::new(DiagnosticSpanMacroExpansion {
|
|
|
|
span: call_site,
|
|
|
|
macro_decl_name: bt.macro_decl_name,
|
2017-08-07 05:54:09 +00:00
|
|
|
def_site_span,
|
2016-04-20 18:57:20 +00:00
|
|
|
})
|
|
|
|
});
|
2018-01-19 07:34:14 +00:00
|
|
|
|
2016-04-16 01:23:50 +00:00
|
|
|
DiagnosticSpan {
|
2017-12-14 07:09:19 +00:00
|
|
|
file_name: start.file.name.to_string(),
|
2019-10-03 00:55:31 +00:00
|
|
|
byte_start: start.file.original_relative_byte_pos(span.lo()).0,
|
|
|
|
byte_end: start.file.original_relative_byte_pos(span.hi()).0,
|
2016-04-16 01:23:50 +00:00
|
|
|
line_start: start.line,
|
|
|
|
line_end: end.line,
|
|
|
|
column_start: start.col.0 + 1,
|
|
|
|
column_end: end.col.0 + 1,
|
2017-08-07 05:54:09 +00:00
|
|
|
is_primary,
|
2016-04-16 01:23:50 +00:00
|
|
|
text: DiagnosticSpanLine::from_span(span, je),
|
2018-01-18 11:47:46 +00:00
|
|
|
suggested_replacement: suggestion.map(|x| x.0.clone()),
|
2018-05-06 20:30:06 +00:00
|
|
|
suggestion_applicability: suggestion.map(|x| x.1),
|
2016-04-16 01:23:50 +00:00
|
|
|
expansion: backtrace_step,
|
2017-08-07 05:54:09 +00:00
|
|
|
label,
|
2016-04-16 01:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 12:12:47 +00:00
|
|
|
fn from_multispan(msp: &MultiSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
|
2016-04-20 18:57:20 +00:00
|
|
|
msp.span_labels()
|
|
|
|
.into_iter()
|
|
|
|
.map(|span_str| Self::from_span_label(span_str, None, je))
|
|
|
|
.collect()
|
2016-04-16 01:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_suggestion(suggestion: &CodeSuggestion, je: &JsonEmitter)
|
|
|
|
-> Vec<DiagnosticSpan> {
|
2017-11-03 15:17:33 +00:00
|
|
|
suggestion.substitutions
|
2017-05-09 08:04:24 +00:00
|
|
|
.iter()
|
2017-05-11 13:26:22 +00:00
|
|
|
.flat_map(|substitution| {
|
2018-01-18 11:47:46 +00:00
|
|
|
substitution.parts.iter().map(move |suggestion_inner| {
|
2017-05-09 08:04:24 +00:00
|
|
|
let span_label = SpanLabel {
|
2018-01-18 11:47:46 +00:00
|
|
|
span: suggestion_inner.span,
|
2017-05-09 08:04:24 +00:00
|
|
|
is_primary: true,
|
|
|
|
label: None,
|
|
|
|
};
|
|
|
|
DiagnosticSpan::from_span_label(span_label,
|
2018-01-18 11:47:46 +00:00
|
|
|
Some((&suggestion_inner.snippet,
|
2018-04-25 21:51:06 +00:00
|
|
|
suggestion.applicability)),
|
2017-05-09 08:04:24 +00:00
|
|
|
je)
|
|
|
|
})
|
2016-04-20 18:57:20 +00:00
|
|
|
})
|
|
|
|
.collect()
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
2016-04-03 22:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticSpanLine {
|
2018-08-18 10:13:56 +00:00
|
|
|
fn line_from_source_file(fm: &syntax_pos::SourceFile,
|
2016-04-03 22:32:37 +00:00
|
|
|
index: usize,
|
|
|
|
h_start: usize,
|
|
|
|
h_end: usize)
|
|
|
|
-> DiagnosticSpanLine {
|
|
|
|
DiagnosticSpanLine {
|
2017-06-11 11:31:40 +00:00
|
|
|
text: fm.get_line(index).map_or(String::new(), |l| l.into_owned()),
|
2016-04-03 22:32:37 +00:00
|
|
|
highlight_start: h_start,
|
|
|
|
highlight_end: h_end,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Creates a list of DiagnosticSpanLines from span - each line with any part
|
2016-04-03 22:32:37 +00:00
|
|
|
/// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
|
|
|
|
/// `span` within the line.
|
2016-04-16 01:23:50 +00:00
|
|
|
fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
|
2018-10-29 20:26:13 +00:00
|
|
|
je.sm.span_to_lines(span)
|
2019-04-10 23:40:12 +00:00
|
|
|
.map(|lines| {
|
|
|
|
let fm = &*lines.file;
|
|
|
|
lines.lines
|
|
|
|
.iter()
|
|
|
|
.map(|line| DiagnosticSpanLine::line_from_source_file(
|
|
|
|
fm,
|
|
|
|
line.line_index,
|
|
|
|
line.start_col.0 + 1,
|
|
|
|
line.end_col.0 + 1,
|
|
|
|
)).collect()
|
|
|
|
}).unwrap_or_else(|_| vec![])
|
2016-04-03 22:32:37 +00:00
|
|
|
}
|
2016-03-24 02:32:42 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 05:47:14 +00:00
|
|
|
impl DiagnosticCode {
|
2017-10-27 06:21:22 +00:00
|
|
|
fn map_opt_string(s: Option<DiagnosticId>, je: &JsonEmitter) -> Option<DiagnosticCode> {
|
2015-12-31 05:47:14 +00:00
|
|
|
s.map(|s| {
|
2017-10-27 06:21:22 +00:00
|
|
|
let s = match s {
|
|
|
|
DiagnosticId::Error(s) => s,
|
|
|
|
DiagnosticId::Lint(s) => s,
|
|
|
|
};
|
2015-12-31 05:47:14 +00:00
|
|
|
let explanation = je.registry
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|registry| registry.find_description(&s));
|
|
|
|
|
|
|
|
DiagnosticCode {
|
|
|
|
code: s,
|
2017-08-07 05:54:09 +00:00
|
|
|
explanation,
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
|
|
|
})
|
2015-12-31 01:43:42 +00:00
|
|
|
}
|
|
|
|
}
|