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::translation::{to_fluent_args, Translate};
17 use crate::DiagnosticId;
19 CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel, SubDiagnostic,
21 use rustc_lint_defs::Applicability;
23 use rustc_data_structures::sync::Lrc;
24 use rustc_error_messages::FluentArgs;
25 use rustc_span::hygiene::ExpnData;
27 use std::error::Report;
28 use std::io::{self, Write};
30 use std::sync::{Arc, Mutex};
38 pub struct JsonEmitter {
39 dst: Box<dyn Write + Send>,
40 registry: Option<Registry>,
42 fluent_bundle: Option<Lrc<FluentBundle>>,
43 fallback_bundle: LazyFallbackBundle,
46 json_rendered: HumanReadableErrorType,
47 diagnostic_width: Option<usize>,
48 macro_backtrace: bool,
49 track_diagnostics: bool,
54 registry: Option<Registry>,
55 source_map: Lrc<SourceMap>,
56 fluent_bundle: Option<Lrc<FluentBundle>>,
57 fallback_bundle: LazyFallbackBundle,
59 json_rendered: HumanReadableErrorType,
60 diagnostic_width: Option<usize>,
61 macro_backtrace: bool,
62 track_diagnostics: bool,
65 dst: Box::new(io::BufWriter::new(io::stderr())),
81 json_rendered: HumanReadableErrorType,
82 fluent_bundle: Option<Lrc<FluentBundle>>,
83 fallback_bundle: LazyFallbackBundle,
84 diagnostic_width: Option<usize>,
85 macro_backtrace: bool,
86 track_diagnostics: bool,
88 let file_path_mapping = FilePathMapping::empty();
91 Lrc::new(SourceMap::new(file_path_mapping)),
103 dst: Box<dyn Write + Send>,
104 registry: Option<Registry>,
105 source_map: Lrc<SourceMap>,
106 fluent_bundle: Option<Lrc<FluentBundle>>,
107 fallback_bundle: LazyFallbackBundle,
109 json_rendered: HumanReadableErrorType,
110 diagnostic_width: Option<usize>,
111 macro_backtrace: bool,
112 track_diagnostics: bool,
129 pub fn ui_testing(self, ui_testing: bool) -> Self {
130 Self { ui_testing, ..self }
134 impl Translate for JsonEmitter {
135 fn fluent_bundle(&self) -> Option<&Lrc<FluentBundle>> {
136 self.fluent_bundle.as_ref()
139 fn fallback_fluent_bundle(&self) -> &FluentBundle {
140 &self.fallback_bundle
144 impl Emitter for JsonEmitter {
145 fn emit_diagnostic(&mut self, diag: &crate::Diagnostic) {
146 let data = Diagnostic::from_errors_diagnostic(diag, self);
147 let result = if self.pretty {
148 writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap())
150 writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap())
152 .and_then(|_| self.dst.flush());
153 if let Err(e) = result {
154 panic!("failed to print diagnostics: {:?}", e);
158 fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {
159 let data = ArtifactNotification { artifact: path, emit: artifact_type };
160 let result = if self.pretty {
161 writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap())
163 writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap())
165 .and_then(|_| self.dst.flush());
166 if let Err(e) = result {
167 panic!("failed to print notification: {:?}", e);
171 fn emit_future_breakage_report(&mut self, diags: Vec<crate::Diagnostic>) {
172 let data: Vec<FutureBreakageItem> = diags
175 if diag.level == crate::Level::Allow {
176 diag.level = crate::Level::Warning(None);
178 FutureBreakageItem { diagnostic: Diagnostic::from_errors_diagnostic(&diag, self) }
181 let report = FutureIncompatReport { future_incompat_report: data };
182 let result = if self.pretty {
183 writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&report).unwrap())
185 writeln!(&mut self.dst, "{}", serde_json::to_string(&report).unwrap())
187 .and_then(|_| self.dst.flush());
188 if let Err(e) = result {
189 panic!("failed to print future breakage report: {:?}", e);
193 fn emit_unused_externs(&mut self, lint_level: rustc_lint_defs::Level, unused_externs: &[&str]) {
194 let lint_level = lint_level.as_str();
195 let data = UnusedExterns { lint_level, unused_extern_names: unused_externs };
196 let result = if self.pretty {
197 writeln!(&mut self.dst, "{}", serde_json::to_string_pretty(&data).unwrap())
199 writeln!(&mut self.dst, "{}", serde_json::to_string(&data).unwrap())
201 .and_then(|_| self.dst.flush());
202 if let Err(e) = result {
203 panic!("failed to print unused externs: {:?}", e);
207 fn source_map(&self) -> Option<&Lrc<SourceMap>> {
211 fn should_show_explain(&self) -> bool {
212 !matches!(self.json_rendered, HumanReadableErrorType::Short(_))
216 // The following data types are provided just for serialisation.
220 /// The primary error message.
222 code: Option<DiagnosticCode>,
223 /// "error: internal compiler error", "error", "warning", "note", "help".
225 spans: Vec<DiagnosticSpan>,
226 /// Associated diagnostic messages.
227 children: Vec<Diagnostic>,
228 /// The message as rustc would render it.
229 rendered: Option<String>,
233 struct DiagnosticSpan {
240 /// 1-based, character offset.
243 /// Is this a "primary" span -- meaning the point, or one of the points,
244 /// where the error occurred?
246 /// Source text from the start of line_start to the end of line_end.
247 text: Vec<DiagnosticSpanLine>,
248 /// Label that should be placed at this location (if any)
249 label: Option<String>,
250 /// If we are suggesting a replacement, this will contain text
251 /// that should be sliced in atop this span.
252 suggested_replacement: Option<String>,
253 /// If the suggestion is approximate
254 suggestion_applicability: Option<Applicability>,
255 /// Macro invocations that created the code at this span, if any.
256 expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
260 struct DiagnosticSpanLine {
263 /// 1-based, character offset in self.text.
264 highlight_start: usize,
266 highlight_end: usize,
270 struct DiagnosticSpanMacroExpansion {
271 /// span where macro was applied to generate this code; note that
272 /// this may itself derive from a macro (if
273 /// `span.expansion.is_some()`)
274 span: DiagnosticSpan,
276 /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
277 macro_decl_name: String,
279 /// span where macro was defined (if known)
280 def_site_span: DiagnosticSpan,
284 struct DiagnosticCode {
287 /// An explanation for the code.
288 explanation: Option<&'static str>,
292 struct ArtifactNotification<'a> {
293 /// The path of the artifact.
295 /// What kind of artifact we're emitting.
300 struct FutureBreakageItem {
301 diagnostic: Diagnostic,
305 struct FutureIncompatReport {
306 future_incompat_report: Vec<FutureBreakageItem>,
309 // NOTE: Keep this in sync with the equivalent structs in rustdoc's
310 // doctest component (as well as cargo).
311 // We could unify this struct the one in rustdoc but they have different
312 // ownership semantics, so doing so would create wasteful allocations.
314 struct UnusedExterns<'a, 'b, 'c> {
315 /// The severity level of the unused dependencies lint
317 /// List of unused externs by their names.
318 unused_extern_names: &'b [&'c str],
322 fn from_errors_diagnostic(diag: &crate::Diagnostic, je: &JsonEmitter) -> Diagnostic {
323 let args = to_fluent_args(diag.args());
324 let sugg = diag.suggestions.iter().flatten().map(|sugg| {
325 let translated_message =
326 je.translate_message(&sugg.msg, &args).map_err(Report::new).unwrap();
328 message: translated_message.to_string(),
331 spans: DiagnosticSpan::from_suggestion(sugg, &args, je),
337 // generate regular command line output and store it in the json
339 // A threadsafe buffer for writing.
340 #[derive(Default, Clone)]
341 struct BufWriter(Arc<Mutex<Vec<u8>>>);
343 impl Write for BufWriter {
344 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
345 self.0.lock().unwrap().write(buf)
347 fn flush(&mut self) -> io::Result<()> {
348 self.0.lock().unwrap().flush()
351 let buf = BufWriter::default();
352 let output = buf.clone();
357 je.fluent_bundle.clone(),
358 je.fallback_bundle.clone(),
362 je.track_diagnostics,
364 .ui_testing(je.ui_testing)
365 .emit_diagnostic(diag);
366 let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
367 let output = String::from_utf8(output).unwrap();
369 let translated_message = je.translate_messages(&diag.message, &args);
371 message: translated_message.to_string(),
372 code: DiagnosticCode::map_opt_string(diag.code.clone(), je),
373 level: diag.level.to_str(),
374 spans: DiagnosticSpan::from_multispan(&diag.span, &args, je),
378 .map(|c| Diagnostic::from_sub_diagnostic(c, &args, je))
381 rendered: Some(output),
385 fn from_sub_diagnostic(
386 diag: &SubDiagnostic,
387 args: &FluentArgs<'_>,
390 let translated_message = je.translate_messages(&diag.message, args);
392 message: translated_message.to_string(),
394 level: diag.level.to_str(),
398 .map(|sp| DiagnosticSpan::from_multispan(sp, args, je))
399 .unwrap_or_else(|| DiagnosticSpan::from_multispan(&diag.span, args, je)),
406 impl DiagnosticSpan {
409 suggestion: Option<(&String, Applicability)>,
410 args: &FluentArgs<'_>,
412 ) -> DiagnosticSpan {
418 .map(|m| je.translate_message(m, args).unwrap())
419 .map(|m| m.to_string()),
428 label: Option<String>,
429 suggestion: Option<(&String, Applicability)>,
431 ) -> DiagnosticSpan {
432 // obtain the full backtrace from the `macro_backtrace`
433 // helper; in some ways, it'd be better to expand the
434 // backtrace ourselves, but the `macro_backtrace` helper makes
435 // some decision, such as dropping some frames, and I don't
436 // want to duplicate that logic here.
437 let backtrace = span.macro_backtrace();
438 DiagnosticSpan::from_span_full(span, is_primary, label, suggestion, backtrace, je)
444 label: Option<String>,
445 suggestion: Option<(&String, Applicability)>,
446 mut backtrace: impl Iterator<Item = ExpnData>,
448 ) -> DiagnosticSpan {
449 let start = je.sm.lookup_char_pos(span.lo());
450 let end = je.sm.lookup_char_pos(span.hi());
451 let backtrace_step = backtrace.next().map(|bt| {
452 let call_site = Self::from_span_full(bt.call_site, false, None, None, backtrace, je);
453 let def_site_span = Self::from_span_full(
454 je.sm.guess_head_span(bt.def_site),
461 Box::new(DiagnosticSpanMacroExpansion {
463 macro_decl_name: bt.kind.descr(),
469 file_name: je.sm.filename_for_diagnostics(&start.file.name).to_string(),
470 byte_start: start.file.original_relative_byte_pos(span.lo()).0,
471 byte_end: start.file.original_relative_byte_pos(span.hi()).0,
472 line_start: start.line,
474 column_start: start.col.0 + 1,
475 column_end: end.col.0 + 1,
477 text: DiagnosticSpanLine::from_span(span, je),
478 suggested_replacement: suggestion.map(|x| x.0.clone()),
479 suggestion_applicability: suggestion.map(|x| x.1),
480 expansion: backtrace_step,
487 args: &FluentArgs<'_>,
489 ) -> Vec<DiagnosticSpan> {
492 .map(|span_str| Self::from_span_label(span_str, None, args, je))
497 suggestion: &CodeSuggestion,
498 args: &FluentArgs<'_>,
500 ) -> Vec<DiagnosticSpan> {
504 .flat_map(|substitution| {
505 substitution.parts.iter().map(move |suggestion_inner| {
507 SpanLabel { span: suggestion_inner.span, is_primary: true, label: None };
508 DiagnosticSpan::from_span_label(
510 Some((&suggestion_inner.snippet, suggestion.applicability)),
520 impl DiagnosticSpanLine {
521 fn line_from_source_file(
522 sf: &rustc_span::SourceFile,
526 ) -> DiagnosticSpanLine {
528 text: sf.get_line(index).map_or_else(String::new, |l| l.into_owned()),
529 highlight_start: h_start,
530 highlight_end: h_end,
534 /// Creates a list of DiagnosticSpanLines from span - each line with any part
535 /// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
536 /// `span` within the line.
537 fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
541 // We can't get any lines if the source is unavailable.
542 if !je.sm.ensure_source_file_source_present(lines.file.clone()) {
546 let sf = &*lines.file;
551 DiagnosticSpanLine::line_from_source_file(
554 line.start_col.0 + 1,
560 .unwrap_or_else(|_| vec![])
564 impl DiagnosticCode {
565 fn map_opt_string(s: Option<DiagnosticId>, je: &JsonEmitter) -> Option<DiagnosticCode> {
568 DiagnosticId::Error(s) => s,
569 DiagnosticId::Lint { name, .. } => name,
572 je.registry.as_ref().map(|registry| registry.try_find_description(&s)).unwrap();
574 DiagnosticCode { code: s, explanation: je_result.unwrap_or(None) }