1 //! A JSON emitter for errors.
3 //! This works by converting errors to a simplified structural format (see the
4 //! structs at the start of the file) and then serializing them. These should
5 //! contain as much information about the error as possible.
7 //! The format of the JSON output should be considered *unstable*. For now the
8 //! structs at the end of this file (Diagnostic*) specify the error format.
10 // FIXME: spec the JSON output properly.
12 use syntax_pos::source_map::{SourceMap, FilePathMapping};
14 use crate::registry::Registry;
15 use crate::{SubDiagnostic, CodeSuggestion};
16 use crate::{DiagnosticId, Applicability};
17 use crate::emitter::{Emitter, HumanReadableErrorType};
19 use syntax_pos::{MacroBacktrace, Span, SpanLabel, MultiSpan};
20 use rustc_data_structures::sync::Lrc;
21 use std::io::{self, Write};
24 use std::sync::{Arc, Mutex};
26 use rustc_serialize::json::{as_json, as_pretty_json};
31 pub struct JsonEmitter {
32 dst: Box<dyn Write + Send>,
33 registry: Option<Registry>,
37 json_rendered: HumanReadableErrorType,
38 external_macro_backtrace: bool,
43 registry: Option<Registry>,
44 source_map: Lrc<SourceMap>,
46 json_rendered: HumanReadableErrorType,
47 external_macro_backtrace: bool,
50 dst: Box::new(io::stderr()),
56 external_macro_backtrace,
62 json_rendered: HumanReadableErrorType,
63 external_macro_backtrace: bool,
65 let file_path_mapping = FilePathMapping::empty();
66 JsonEmitter::stderr(None, Lrc::new(SourceMap::new(file_path_mapping)),
67 pretty, json_rendered, external_macro_backtrace)
71 dst: Box<dyn Write + Send>,
72 registry: Option<Registry>,
73 source_map: Lrc<SourceMap>,
75 json_rendered: HumanReadableErrorType,
76 external_macro_backtrace: bool,
85 external_macro_backtrace,
89 pub fn ui_testing(self, ui_testing: bool) -> Self {
90 Self { ui_testing, ..self }
94 impl Emitter for JsonEmitter {
95 fn emit_diagnostic(&mut self, diag: &crate::Diagnostic) {
96 let data = Diagnostic::from_errors_diagnostic(diag, self);
97 let result = if self.pretty {
98 writeln!(&mut self.dst, "{}", as_pretty_json(&data))
100 writeln!(&mut self.dst, "{}", as_json(&data))
102 if let Err(e) = result {
103 panic!("failed to print diagnostics: {:?}", e);
107 fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {
108 let data = ArtifactNotification { artifact: path, emit: artifact_type };
109 let result = if self.pretty {
110 writeln!(&mut self.dst, "{}", as_pretty_json(&data))
112 writeln!(&mut self.dst, "{}", as_json(&data))
114 if let Err(e) = result {
115 panic!("failed to print notification: {:?}", e);
119 fn source_map(&self) -> Option<&Lrc<SourceMap>> {
123 fn should_show_explain(&self) -> bool {
124 match self.json_rendered {
125 HumanReadableErrorType::Short(_) => false,
131 // The following data types are provided just for serialisation.
133 #[derive(RustcEncodable)]
135 /// The primary error message.
137 code: Option<DiagnosticCode>,
138 /// "error: internal compiler error", "error", "warning", "note", "help".
140 spans: Vec<DiagnosticSpan>,
141 /// Associated diagnostic messages.
142 children: Vec<Diagnostic>,
143 /// The message as rustc would render it.
144 rendered: Option<String>,
147 #[derive(RustcEncodable)]
148 struct DiagnosticSpan {
155 /// 1-based, character offset.
158 /// Is this a "primary" span -- meaning the point, or one of the points,
159 /// where the error occurred?
161 /// Source text from the start of line_start to the end of line_end.
162 text: Vec<DiagnosticSpanLine>,
163 /// Label that should be placed at this location (if any)
164 label: Option<String>,
165 /// If we are suggesting a replacement, this will contain text
166 /// that should be sliced in atop this span.
167 suggested_replacement: Option<String>,
168 /// If the suggestion is approximate
169 suggestion_applicability: Option<Applicability>,
170 /// Macro invocations that created the code at this span, if any.
171 expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
174 #[derive(RustcEncodable)]
175 struct DiagnosticSpanLine {
178 /// 1-based, character offset in self.text.
179 highlight_start: usize,
181 highlight_end: usize,
184 #[derive(RustcEncodable)]
185 struct DiagnosticSpanMacroExpansion {
186 /// span where macro was applied to generate this code; note that
187 /// this may itself derive from a macro (if
188 /// `span.expansion.is_some()`)
189 span: DiagnosticSpan,
191 /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
192 macro_decl_name: String,
194 /// span where macro was defined (if known)
195 def_site_span: DiagnosticSpan,
198 #[derive(RustcEncodable)]
199 struct DiagnosticCode {
202 /// An explanation for the code.
203 explanation: Option<&'static str>,
206 #[derive(RustcEncodable)]
207 struct ArtifactNotification<'a> {
208 /// The path of the artifact.
210 /// What kind of artifact we're emitting.
215 fn from_errors_diagnostic(diag: &crate::Diagnostic,
218 let sugg = diag.suggestions.iter().map(|sugg| {
220 message: sugg.msg.clone(),
223 spans: DiagnosticSpan::from_suggestion(sugg, je),
229 // generate regular command line output and store it in the json
231 // A threadsafe buffer for writing.
232 #[derive(Default, Clone)]
233 struct BufWriter(Arc<Mutex<Vec<u8>>>);
235 impl Write for BufWriter {
236 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
237 self.0.lock().unwrap().write(buf)
239 fn flush(&mut self) -> io::Result<()> {
240 self.0.lock().unwrap().flush()
243 let buf = BufWriter::default();
244 let output = buf.clone();
245 je.json_rendered.new_emitter(
246 Box::new(buf), Some(je.sm.clone()), false, None, je.external_macro_backtrace
247 ).ui_testing(je.ui_testing).emit_diagnostic(diag);
248 let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
249 let output = String::from_utf8(output).unwrap();
252 message: diag.message(),
253 code: DiagnosticCode::map_opt_string(diag.code.clone(), je),
254 level: diag.level.to_str(),
255 spans: DiagnosticSpan::from_multispan(&diag.span, je),
256 children: diag.children.iter().map(|c| {
257 Diagnostic::from_sub_diagnostic(c, je)
258 }).chain(sugg).collect(),
259 rendered: Some(output),
263 fn from_sub_diagnostic(diag: &SubDiagnostic, je: &JsonEmitter) -> Diagnostic {
265 message: diag.message(),
267 level: diag.level.to_str(),
268 spans: diag.render_span.as_ref()
269 .map(|sp| DiagnosticSpan::from_multispan(sp, je))
270 .unwrap_or_else(|| DiagnosticSpan::from_multispan(&diag.span, je)),
277 impl DiagnosticSpan {
278 fn from_span_label(span: SpanLabel,
279 suggestion: Option<(&String, Applicability)>,
282 Self::from_span_etc(span.span,
289 fn from_span_etc(span: Span,
291 label: Option<String>,
292 suggestion: Option<(&String, Applicability)>,
295 // obtain the full backtrace from the `macro_backtrace`
296 // helper; in some ways, it'd be better to expand the
297 // backtrace ourselves, but the `macro_backtrace` helper makes
298 // some decision, such as dropping some frames, and I don't
299 // want to duplicate that logic here.
300 let backtrace = span.macro_backtrace().into_iter();
301 DiagnosticSpan::from_span_full(span,
309 fn from_span_full(span: Span,
311 label: Option<String>,
312 suggestion: Option<(&String, Applicability)>,
313 mut backtrace: vec::IntoIter<MacroBacktrace>,
316 let start = je.sm.lookup_char_pos(span.lo());
317 let end = je.sm.lookup_char_pos(span.hi());
318 let backtrace_step = backtrace.next().map(|bt| {
320 Self::from_span_full(bt.call_site,
327 Self::from_span_full(bt.def_site_span,
333 Box::new(DiagnosticSpanMacroExpansion {
335 macro_decl_name: bt.macro_decl_name,
341 file_name: start.file.name.to_string(),
342 byte_start: start.file.original_relative_byte_pos(span.lo()).0,
343 byte_end: start.file.original_relative_byte_pos(span.hi()).0,
344 line_start: start.line,
346 column_start: start.col.0 + 1,
347 column_end: end.col.0 + 1,
349 text: DiagnosticSpanLine::from_span(span, je),
350 suggested_replacement: suggestion.map(|x| x.0.clone()),
351 suggestion_applicability: suggestion.map(|x| x.1),
352 expansion: backtrace_step,
357 fn from_multispan(msp: &MultiSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
360 .map(|span_str| Self::from_span_label(span_str, None, je))
364 fn from_suggestion(suggestion: &CodeSuggestion, je: &JsonEmitter)
365 -> Vec<DiagnosticSpan> {
366 suggestion.substitutions
368 .flat_map(|substitution| {
369 substitution.parts.iter().map(move |suggestion_inner| {
370 let span_label = SpanLabel {
371 span: suggestion_inner.span,
375 DiagnosticSpan::from_span_label(span_label,
376 Some((&suggestion_inner.snippet,
377 suggestion.applicability)),
385 impl DiagnosticSpanLine {
386 fn line_from_source_file(fm: &syntax_pos::SourceFile,
390 -> DiagnosticSpanLine {
392 text: fm.get_line(index).map_or(String::new(), |l| l.into_owned()),
393 highlight_start: h_start,
394 highlight_end: h_end,
398 /// Creates a list of DiagnosticSpanLines from span - each line with any part
399 /// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
400 /// `span` within the line.
401 fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
402 je.sm.span_to_lines(span)
404 let fm = &*lines.file;
407 .map(|line| DiagnosticSpanLine::line_from_source_file(
410 line.start_col.0 + 1,
413 }).unwrap_or_else(|_| vec![])
417 impl DiagnosticCode {
418 fn map_opt_string(s: Option<DiagnosticId>, je: &JsonEmitter) -> Option<DiagnosticCode> {
421 DiagnosticId::Error(s) => s,
422 DiagnosticId::Lint(s) => s,
424 let explanation = je.registry
426 .and_then(|registry| registry.find_description(&s));