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 rustc_span::source_map::{FilePathMapping, SourceMap};
14 use crate::emitter::{Emitter, HumanReadableErrorType};
15 use crate::registry::Registry;
16 use crate::{Applicability, DiagnosticId};
17 use crate::{CodeSuggestion, SubDiagnostic};
19 use rustc_data_structures::sync::Lrc;
20 use rustc_span::{MacroBacktrace, MultiSpan, Span, SpanLabel};
21 use std::io::{self, Write};
23 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();
68 Lrc::new(SourceMap::new(file_path_mapping)),
71 external_macro_backtrace,
76 dst: Box<dyn Write + Send>,
77 registry: Option<Registry>,
78 source_map: Lrc<SourceMap>,
80 json_rendered: HumanReadableErrorType,
81 external_macro_backtrace: bool,
90 external_macro_backtrace,
94 pub fn ui_testing(self, ui_testing: bool) -> Self {
95 Self { ui_testing, ..self }
99 impl Emitter for JsonEmitter {
100 fn emit_diagnostic(&mut self, diag: &crate::Diagnostic) {
101 let data = Diagnostic::from_errors_diagnostic(diag, self);
102 let result = if self.pretty {
103 writeln!(&mut self.dst, "{}", as_pretty_json(&data))
105 writeln!(&mut self.dst, "{}", as_json(&data))
107 if let Err(e) = result {
108 panic!("failed to print diagnostics: {:?}", e);
112 fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {
113 let data = ArtifactNotification { artifact: path, emit: artifact_type };
114 let result = if self.pretty {
115 writeln!(&mut self.dst, "{}", as_pretty_json(&data))
117 writeln!(&mut self.dst, "{}", as_json(&data))
119 if let Err(e) = result {
120 panic!("failed to print notification: {:?}", e);
124 fn source_map(&self) -> Option<&Lrc<SourceMap>> {
128 fn should_show_explain(&self) -> bool {
129 match self.json_rendered {
130 HumanReadableErrorType::Short(_) => false,
136 // The following data types are provided just for serialisation.
138 #[derive(RustcEncodable)]
140 /// The primary error message.
142 code: Option<DiagnosticCode>,
143 /// "error: internal compiler error", "error", "warning", "note", "help".
145 spans: Vec<DiagnosticSpan>,
146 /// Associated diagnostic messages.
147 children: Vec<Diagnostic>,
148 /// The message as rustc would render it.
149 rendered: Option<String>,
152 #[derive(RustcEncodable)]
153 struct DiagnosticSpan {
160 /// 1-based, character offset.
163 /// Is this a "primary" span -- meaning the point, or one of the points,
164 /// where the error occurred?
166 /// Source text from the start of line_start to the end of line_end.
167 text: Vec<DiagnosticSpanLine>,
168 /// Label that should be placed at this location (if any)
169 label: Option<String>,
170 /// If we are suggesting a replacement, this will contain text
171 /// that should be sliced in atop this span.
172 suggested_replacement: Option<String>,
173 /// If the suggestion is approximate
174 suggestion_applicability: Option<Applicability>,
175 /// Macro invocations that created the code at this span, if any.
176 expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
179 #[derive(RustcEncodable)]
180 struct DiagnosticSpanLine {
183 /// 1-based, character offset in self.text.
184 highlight_start: usize,
186 highlight_end: usize,
189 #[derive(RustcEncodable)]
190 struct DiagnosticSpanMacroExpansion {
191 /// span where macro was applied to generate this code; note that
192 /// this may itself derive from a macro (if
193 /// `span.expansion.is_some()`)
194 span: DiagnosticSpan,
196 /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
197 macro_decl_name: String,
199 /// span where macro was defined (if known)
200 def_site_span: DiagnosticSpan,
203 #[derive(RustcEncodable)]
204 struct DiagnosticCode {
207 /// An explanation for the code.
208 explanation: Option<&'static str>,
211 #[derive(RustcEncodable)]
212 struct ArtifactNotification<'a> {
213 /// The path of the artifact.
215 /// What kind of artifact we're emitting.
220 fn from_errors_diagnostic(diag: &crate::Diagnostic, je: &JsonEmitter) -> Diagnostic {
221 let sugg = diag.suggestions.iter().map(|sugg| Diagnostic {
222 message: sugg.msg.clone(),
225 spans: DiagnosticSpan::from_suggestion(sugg, je),
230 // generate regular command line output and store it in the json
232 // A threadsafe buffer for writing.
233 #[derive(Default, Clone)]
234 struct BufWriter(Arc<Mutex<Vec<u8>>>);
236 impl Write for BufWriter {
237 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
238 self.0.lock().unwrap().write(buf)
240 fn flush(&mut self) -> io::Result<()> {
241 self.0.lock().unwrap().flush()
244 let buf = BufWriter::default();
245 let output = buf.clone();
252 je.external_macro_backtrace,
254 .ui_testing(je.ui_testing)
255 .emit_diagnostic(diag);
256 let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
257 let output = String::from_utf8(output).unwrap();
260 message: diag.message(),
261 code: DiagnosticCode::map_opt_string(diag.code.clone(), je),
262 level: diag.level.to_str(),
263 spans: DiagnosticSpan::from_multispan(&diag.span, je),
267 .map(|c| Diagnostic::from_sub_diagnostic(c, je))
270 rendered: Some(output),
274 fn from_sub_diagnostic(diag: &SubDiagnostic, je: &JsonEmitter) -> Diagnostic {
276 message: diag.message(),
278 level: diag.level.to_str(),
282 .map(|sp| DiagnosticSpan::from_multispan(sp, je))
283 .unwrap_or_else(|| DiagnosticSpan::from_multispan(&diag.span, je)),
290 impl DiagnosticSpan {
293 suggestion: Option<(&String, Applicability)>,
295 ) -> DiagnosticSpan {
296 Self::from_span_etc(span.span, span.is_primary, span.label, suggestion, je)
302 label: Option<String>,
303 suggestion: Option<(&String, Applicability)>,
305 ) -> DiagnosticSpan {
306 // obtain the full backtrace from the `macro_backtrace`
307 // helper; in some ways, it'd be better to expand the
308 // backtrace ourselves, but the `macro_backtrace` helper makes
309 // some decision, such as dropping some frames, and I don't
310 // want to duplicate that logic here.
311 let backtrace = span.macro_backtrace().into_iter();
312 DiagnosticSpan::from_span_full(span, is_primary, label, suggestion, backtrace, je)
318 label: Option<String>,
319 suggestion: Option<(&String, Applicability)>,
320 mut backtrace: vec::IntoIter<MacroBacktrace>,
322 ) -> DiagnosticSpan {
323 let start = je.sm.lookup_char_pos(span.lo());
324 let end = je.sm.lookup_char_pos(span.hi());
325 let backtrace_step = backtrace.next().map(|bt| {
326 let call_site = Self::from_span_full(bt.call_site, false, None, None, backtrace, je);
328 Self::from_span_full(bt.def_site_span, false, None, None, vec![].into_iter(), je);
329 Box::new(DiagnosticSpanMacroExpansion {
331 macro_decl_name: bt.macro_decl_name,
337 file_name: start.file.name.to_string(),
338 byte_start: start.file.original_relative_byte_pos(span.lo()).0,
339 byte_end: start.file.original_relative_byte_pos(span.hi()).0,
340 line_start: start.line,
342 column_start: start.col.0 + 1,
343 column_end: end.col.0 + 1,
345 text: DiagnosticSpanLine::from_span(span, je),
346 suggested_replacement: suggestion.map(|x| x.0.clone()),
347 suggestion_applicability: suggestion.map(|x| x.1),
348 expansion: backtrace_step,
353 fn from_multispan(msp: &MultiSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
356 .map(|span_str| Self::from_span_label(span_str, None, je))
360 fn from_suggestion(suggestion: &CodeSuggestion, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
364 .flat_map(|substitution| {
365 substitution.parts.iter().map(move |suggestion_inner| {
367 SpanLabel { span: suggestion_inner.span, is_primary: true, label: None };
368 DiagnosticSpan::from_span_label(
370 Some((&suggestion_inner.snippet, suggestion.applicability)),
379 impl DiagnosticSpanLine {
380 fn line_from_source_file(
381 fm: &rustc_span::SourceFile,
385 ) -> DiagnosticSpanLine {
387 text: fm.get_line(index).map_or(String::new(), |l| l.into_owned()),
388 highlight_start: h_start,
389 highlight_end: h_end,
393 /// Creates a list of DiagnosticSpanLines from span - each line with any part
394 /// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
395 /// `span` within the line.
396 fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
400 let fm = &*lines.file;
405 DiagnosticSpanLine::line_from_source_file(
408 line.start_col.0 + 1,
414 .unwrap_or_else(|_| vec![])
418 impl DiagnosticCode {
419 fn map_opt_string(s: Option<DiagnosticId>, je: &JsonEmitter) -> Option<DiagnosticCode> {
422 DiagnosticId::Error(s) => s,
423 DiagnosticId::Lint(s) => s,
426 je.registry.as_ref().and_then(|registry| registry.find_description(&s));
428 DiagnosticCode { code: s, explanation }